简体   繁体   中英

Recovering dataframe from Rstudio viewer?

在此处输入图像描述

Hello, I made a dataframe in an rstudio session named 'pull3'. I then left that session and started a new project. When I went back to the previous session, I can see the dataframe from when I previously used the Viewer() to find it, but it is no longer in the environment variables and I can't seem to download/reference it at all.

It looks like it is stored in data://pull3...does anyone know how to access this?

Thanks.

Inside your project directory you will find a hidden folder called .Rproj.user . Further in your directory tree, eventually you will find a folder called viewer-cache which contains a .Rdata file.

I suspect that the directory names are random, so you'll have to do a little digging. If you're on Linux or MacOS, you might try find ~/Project | grep ".Rdata" find ~/Project | grep ".Rdata" to speed up the process.

Here's an example of toy data I just saved.

Project
└── .Rproj.user
    └── D31E74F4
        └── viewer-cache
            └── 6F635E12.Rdata

You can load that .Rdata with load() :

load("~/Project/.Rproj.user/D31E74F4/viewer-cache/6F635E12.Rdata")

And there will then be an object with the same name as the file in your global environment:

head(`6F635E12`)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

If you are not using a project, you may find the file in ~/.rstudio-desktop/viewer-cache .

Based on Ian's answer, here is a function that returns a list containing all of the objects it recovers.

recover_data_viewer_cache_objects <- function() {
  active_project <- rstudioapi::getActiveProject()

  if(is.null(active_project)) {
    if(.Platform$OS.type == "windows")
      viewer_cache_files <- Sys.glob(file.path(Sys.getenv("localappdata"),
                                               "RStudio-Desktop",
                                               "viewer-cache",
                                               "*.Rdata"))
    else
      viewer_cache_files <- Sys.glob(file.path("~",
                                               ".rstudio-desktop",
                                               "viewer-cache",
                                               "*.Rdata"))
  } else {
    viewer_cache_files <- Sys.glob(file.path(active_project,
                                             ".Rproj.user",
                                             "*",
                                             "viewer-cache",
                                             "*.Rdata"))
  }

  # record environment, load cached objects, return environment diff
  # (apply family does not work with base::load() or base::get())
  ls_0 <- ls()
  for(o in viewer_cache_files)
    load(o)
  new_objects <- setdiff(ls(), c(ls_0, "ls_0", "o"))
  recovered_objects <- list()
  for(o in seq_along(new_objects))
    recovered_objects[[new_objects[o]]] <- get(new_objects[o])

  recovered_objects
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM