Use Data from one .Rmd file in another

I'm building a website using Rstudio in Rmarkdown. I have several .Rmd files, one of which is used for the coding, data cleaning etc. I want to use the data that I loaded in dataCleaning.Rmd in another file called Visualization.Rmd
When I try to use the data set in visualizations.Rmd e.g. df.head() It doesn't execute it and says df not defined.
I'm new to Rmarkdown and I don't know how to do this. Any help would be appreciated.

I'm building a website using Rstudio in Rmarkdown.

Are you using R Markdown website ? or another output format like blogdown or distill ?

With default generator for R Markdown all the rmd files are rendered. Unless you have set new_session: TRUE in your _site.yml, they should be rendered in the same environment, so they should share the object created. Take care of the order of rendering in this case. It should be alphabetical.

However, if new_session: TRUE, then a new R session will be used for each Rmd file. In that case you can't share the workspace.

Safest way to share elements would be to use a caching-like mechanism. For example, you can use .Rds format to store your object with saveRDS() in dataCleaning.Rmd to read it later with readRDS() later. See the doc of these functions 2readRDS(). Any other file format would work. The idea is write to disk to read it later.

Know that you can also share common content between files

Hope it helps


I'm using the library reticulate and the code is in python. Would saveRDS() work with it?

Is there no other way to use the same data without saving it? Can't i somehow link the two .Rmd files and use the data from dataCleaning.Rmd in visualizations.Rmd?

Did you check with the advice I gave above ?

I believe the default behavior is that it should work if you don't have new_session: TRUE in _site.yaml

You can look at the book chapter about how to import one Rmd as partial in another.

Maybe the specificity here is because you are using python chunk. Without a reproducible example, it is hard to say. It would be great if you can share one.

RDS will work if you use the R objects from your Python objects (, but the idea stays the same in python: You can save your python object in a way python knows, and read it in your later python chunk.

For your interest, packages like feather or now arrow offers objects you can read from both languages using arrow library.

I apologize for responding so late.

Yes i did follow the advice you gave but it still wasn't working. Maybe because i was using python with reticulate, I had a deadline actually so i just skipped this and I simply downloaded my data into another .csv and then read it with python panda's read_csv() in the other .Rmd file.

I didn't know about this library arrow, I'll definitely try this out. Thank you!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.