Can't change params in Rmd documents

血红的双手。 提交于 2019-12-06 22:11:39

问题


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!


回答1:


The error message indicates you have already got an object named params in your current workspace, and you should remove it (rm(params)) before calling rmarkdown::render(). Use ls() to double check the objects in the current environment.




回答2:


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.



来源:https://stackoverflow.com/questions/32024013/cant-change-params-in-rmd-documents

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!