ESS & Knitr / Sweave: How to upload an Rnw file to an interactive session?

This is a terribly simple request, and I cannot believe that I have not yet found a solution for this, but I have searched for it far and without luck.

I have a .Rnw file loaded in Emacs, I use Mn s to compile it. Everything works well, and even opens the R buffer. Excellent. But this buffer is completely useless: it does not contain the objects that I just received.

Example of a minimal .Rnw file:

 \documentclass{article} \begin{document} <<>>= foo <- "bar" @ \end{document} 

Using Mn s , I now have a new R buffer with a loaded session, but:

 > foo Error: object 'foo' not found 

This is disappointing. I would like to play with the data interactively. How do I achieve this? I donโ€™t want me to look for a file in turn, or by region with Cc Cc or something like that every time my code changes. Ideally, this should be exactly the same as the RStudio source function, which leaves me with a fully prepared R session.

I have not tried this with sweave, only with knitr.

EDIT: The eval=TRUE chunk parameter does not seem to produce the correct behavior.

+6
source share
2 answers

This behavior has recently been modified in ESS. Now sweave and knitr are executed directly in the global environment, as if you yourself wrote it on the command line. So wait a couple of weeks until ESSv13.09 comes out or uses the development version.

Alternatively, you can also set ess-swv-processing-command to "% s (% s)" and you will get the same result, except for automatically loading the library.

For the record, knitr (unlike sweave) evaluates everything in its own environment, unless you specify otherwise.

[edit: Something went wrong. I no longer see the correct .ess_weave. Probably some git messes up again. So this is not fixed in 13.09. Commit now. Unfortunately.]

+6
source

Open an interactive R session and then call Sweave right away, I believe this is (unchecked). knitr works the same way, although you need to download the knitr library first.

 > Sweave("yourfile.Rnw") 

However, there is some potential for danger. If you call Sweave in a session after performing other actions, your code may use things earlier in the workspace, which makes your results unrecoverable.

+6
source

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


All Articles