Search code examples
rrstudiosweave

Sweave in RStudio cannot find objects loaded in global environment


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


Solution

  • 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.