Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass df_print an arbitrary function when knitting in RStudio

When using rmarkdown to knit a document in RStudio, I want to use a particular function to print a data.frame (in my case, pander to get a multiline table). The rmarkdown package documentation says that, in addition to the four valid methods that can be passed to df_print, I can also pass it an arbitrary function:

In addition to the named methods you can also pass an arbitrary function to be used for printing data frames.

I want to set the df_print option in the YAML header:

output:
  pdf_document:
    df_print: pander

However, when using the Knit button in RStudio, it errors, saying:

Error: Invalid value for df_print (valid values are default, kable, tibble, paged
Execution halted

But, when I call the render function directly:

render("example.Rmd", pdf_document(df_print = pander))

The document renders just fine, and I get the PDF file I want. Is RStudio inserting itself into the knitting process when I use the Knit button in a way that it doesn't when I just call render directly? Why can't I pass an arbitrary function in the YAML header as well?

I also tried passing pander::pander, but that didn't help.

like image 874
Connor J Avatar asked Jun 08 '17 19:06

Connor J


1 Answers

You can force the evaluation of an expression in the YAML header using !expr:

output: 
  pdf_document:
    df_print: !expr pander::pander
like image 108
RLesur Avatar answered Sep 29 '22 20:09

RLesur