Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change params in Rmd documents

As explained on this site it is possible to define parameters in the YAML header of a Rmarkdown file and the default values specified there can be overwritten with rmarkdown::render("foo.Rmd", params = list(param1 = "bar"). However when I try this I get the following error:

params object already exists in knit environment so can't be overwritten by render params

Here is a minimal reproducible Rmd document. Let's say the file name is test.Rmd.

---
title: "Test"
output: pdf_document
params:
  name: Andreas
---

Hello, my name is `r params$name`.

When I now try rmarkdown::render("test.Rmd", params = list(name = "Jordan") it stops with the error written above.

This is my sessionInfo():

R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin14.4.0 (64-bit)
Running under: OS X 10.10.4 (Yosemite)

locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.11      rmarkdown_0.7.3

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.0      XML_3.98-1.3     digest_0.6.8     MASS_7.3-40     grid_3.2.1      
 [6] plyr_1.8.3       gtable_0.1.2     magrittr_1.5     scales_0.2.5     ggplot2_1.0.1   
[11] stringi_0.5-5    reshape2_1.4.1   jwiDlst_0.1.1    proto_0.3-10     tools_3.2.1     
[16] stringr_1.0.0    munsell_0.4.2    yaml_2.1.13      parallel_3.2.1   colorspace_1.2-6
[21] htmltools_0.2.6 

Thanks for you help!

like image 566
Thomas Neitmann Avatar asked Aug 15 '15 10:08

Thomas Neitmann


People also ask

How do I use parameters in Rmarkdown?

Parameters in an R Markdown document are very simple to use. In the yaml header (the section at the top of the markdown document), you just have to add a couple new lines for your parameters to hardcode them in. Once they're coded in, they will be available in the params object for use in the rest of the analysis.

What is a parameter in R studio?

Parameters are useful when you want to re-render the same report with distinct values for various key inputs, for example: Running a report specific to a department or geographic region. Running a report that covers a specific period in time. Running multiple versions of a report for distinct sets of core assumptions.


1 Answers

Alternatively - knit the document in a new environment by including the option envir = new.env():

rmarkdown::render("test.Rmd", params = list(name = "Jordan"), envir = new.env() )

I like to do this in any case to make sure the rmarkdown report only uses objects that were explicitly defined as part of its own code.

like image 155
DonJ Avatar answered Sep 20 '22 05:09

DonJ