我做了以下功能,它将在您的主文件夹中放置一个临时文件,并在默认情况下获取时将其删除:
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"
Run Code Online (Sandbox Code Playgroud)
如果一个 RStudio 会话有错误并且我无法绘图,那么在我的情况下很有用,所以我可以将它发送给另一个。
例如,只需更改默认路径即可在网络中轻松地在同事之间传递数据。
感谢@MrFlick 的建议