Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use objects from global environment in Rstudio Markdown

Tags:

r

rstudio

knitr

I've seen similar questions on Stack Overflow but virtually no conclusive answers, and certainly no answer that worked for me.

What is the easiest way to access and use objects (regression fits, data frames, other objects) that are located in the global R environment in the Markdown (Rstudio) script.

I find it surprising that there is no easy solution to this, given the tendency of the RStudio team to make things comfortable and effective.

Thanks in advance.

like image 823
Adam Robinsson Avatar asked Dec 01 '15 20:12

Adam Robinsson


People also ask

How do I import a dataset into R Markdown?

You can click in the upper left menu File > Import Dataset > From Excel and select the file to import it. Then you can copy the code that appears in the R console with the code required for import the data in xlsx and then copy it in a R Markdown code chunk.

What is global environment in RStudio?

When a user starts a new session in R, the R system creates a new environment for objects created during that session. This environment is called the global environment.

What is the difference between RStudio and R Markdown?

To put it simply - R is the actual programming language, RStudio is a convenient interface in which to use it, and R Markdown is a specific type of file format designed to produce documents that include both code and text.


2 Answers

For better or worse, this omission is intentional. Relying on objects created outside the document makes your document less reproducible--that is, if your document needs data in the global environment, you can't just give someone (or yourself in two years) the document and data files and let them recreate it themselves.

For this reason, and in order to perform the render in the background, RStudio actually creates a separate R session to render the document. That background R session cannot see any of the environments in the interactive R session you see in RStudio.

The best way around this problem is to take the code you used to create the contents of your global environment and move it inside your document (you can use echo = FALSE if you don't want it to show up in the document). This makes your document self-contained and reproducible.

If you can't do that, there are a few approaches you can take to use the data in the global environment directly:

  1. Instead of using the Knit HTML button, type rmarkdown::render("your_doc.Rmd") at the R console. This will knit in the current session instead of a background session. Alternatively:

  2. Save your global environment to an .Rdata file prior to rendering (use R's save function), and load it in your document.

like image 174
Jonathan Avatar answered Sep 17 '22 12:09

Jonathan


Going to RStudio´s 'Tools' and 'Global options' and visiting the 'R Markdown' tab, you can make a selection in 'Evaluate chunks in directory', there select the option 'Documents' and the R Markdown knitting engine will be accessing the global environment as plain R code does. Hope this helps those who search this info!

like image 21
Patrik_P Avatar answered Sep 18 '22 12:09

Patrik_P