accessing global environment objects created in cached chunks Rmarkdown documents

天涯浪子 提交于 2019-12-13 14:50:12

问题


I want to run a R script to run a simulation and cache the results for the Rmarkdown document. I am using Rstudio and try to create an HTML report by using knit HTML Here's a simple example.

```{r test_global_env,cache=TRUE}
print(getwd())
source("./test_script.R")
```
```{r test_global_env_2}
print(a)
```

and test_script.R is as follows

  a<-1

When I change the cache option for the chunk to FALSE, print(a) works. If I set it to TRUE, it works the first time, the second time, I get object 'a' not found error. A similar question is Can knit2pdf use the global environment? , but I could not figure out if it applies to my situation. Here is the sessionInfo()

R version 3.0.0 (2013-04-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)

 locale:
 [1] LC_COLLATE=English_United States.1252 
 [2] LC_CTYPE=English_United States.1252   
 [3] LC_MONETARY=English_United States.1252
 [4] LC_NUMERIC=C                          
 [5] LC_TIME=English_United States.1252    

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

 other attached packages:
 [1] knitr_1.2       igraph_0.6.5-2  lubridate_1.3.0


 loaded via a namespace (and not attached):
 [1] digest_0.6.3   evaluate_0.4.3 formatR_0.8   
 [4] memoise_0.1    plyr_1.8       stringr_0.6.2 
 [7] tools_3.0.0 

来源:https://stackoverflow.com/questions/17222380/accessing-global-environment-objects-created-in-cached-chunks-rmarkdown-document

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