Creating persistent data from within an R Markdown code chunk

I am new to R Markdown so haven't quite figured out scoping etc. A few brief google searches have not resolved my issue.

I have written a code chunk that creates a new data frame and I want this data frame to persist when the code chunk finishes executing. Is this possible? Is there something equivalent to <<- that is used from within a function??



Hi @SDPDX! Objects you create inside RMarkdown chunks should actually persist from one chunk to the next (unless you've specifically told a chunk to run in its own session). Functions create a new scope, but chunks oughtn'r be default.

If you're seeing something otherwise, put a reprex together and we can figure out what's going wrong :slight_smile:


No worries! You have it right: when you run chunks using the arrow in RStudio, they're run in your current R session. However, when you Knit the document, the chunks are run sequentially in a single new session, so the won't have access to your existing session and won't bring any objects back to it.

Knits do, however, share that session between chunks (unless configured otherwise), so objects persist between chunks while knitting :slight_smile:

1 Like

This topic was automatically closed 7 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.

Hi Rensa,
Thank you for helping to sort out my problem. Upon further inspection I have realized that running a code chunk (by using the green arrow to the right of the code chunk) does indeed create a persistent copy of a data frame that was created within the code chunk. However, when I knit the document, any data frames created within code chunks do not persist. I can work with this.

Thanks again,