Stargazer 输出出现在文本下方 - rmarkdown to pdf

Stargazer output appears below text - rmarkdown to pdf

问题

在 RStudio 中使用 rmarkdown 时,我的 stargazer(glm()) 输出位于我想要的文本下方。它位于与 r chunk 不同的位置。

PDF 创建完美,只是 stargazer 输出的位置有问题。

背景

我正在尝试创建一个包含大量文本的 PDF,并且在几个段落之间有几个 stargazer glm() 输出。当我在我的 rmarkdown 文件中放置多个 stargazer() 输出然后 "Knit" 到 PDF 时,stargazer() 输出被移到文本下方。

我希望 stargazer output 位于我放置 r chunks 的位置。

以类似的方式使用插入 ggplot2() 输出时,我没有遇到同样的问题。

尝试失败

我已经尝试了尽可能多的组合来定位我的 r chunks 参数。 (以防万一)

我已经尝试了 paragraphs/headers/r-chunks/etc 之前和之后的所有制表符与空格的组合。 (这是我曾经遇到过 ggplot2 输出的问题)

我参考了以下 Whosebug 问题:

可重现的例子

我的工作问题的可重现示例:

---
title: "Untitled"
author: "Me"
output: pdf_document
---

```{r setup, echo = FALSE}
library(stargazer)

mtcars_glm <- glm(formula = vs ~ disp + am + cyl + mpg, family = "binomial", data = mtcars)

```

# Heading1

I have tried creating paragraphs like this.

    I have also tried creating paragraphs with 2 indents.

## Heading2

Lets try to create a couple of nice tables with stargazer.

```{r attempt1, results = 'asis', echo = FALSE}

stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), header = FALSE)

```

And then we will add some text down here, too.

```{r attempt2, results = 'asis', echo = FALSE}

stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), header = FALSE)

```

And some more text.

```{r attempt3, results = 'asis', echo = FALSE}

stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), header = FALSE)

```

Lets see what happens.

### Heading3

```{r plot_attempt}

boxplot(mtcars$mpg ~ mtcars$cyl)
```

# Second Section

## Second Header

这是 3 页输出:

第 1 页

第 2 页

第 3 页

这是我的会话信息:

R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stargazer_5.2.1

loaded via a namespace (and not attached):
 [1] compiler_3.4.4  backports_1.1.2 magrittr_1.5    rprojroot_1.3-2 htmltools_0.3.6 tools_3.4.4     yaml_2.1.19     Rcpp_0.12.16    stringi_1.1.7   rmarkdown_1.9  
[11] knitr_1.20      stringr_1.3.0   digest_0.6.15   evaluate_0.10.1

谢谢

如果你能帮助我,谢谢。我对 LaTeX 或 Pandoc 了解不多,所以我想这是某种知识差距。如果您认为自己找到了解决方案,请随时为我指出正确的方向。

非常感谢。

设置float = FALSE。从手册中,

float: a logical value that indicates whether the resulting table will be a 
floating table (set off, for instance, by \begin{table} and \end{table}).

在 LaTeX 中,table 环境是浮动环境。

例如

stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), float = FALSE)

如果您设置 float = FALSE,您将无法使用浮动环境附带的任何功能,例如标题(即标题)或标签。相反,请考虑使用 float 包设置无条件 table 展示位置。例如,考虑以下文档(我使用 \clearpage 在第 2 页开始 body,因此我们可以在屏幕截图上看到相邻的页面):

---
title: "Untitled"
author: "Me"
header-includes:
   - \usepackage{lipsum}
output: pdf_document
---
\clearpage
\lipsum[1]
```{r setup, echo = FALSE, include = FALSE}
library(stargazer)
mtcars_glm <- glm(formula = vs ~ disp + am + cyl + mpg, family = "binomial", data = mtcars)
```

Table 1 here.
```{r tab1, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), 
          header = FALSE, title = "Table 1")
```

\lipsum[2-3]
Table 2 here.
```{r tab2, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), 
          header = FALSE, title = "Table 2")
```

\lipsum[4]

这给出了

其中Table 2 已经跳到下一页,Table 2 之后的文字已经上移。这就是 LaTeX 的行为方式;它不想在页面底部留下太多白色 space。要坚持 Table 2 跟随一段文本,您可以使用 H 说明符(这需要 float LaTeX 包)。这是相同的文档,但请注意 tab2 块中的 table.placement 参数:

---
title: "Untitled"
author: "Me"
header-includes:
   - \usepackage{float}
   - \usepackage{lipsum}
output: pdf_document
---
\clearpage
\lipsum[1]
```{r setup, echo = FALSE, include = FALSE}
library(stargazer)
mtcars_glm <- glm(formula = vs ~ disp + am + cyl + mpg, family = "binomial", data = mtcars)
```

Table 1 here.
```{r tab1, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), 
          header = FALSE, title = "Table 1")
```

\lipsum[2-3]
Table 2 here.
```{r tab2, results = 'asis', echo = FALSE}
stargazer(mtcars_glm, ci=FALSE, no.space = TRUE, report = c("vc*"), 
          header = FALSE, title = "Table 2", table.placement = "H")
```

\lipsum[4]

这给出了

table 被放置在文本 ("Table 2 here") 之后,即使以在页面底部留下白色 space 为代价。另一种选择是 placeins 包中的 \FloatBarrier;参见 https://tex.stackexchange.com/questions/19766/how-to-control-the-position-of-floating-images

一般来说,您应该将浮动(即 tables 和数字)位置留给 LaTeX。有关广泛的讨论,请参阅 https://tex.stackexchange.com/questions/39017/how-to-influence-the-position-of-float-environments-like-figure-and-table-in-lat