Sweave and the R Session State

dtkaplan's Avatar

dtkaplan

21 Feb, 2011 10:52 PM

When using Sweave, each invocation of compilePdf starts a new R session. That's reasonable for many purposes, since it ensures that the document contains a complete and reproducible set of commands.

However, for beginning students and for document development, it's nice to be able to continue the R session. This lets the writer test out commands on the command line, which is a lot faster than going through the compile/view cycle.

Possibly there is a flag that will allow Sweave to inherit the existing session state. It would suffice to have an optional argument to compilePdf to set that flag.

  1. Support Staff 2 Posted by Josh Paulson on 22 Feb, 2011 03:20 AM

    Josh Paulson's Avatar

    Danny,

    Thanks for the feedback and this is a great idea. We currently keep Sweave running in a separate session, as we earlier had problems with unforeseen side-effects. For example left over graphics devices, altered parameter settings, among others. To your point though, we would like to make something similar to what you mentioned work in the future. If done correctly, it would be a great addition.

    Thanks again,

    Josh

  2. Josh Paulson closed this discussion on 22 Feb, 2011 03:20 AM.

  3. JJ Allaire re-opened this discussion on 22 Feb, 2011 11:34 AM

  4. Support Staff 3 Posted by JJ Allaire on 22 Feb, 2011 11:34 AM

    JJ Allaire's Avatar

    Danny,

    Quick addition to what Josh said: it looks to me like the Sweave function is as a practical matter almost always invoked using R CMD Sweave. When invoking it inside an R sesssion it has the side-effects on the graphcs device(s) Josh mentioned plus it can actually break R stdout (because it sinks output and then if an error occurs during the call to Sweave it sometimes doesn't restore the old sink!). When we had Sweave in-process we had all kinds of hacks to try to "fixup" the session in the presence of these issues but never quite battened down all of the hatches.

    One approach I could suggest: we could have an option for the R CMD Sweave process to save its environment after the run and then we could read it back into RStudio after that. Issues/problems with this might include an environment with large datasets that takes many seconds to load. Also, I don't think R CMD Sweave alone could do this -- we'd probably have to write our own variation of this.

    Anyway, just wanted you to know that we understand the use case your advocating for but there are some sticky problems between us and a satisfactory solution.

    Best,

    J.J.

  5. JJ Allaire closed this discussion on 22 Feb, 2011 11:35 AM.

  6. JJ Allaire re-opened this discussion on 22 Feb, 2011 08:58 PM

  7. Support Staff 4 Posted by JJ Allaire on 22 Feb, 2011 08:58 PM

    JJ Allaire's Avatar

    Note you can also approximate this by doing "Run All" instead of Compile PDF. It will extract all the R code and run it in the current environment (which I had thought of as a bug, but in your case it sounds more like a feature).

    Joe

  8. Joe Cheng closed this discussion on 02 Mar, 2011 05:17 PM.

Comments are currently closed for this discussion. You can start a new one.