0

I pretty much copied word-for-word the example from this site on how to create multiple PDFs in knitr from one for-loop: http://www.reed.edu/data-at-reed/software/R/markdown_multiple_reports.html

When I try calling knitr::kable(cars) or kable(cars) the output of each PDF appears as one long column instead of an actual table, as shown in the photo.

enter image description here

Any idea what causes this? Here's the .R and the .Rmd code called in each iteration of the loop:

.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)

I was able to get are relatively normal looking table by using kable(cars, format = "latex")

enter image description here

I was hoping to get some quick and decent html tables, and now when I try to format in LaTeX with something like kable(cars, format = "latex", booktabs = T), I get this error:

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
Nicholas Hassan
  • 949
  • 2
  • 10
  • 27
  • 2
    What is the output supposed to look like? It's unclear exactly what you are trying to do with `cars$lat <- sapply(rownames(cars), function(x) round(runif(1, 30, 46), 3))`. Are you just trying to round one columns? Are you trying to store multiple columns in the `lat` column of `cars`? – MrFlick Dec 01 '17 at 22:08
  • @MrFlick I added an edit to show the kind of table it should look like. Re: $lat and $lon: They're just some random extra columns to add on. The example in the site includes some map data too, but I was just testing the ability to create multiple PDFs with one for-loop. – Nicholas Hassan Dec 01 '17 at 22:16

1 Answers1

0

You have to modify the .Rmd file to include the package in output Create a YAML header with

header-includes:
   - \usepackage{booktabs}

modified .Rmd file which now works for me (I was also reproducing your error)

---
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)
```

Now this produces four files

enter image description here

Cedric
  • 2,412
  • 17
  • 31
  • That does not help me, who has the exact same problem only knittering for word instead – Jørgen K. Kanters Jul 10 '18 at 07:21
  • Some issues related to rmarkdown and tables https://stackoverflow.com/questions/47704329/how-to-format-kable-table-when-knit-from-rmd-to-word-with-bookdown/47739242#47739242 – Cedric Jul 10 '18 at 12:26