Render .Rmd from Rscript does not work

Hi!
I used RStudio quite a bit but I am new to using RMarkdown.

I created an .Rmd file to create .pdf-reports. It includes text, R-chunks with ggplot2-plots and tables like:

```{r}
cars <- mtcars[rownames(mtcars)==car,]
m <- as.matrix(cars)
m
kable(m,format="latex",booktabs=TRUE,align="c") %>%
  kable_styling(full_width = T,font_size = 20)
rm(m)

and it works fine when knitting it to pdf!!!

But if I run it from a simple R script (I need to do that as I want to create many of these reports from within a loop) and use

```{r}
for (car in unique(rownames(mtcars))){
  rmarkdown::render(input = "test.Rmd",
                    output_format = "pdf_document",
                    output_file = paste("test_report_", car, Sys.Date(), ".pdf", sep=''))

}

it does not work anymore.

I will get this error:

! LaTeX Error: Environment tabu undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.141 \begin{tabu}

pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS test_2.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test_2.pdf --template "C:\Users\NK\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43

I loeaded all needed packages in both R and Rmd-file.

library(knitr)
library(markdown)
library(rmarkdown)
library(kableExtra)

But somehow it wants me to define an environment for the table I create with kable. How can I do that? It took me quite some time to adapt the layout for my tables and plots in the .Rmd file and now R does not seem to recognize any of it.

1 Like

These may (possibly) help:

1 Like

The problem is not on the R side, but on pandoc / LaTeX side. The ! LaTeX Error: Environment tabu undefined message means that a required LaTeX macro is not defined.

I found it rather common problem with Rmarkdown. It can get a bit confusing when using the default templates; as a solution I built my own .tex template, over which I have full control and in which I include all the necessary LaTeX calls (in your case \usepackage{tabu} seems to be missing).

Your can have a look at my Github RPTG repository, and have a look at my template. It is unlikely to suit your needs as it is, but it could be an inspiration for you to make your own.

Thank you for you help. Unfortunately, this does not seem to change anything for my problem. But I will keep it in mind in case this problem appears. I have never used "shiny", so I am not familiar with its implementation.

It is, indeed, very confusing as it works perfectly fine when just knitting it directly from the .Rmd-file. This is what I find so confusing. At that point the LaTeX macro seems to be there.
I thought that "render" basically does the same thing as knitting and is just called from another place - the Rscript.

My problem is, that I am not familiar with LateX. So, I don't have any idea how it works. I will try to follow your example and see if I can built my own .tex template.

But how and where would I then include it? In my R.file or in the .Rmd-file or do I need to call it when using the render function?

Thank you for help!!

My suggestion:

  • copy the Report.tex template from my github to your working directory (or someplace else, and use the full path; working directory makes debugging easier)
  • in the YAML heading of your .Rmd file include this reference:
output:
  pdf_document:
    latex_engine: xelatex
    template: Report.tex

The indentations are important.

  • then run your script again; it should not fail on \begin{tabu}, since package tabu is attached in my template.

I realize this is probably old news by now, but I remembered there is an easier way to get the {tabu} LaTeX macro to work than building it into a a custom tex template: include it in header-includes section of the YAML header of your Rmd file.
For example (copied from http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf by Hao Zhu)

header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage[table]{xcolor}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage[normalem]{ulem}

You are likely to end up writing your own template anyway to get more control, but this will help to start out with generated reports.

2 Likes