moodymudskipper
moodymudskipper

Reputation: 47310

How can I conveniently move objects between sessions

It's obviously not something to advise in an ideal workflow but sometimes it can be useful.

Can it be done easily ?

Upvotes: 2

Views: 406

Answers (1)

moodymudskipper
moodymudskipper

Reputation: 47310

I made the following functions, it will put a temp file in your home folder and delete it when it's fetched by default :

shoot <- function(..., list = character(), rm = FALSE){
  path <- file.path(path.expand("~"),"temp_object.RData")
  save(..., list =  list, file = path)
  if(rm) rm(list = c(list,as.character(substitute(alist(...))[-1])),
                envir = parent.frame())
  invisible(NULL)
}

loot <- function(rm = TRUE){
  path <- file.path(path.expand("~"),"temp_object.RData")
  if(file.exists(path)){
    load(path,envir = parent.frame())
    if(rm) file.remove(path)
  } else {
    stop("nothing to loot!")
  }
  invisible(NULL)
}

test <- "abcd"
shoot(test)
rm(test)
loot() # in practice from another session
test
# [1] "abcd"

Useful in my case if one RStudio session has a bug and I can't plot, so I can send it to another.

With a simple change in the default path can be used in a network to easily pass data between colleagues for example.

Thanks to @MrFlick for suggestions

Upvotes: 6

Related Questions