3

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 
Community
  • 1
  • 1
  • Hmm. Looks like this is likely an Rstudio issue. I don't have Rstudio, but the html comes out fine for me when I create it directly by launching an R session and run `knit2html("main.rmd")` (where `"main.rmd"` contains the text in your code block). I even deleted the resultant `main.html` and `main.md` before running `knit2html` the second time. – Josh O'Brien Jun 21 '13 at 01:12
  • Try entering the command at the console in RStudio and see if the problem is still there. – Gregor Thomas Jun 21 '13 at 16:52
  • Can you post `library(knitr); sessionInfo()`? Without that, you have to see [FAQ 1](http://bit.ly/knitr-faq) – Yihui Xie Jun 21 '13 at 21:29
  • I did update R packages to see if that would fix the problem. It didn't. I also suspect Rstudio's knit html button might be the problem. I am trying knit2html now. – statistician_in_training Jun 22 '13 at 00:17
  • @statistician_in_training did you try to clean up the cache files or start from a fresh directory after upgrading? the worst case is you specify `cache.vars='a'` for your first chunk, but normally that is unnecessary – Yihui Xie Jun 25 '13 at 00:22

0 Answers0