Marro
Marro

Reputation: 105

R file.copy current script from Source as Background Job error in CallRemote: RStudio did not respond to rstudioapi IPC request

Based on Get the path of current script, I get the filepath of the current script, which works when called from different sources.

In this case I want to Source as Background Job from RStudio.

When I run this, it sometimes works fine, and sometimes I get the following error message:

Error in callRemote(sys.call(), parent.frame()): RStudio did not respond to rstudioapi IPC request Calls: .rs.sourceWithProgress ... <Anonymous> -> getDocumentContext -> callFun -> callRemote

Here is my code, contained in some.R script.

thisFilePath <- function() {
  stub <- function() {}
  cmdArgs <- commandArgs(trailingOnly = FALSE)
  if (length(grep("^-f$", cmdArgs)) > 0) {
    # R console option
    normalizePath(cmdArgs[grep("^-f", cmdArgs) + 1])[1]
  } else if (length(grep("^--file=", cmdArgs)) > 0) {
    # Rscript/R console option
    scriptPath <- normalizePath(sub("^--file=", "", cmdArgs[grep("^--file=", cmdArgs)]))[1]
  } else if (Sys.getenv("RSTUDIO") == "1") {
    # RStudio
    rstudioapi::getSourceEditorContext()$path
  } else if (is.null(attr(stub, "srcref")) == FALSE) {
    # 'source'd via R console
    normalizePath(attr(attr(stub, "srcref"), "srcfile")$filename)
  } else {
    stop("Cannot find file path")
  }
}

file.copy(thisFilePath(), getwd(), overwrite = TRUE)

Upvotes: 0

Views: 27

Answers (0)

Related Questions