问题描述
问题
在RStudio中使用rmarkdown时,我的stargazer(glm())
输出位于我想要的文本下方.它位于与r chunk
不同的位置.
When using rmarkdown in RStudio, my stargazer(glm())
output gets positioned below the text that I would like it to. It gets positioned in a different spot than the r chunk
is.
PDF完美地创建了,只是stargazer
输出的位置有问题.
The PDF is created perfectly, it's just the position of the stargazer
output that is a problem.
背景
我正在尝试创建一个包含大量文本并在几个段落之间输出几个stargazer
glm()
的PDF.当我在rmarkdown文件中放置多个stargazer()
输出,然后编织"为PDF时,stargazer()
输出将向下移动到文本下方.
I am trying to create a PDF with lots of text and a couple of stargazer
glm()
output between a few of the paragraphs. When I put more than one stargazer()
output in my rmarkdown file and then "Knit" to PDF, the stargazer()
output gets moved down below the text.
我希望将stargazer output
放到放置r chunks
的位置.
I would like the stargazer output
to get positioned where I put the r chunks
.
使用类似的方式插入ggplot2()
输出时,我没有相同的问题.
I do not have the same problem when using inserting ggplot2()
output in a similar manner.
尝试失败
我已经尝试了尽可能多的组合,这些组合我都知道如何定位我的r chunks
参数. (以防万一)
I have tried as many combinations as I know how of positioning my r chunks
arguments. (Just in case)
在段落/标题/r-chunks/等之前和之后,我尝试了制表符与空格的每种组合. (那是我曾经用ggplot2输出遇到的一个问题)
I have tried every combination of tab vs. spaces, before and after paragraphs/headers/r-chunks/etc. (That was a problem I had once with ggplot2 output)
我引用了以下StackOverflow问题:
I have referenced the following StackOverflow Questions:
- 谢一辉的例子
- Stargazer和RStudio
- 我曾尝试在上述文章之一中提到的块的末尾(和之间)使用"\".
- Using fig.pos = "H" and fig.pos = "h"
- This very similar question
- Yihui Xie's examples
- Stargazer and RStudio
- I tried using "\" at the end of (and between) chunks as mentioned in one of the above posts.
可复制示例
我的工作问题的可复制示例:
A reproducible example of my work problem:
---
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页
这是我的sessionInfo:
Here is my 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不太了解,所以我认为这是某种知识鸿沟.如果您认为自己已经找到了解决方案,也请随时为我指明正确的方向.
If you can help me, thanks. I do not know much about LaTeX or Pandoc, so I imagine it is some sort of knowledge gap. Feel free just to point me in the right direction, too, if you think you've found a solution.
我很感激.
推荐答案
如果设置float = FALSE
,则将没有浮动环境附带的任何功能,例如标题(即标题)或标签.相反,请考虑使用float
包设置无条件的表放置.作为示例,请考虑以下文档(我使用\clearpage
在第2页上启动正文,因此我们可以在屏幕截图上看到相邻的页面):
If you set float = FALSE
, you will not have any of the features that come with a floating environment, such as captions (i.e. the title) or labels. Instead, consider setting an unconditional table placement with the float
package. As an example, consider the following document (I use \clearpage
to start the body on page 2 so we can see the adjoining pages on the screenshot):
---
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]
给出
其中表2已移至下一页,并且表2之后的文本已上移.这就是LaTeX的行为方式;它不想在页面底部留下太多空白.要坚持使用表2,请使用H
指示符(需要使用float
LaTeX软件包).这是同一文档,但请注意tab2
块中的table.placement
参数:
where Table 2 has been bumped to the following page, and the text after Table 2 has been moved up. This is how LaTeX behaves; it does not want to leave too much white space at the bottom of the page. To insist that Table 2 follows a piece of text, you can use the H
specifier (which requires the float
LaTeX package). Here's the same document, but note the table.placement
argument in the tab2
chunk:
---
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]
给出
该表放置在文本之后(此处为表2"),即使以在页面底部保留空白为代价.另一种选择是placeins
包中的\FloatBarrier
;参见 https://tex.stackexchange.com/questions/19766 /如何控制浮动图像的位置.
The table is placed after the text ("Table 2 here"), even at the expense of leaving white space at the bottom of the page. An alternative is \FloatBarrier
from the placeins
package; see 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-float-environments-like-figure-and-table-in-lat 中进行了广泛的讨论.
In general, you should leave float (i.e. tables and figures) placements to LaTeX. See https://tex.stackexchange.com/questions/39017/how-to-influence-the-position-of-float-environments-like-figure-and-table-in-lat for an extensive discussion.
这篇关于Stargazer的输出显示在文本下方-rmarkdown至pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!