0

To preface this, I think my question is related to this, but it's not exactly the same: How to source R Markdown file like `source('myfile.r')`?

Basically, I perform most of my data cleaning and analysis in Rmarkdown files because the visual separation between chunks of code and my own comments on what should be done for the analysis/cleaning is very helpful to me. It also helps that within Rstudio if you run a table, df, then it'll display an interactive snippet of it in the document. This is all very helpful in complicated cleaning/analyses. So in other words, I'd like to develop in one R-Markdown file and write in another R-Markdown file. Splitting/writing the code into a source.R file is not ideal, unless there was a very automated and reproducible way to do it.

The issue is that for reports, I'd sometimes like to take specific objects that were generated from these lengthy data-cleaning and analyses files in Rmarkdown. For example, let's say that during my data-cleaning in Rmarkdown-file-1, there was a particular table that was giving me trouble problematic.df and that I'd like to call in my report or possibly perform further manipulations in my report (Rmarkdown-file-2).

So ultimately I think this is the question:

How can I call any arbitrary object generated at any arbitrary point of one Rmarkdown file in another Rmarkdown file?

Obviously, the above would be the ideal, but it sounds unreasonable, so perhaps this is a better question/request:

How can I call any arbitrary objects generated by the end of one Rmarkdown file in another Rmarkdown file?

Upon further reflection, my question might already be answered in the post I linked, but it's been a while since that question was posted and perhaps there are new solutions or perspectives on this issue.

NAccumbens
  • 33
  • 4
  • Using `saveRDS()`, `save()` or `save.image()` at the end of one file will create a file containing objects created in it. You can then `readRDS()` or `load()` that file to recreate them in another document. (Using `saveRDS()` on individual objects is less convenient but more flexible than using `save.image()`. `save()` falls in between, but I'd recommend one of the others.) – user2554330 Aug 13 '20 at 23:49
  • @user2554330 This sounds good. Do you know if it's possible to load the saved environment into something like a list? My intention would be to load multiple environments, while keeping them separate from each other. – NAccumbens Aug 17 '20 at 15:31
  • With `readRDS()` you get the value of the saved object; you can do what you like with it. With `load()`, you would need to create a list of environments and specify which one you should load into. – user2554330 Aug 17 '20 at 18:02

0 Answers0