Can you run Sweave in a new environment?

I am writing a package that works with Sweave and would like to be able to isolate Sweave's call from the global environment. Here is how I think it should work, but does not:

sweaveEnv <- new.env() eval(Sweave('myDocument.Rnw'), envir=sweaveEnv) 

Ideally, I would like to keep this environment for later debugging.

 save(sweaveEnv, file='mySweaveEnv.Rda') 
+4
source share
2 answers

You can simply start the new R process:

 system("Rscript -e 'Sweave(\"test.Rnw\");save.image(file=\"mySweaveEnv.Rda\")'") 
+3
source

If your main goal is to save objects in this environment, you can use the knitr package with the chunk cache=TRUE option, in which case all objects in the piece are saved in files, and you can load them into R later for debugging; knitr will create a .rdx and .rdb respectively for a fragment in the cache directory, and you can call lazyLoad() to load objects in these databases into R.

If you want to do more things with the environment in which the piece is evaluated, knitr also has chunk hooks and you have access to the environments through hooks; so basically you can do whatever you want - knitr makes almost everything accessible to the user. Note that each chunk is evaluated in a different empty environment when the cache is turned on. I have no examples on this subject yet, so please let me know if my description here is clear enough.

+3
source

Source: https://habr.com/ru/post/1392152/


All Articles