2

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

Can it be done easily ?

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167

1 Answers1

6

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

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • Hard-coding windows paths probably isn't a great idea. You can use "~" with `path.expand()` to more generally get a user's home directory. Also `throw` and `catch` are terms strongly associated with error handling. For clarity you might want to consider verbs with less "baggage". – MrFlick Oct 12 '18 at 15:38
  • good points, I'll edit when I think of better names, I kind of liked these ones. I just know `tryCatch` in `R`. – moodymudskipper Oct 12 '18 at 15:41
  • done, not so happy about the name but it'll do! I would have made it a single function acting different depending on arguments, like `sink`, if I could have found a good name... – moodymudskipper Oct 15 '18 at 19:12
  • 2
    I like the rhyming names :) – MrFlick Oct 15 '18 at 19:14