我正在尝试为不同的表创建单独的Rmd文件,其中每个表都被呈现为单独的pdf。由于一些复杂的格式问题,我正在尝试使用xtable来解决这个问题。我有一些表格,我预计会填满一个8.5x11.0的页面,边距为1英寸。但是当我渲染它们时,pdf的第一页是空白的,而第二页则是整个格式正确的表格。我正在寻求帮助以使其正常工作。这是我最小的可行示例...
---
output: pdf_document
tables: true
geometry: margin=1.0in
---
```{r results='asis', echo=FALSE, warning=FALSE, eval=TRUE}
require(xtable)
# set up data frame
df <- data.frame(Label=c(letters[1:26], letters[1:13]), Numbers=1:39)
strCaption <- "\\textbf{Supplementary Table 1. This table is just produced with some random data and does not mean anything.}"
# set up xtable output
print(xtable(df, caption = strCaption, label = ""),
size = "normalsize",
include.rownames = FALSE,
include.colnames = TRUE,
caption.placement = "top",
comment=FALSE
)
```
这会保存为test.Rd
,我使用...
library(rmarkdown)
render("test.Rmd"
如果我更改边距大小,它似乎只会影响左边距和右边距。如果我缩小字体的大小,它将适合一页,但我想保持字体大小原样,并摆脱空白的第一页。想法?我是一个乳胶新手,所以对于遗漏一些明显的东西道歉。
答案 0 :(得分:2)
我认为如果添加floating = FALSE
参数,它应该可以解决问题。我认为这是定义表时LaTex
这个参数的!h
等价物。我还将library
调用与其他代码分开(并使用library
代替require
),但这样风格和挑剔。
---
output: pdf_document
tables: true
geometry: margin=1.0in
---
```{r echo=FALSE, warning=FALSE, eval=TRUE, results='hide'}
library(xtable)
```
```{r eval=TRUE, echo=FALSE, results='asis'}
# set up data frame
df <- data.frame(Label=c(letters[1:26], letters[1:13]), Numbers=1:39)
strCaption <- "\\textbf{Supplementary Table 1. This table is just produced with some random data and does not mean anything.}"
# set up xtable output
print(xtable(df, caption = strCaption, label = ""),
include.rownames = FALSE,
include.colnames = TRUE,
caption.placement = "top",
comment=FALSE,
floating=FALSE
)
```
答案 1 :(得分:2)
感谢ted-dallas的帖子,我能够发现table.placement
选项符合我的要求而无需关闭浮动...
---
output: pdf_document
tables: true
geometry: margin=1.0in
---
```{r results='asis', echo=FALSE, warning=FALSE, eval=TRUE}
require(xtable)
# set up data frame
df <- data.frame(Label=c(letters[1:26], letters[1:13]), Numbers=1:39)
strCaption <- "\\textbf{Supplementary Table 1. This table is just produced with some random data and does not mean anything.}"
# set up xtable output
print(xtable(df, caption = strCaption, label = ""),
size = "normalsize",
include.rownames = FALSE,
include.colnames = TRUE,
caption.placement = "top",
comment=FALSE,
table.placement = "!ht"
)
```
这会生成我正在寻找的输出。