【问题标题】:Rmarkdown table pushed to two pages when it can fit in one当 Rmarkdown 表可以放入一页时,它会被推送到两页
【发布时间】:2015-12-07 17:11:34
【问题描述】:

我正在尝试为不同的表创建单独的 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"

如果我更改边距大小,它似乎只影响左右边距。如果我缩小字体大小,它将适合一页,但我想保持字体大小不变并摆脱空白的第一页。想法?我是一个乳胶新手,所以很抱歉遗漏了一些明显的东西。

【问题讨论】:

    标签: r latex markdown xtable


    【解决方案1】:

    我认为如果您添加 floating = FALSE 参数,它应该可以解决问题。我认为这是定义表时 !h 此处参数的 LaTex 等效项。我还将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
          )
    ```
    

    【讨论】:

    • 谢谢!这摆脱了最初的空白页并将其全部放在一页上,但现在标题消失了。我可以在代码块之前使用 markdown 添加它,但最好让它与标题选项一起使用。
    • 啊,现在我明白你所说的!h 的意思了——xtable 有一个table.placement 选项,它接受代码
    【解决方案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"
          )
    ```
    

    这会生成我正在寻找的输出。

    【讨论】:

    • 你的解决方案绝对比我的更优雅。好东西。
    猜你喜欢
    • 1970-01-01
    • 2011-09-13
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多