Working with datasets visible only in the source pane

I have several datasets which apparently reside only in the Source pane (RStudio), which date back to previous working sessions and on which I would like to work again, but I am not able import them to the Environment pane

If you mean that there is a View of (some of) the data in the same part of the window as source files are in, then my understanding is no, you cannot export from the data viewer see:

https://support.rstudio.com/hc/en-us/articles/205175388-Using-the-Data-Viewer?mobile_site=true

Wherever you brought the data into R from, you need to bring the data in from that original source again. If you saved your steps in a .R or .Rmd file, that should record where you got the data from and you run those steps again.

Look through the session history. It might have the commands you used to create the data.

If you can see them in the source pane, then you can extract the original data from RStudio's cache. It's not officially supported, but there's an environment called .rs.CachedDataEnv which has a cached copy of anything open in a View tab.

You can pull the values out of that environment (using get) and then assign them into your global environment (using assign) to work with them again.

2 Likes

Thank you Jonathan!
Your response goes in the direction of solving my problem, but, being a relative newcomer, I need some further more detailed help: what should I do to get access to the environment .rs.CachedDataEnv and what should I do afterwards?

Try using code like the following:

lapply(ls(.rs.CachedDataEnv), function(x) { assign(x, get(x, envir = .rs.CachedDataEnv), envir = .GlobalEnv)})

This will copy all of the variables from the cached data environment into your global environment. Afterwards you'll see your Environment pane has a bunch of new data sets with random names -- one for each Viewer tab you have open. You will probably have to open them to see which is which. :slight_smile: Hope that helps!

1 Like

THANKYOU Jonathan,
it works !! Never ever in my life I would have been able to write such a complicated thing!
Hope it may be helpful to other people.