How to Use Objects from Global Environment in Rstudio Markdown

How to use objects from global environment in Rstudio Markdown

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.

Using Data from Environment in R Markdown

There are 2 ways to load the data myData to your .RMD file:

  1. Don't knit your file with the Rstudio "knit" button:

    library(knitr)
    knit('your_file.Rmd')

This will take your recent environment into account and the error should be gone.


  1. Store your myData as "myData.RData" and load it manually in your RMD file

    ```{r load myData, include=FALSE}
    load("myData.RData")

    If you do it this way you can use the "knit" button from RStudio.

I hope one of this ways is a good solution for you.

R Studio: How to load a variable into the Global Environment by running a Knitr document

You can manually render your document within the global environment by executing this in the RStudio console:

rmarkdown::render("my_document.Rmd", envir=.GlobalEnv)

I do this frequently and wrote an RStudio Addin for this purpose.

Iteratively create global environment objects from tibble

As we already returned the object value in a list, we need only to specify the lambda function i.e. .x returns the value of the list element which is a tibble and extract the column

library(purrr)
list_output <- map(list_df, ~.x$ave_neg_U_ml)

If the intention is to create global objects, deframe, convert to a list and then use list2env

library(tibble)
list2env(as.list(deframe(neg_tibble_string)), .GlobalEnv)

R markdown to use variables existing in the environment and not run code again

In case loading your very large data set is the problem, try special packages for reading your data like readr.

Alternative, since you working on the design or representation in you PDF, you can work on a subset of your data like only on the first 100000 rows.

Otherwise, I use the following code in my first code chunk

library(knitr)
# global setting to create this document
opts_chunk$set(cache=TRUE,
echo=TRUE, # set to FALSE to remove the code output
warning=FALSE,
message=FALSE)

so I don't need to set cache=TRUE in each chunk.

Hope this helps.



Related Topics



Leave a reply



Submit