简体   繁体   中英

Knitr compiling and running all at the same time in RStudio

For running an Rnw file in RStudio, one can compile or run all . Compiling does not see the variables in the current environment, and the current environment does not see the variables created while compiling. I would like to see how the output would look when I compile, and I debug the code using the environment. This requires me to compile and run , which performs the same calculations twice, which is very impractical for large projects. Is there a way to compile and have the output be seen in the environment?

When you knit a document, the work happens in a different R session, which is why you can't examine the results in the current session.

But you have a lot of choices besides run all . Take a look at the Run button: it allows you to run chunks one at a time, or run all previous chunks, etc.

If some of your chunks take too long to run, then you should consider organizing your work differently. Put the long computations into their own script, and save the results of that script using save() . Run it once, then spend time editing the display of those results in multiple runs in the main .Rnw document.

Finally, if you really want to see variables at the end of a run of your vignette, you can add save.image(file = 'vignette.RData') at the end, and in your interactive session, use load('vignette.RData') to load the values for examination. This won't necessarily give you an accurate view of the state of things at the end of the run, because it will load the values in addition to anything you've already got in your workspace, it won't load option settings or attach packages, but it might be enough for debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM