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

时间:2018-05-09 00:33:23

标签: r pdf r-markdown pandoc stargazer

问题

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

PDF是完美创建的,它只是stargazer输出的位置,这是一个问题。

背景

我正在尝试创建一个包含大量文本的PDF,并在几个段落之间输出几个stargazer glm()。当我在我的rmarkdown文件中放置多个stargazer()输出然后"编织"对于PDF,stargazer()输出会在文本下方向下移动。

我希望stargazer output能够放在r chunks的位置。

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

尝试失败

我尝试过多次组合,因为我知道如何定位我的r chunks参数。 (以防万一)

我已尝试过tab / headers / r-chunks / etc之前和之后的tab与空格的每个组合。 (这是我曾经用ggplot2输出的一个问题)

我引用了以下StackOverflow问题:

可重复的示例

我工作问题的一个可重复的例子:

---
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页 Page 1

第2页 Page 2

第3页 Page 3

这是我的sessionInfo:

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知之甚少,所以我想这是一种知识差距。如果您认为自己找到了解决方案,请随意指出我正确的方向。

我很感激。

2 个答案:

答案 0 :(得分:3)

如果设置float = FALSE,则不会具有浮动环境附带的任何功能,例如字幕(即标题)或标签。相反,请考虑使用float包设置无条件表格位置。作为示例,请考虑以下文档(我使用\clearpage启动第2页的正文,以便我们可以在屏幕截图中看到相邻的页面):

---
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]

给出了 enter image description here

表2已经碰到了下一页,表2之后的文字已经向上移动了。这就是LaTeX的表现;它不希望在页面底部留下太多的空白区域。要坚持表2遵循一段文本,您可以使用H说明符(需要float LaTeX包)。这是同一个文档,但请注意table.placement块中的tab2参数:

---
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]

给出了 enter image description here

表格放在文本后面(“表2”),即使是在页面底部留下空白区也是如此。替代方案是\FloatBarrier包中的placeins;见https://tex.stackexchange.com/questions/19766/how-to-control-the-position-of-floating-images

一般情况下,您应该将浮动(即表格和数字)放置在LaTeX上。有关详细讨论,请参阅https://tex.stackexchange.com/questions/39017/how-to-influence-the-position-of-float-environments-like-figure-and-table-in-lat

答案 1 :(得分:1)

设置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环境是一个浮动环境。

E.g。

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