knitr表格显示为一个长列

时间:2017-12-01 22:02:37

标签: r knitr

我几乎从这个网站逐字逐句地复制了如何在一个for循环中在knitr中创建多个PDF:http://www.reed.edu/data-at-reed/software/R/markdown_multiple_reports.html

当我尝试拨打knitr::kable(cars)kable(cars)时,每张PDF的输出显示为一个长列,而不是实际的表格,如图所示。

enter image description here

知道是什么原因引起的吗?这是在循环的每次迭代中调用的.R和.Rmd代码:

.R

library(knitr)
library(markdown)
library(rmarkdown)
mt <- mtcars[1:5,]
for (car in unique(rownames(mt))){
     rmarkdown::render('test.Rmd',
                output_format = "pdf_document",
                output_file =  paste(car, ' report.pdf', sep=''), 
                output_dir = '~/')

test.Rmd

library(knitr)
library(markdown)
library(rmarkdown)
cars <- mtcars[rownames(mtcars)==car,]

# create example data 
x <- sample(1:10, 1)
cars <- do.call("rbind", replicate(x, cars, simplify = FALSE))

# create hypothetical lat and lon data 
cars$lat <- sapply(rownames(cars), function(x) round(runif(1, 30, 46), 3))
cars$lon <- sapply(rownames(cars), function(x) round(runif(1, -115, -80),3))
knitr::kable(cars)
kable(cars)

使用kable(cars, format = "latex")

,我能够获得相对正常的表格

enter image description here

我希望得到一些快速而且不错的html表格,现在当我尝试使用类似kable(cars, format = "latex", booktabs = T)的格式在LaTeX中进行格式化时,我收到此错误:

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43

1 个答案:

答案 0 :(得分:0)

您必须修改.Rmd文件以将包包含在输出中 使用

创建YAML标头
header-includes:
   - \usepackage{booktabs}

修改后的.Rmd文件现在适用于我(我也在重现你的错误)

---
title: "Title"
author: "Me"
header-includes:
   - \usepackage{booktabs}
output:
    pdf_document
---
```{r kable}
library(knitr) 
library(markdown)
library(rmarkdown) 
cars <-mtcars[rownames(mtcars)==car,]

# create example data 
x <- sample(1:10, 1) 
cars <- do.call("rbind", replicate(x, cars, simplify =FALSE))

# create hypothetical lat and lon data 
cars$lat <- sapply(rownames(cars), function(x) round(runif(1, 30, 46), 3))
cars$lon <-sapply(rownames(cars), function(x) round(runif(1, -115, -80),3))
knitr::kable(cars, format = "latex", booktabs = T)
```

现在生成四个文件

enter image description here