3

Reading this post I thought I could use R objects loaded in the global environment straight away in .Rnw documents and compile them directly to PDF in RStudio. But unfortunately that does not work for me...

I would like to make a summary of a data.frame that undoubtably is already loaded in my global environment.

Evaluating summary(JLLdata) in the console produces the correct output. However, when I try to include summary(JLLdata) in my .Rnw file, I get the error (chunk 1) Object 'JLLdata' not found.

Here my .Rnw Syntax:

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<JLLdata>>=
summary(JLLdata)
@

\end{document}

And here a screenshot from R Studio (you can see that JLLdata is in the global environment and the Compile PDF error)

enter image description here

chamaoskurumi
  • 2,271
  • 2
  • 23
  • 30

1 Answers1

4

The code from the Sweave .Rnw document is run in a separate R session, so it does not have access to objects you've loaded or created in the console. You have to explicitly load the data in a Sweave code chunk in order for it to be accessible when you call summary.

Thomas
  • 43,637
  • 12
  • 109
  • 140
  • Thx @Thomas, you were right. Including `load(file = "/path/globalenvironmnetsaved.RData")` does the job..It is a bit of an annoying solution though, since my `.RData` is 200MB big and hence compiling the PDF takes a while... – chamaoskurumi Mar 31 '15 at 20:30
  • 1
    Consider using knitr instead, which gives a caching option that greatly reduces compilation times. – Thomas Mar 31 '15 at 20:38
  • 1
    Or just use `Sweave()` from the command line, since R itself has the flexibility that GUI-button convenience inevitably takes away. Perhaps write a little utility function to streamline the process, putting it in your .Rprofile or a personal package for easy loading. Something like: `sweave2pdf <- function(file) { texfile <- Sweave(file); tools::texi2pdf(texfile) }` – Josh O'Brien Mar 31 '15 at 20:45