简体   繁体   中英

Is there a way to debug an RScript call in RStudio?

Imagine I run an R script from the command line, like this:

Rscript prog.R x y z

and I want to examine the code at a certain line.

Presently, I can't just debug it interactively within RStudio because I don't know how to pass in the arguments.

As it is designed to be run from the command line, how can I debug the script via the command line / outside of RStudio?

This solution may solve your problem: Is there a way to debug an R script run from the command line with Rscript.exe - Answer

ipdb.r <- function(){
  input <- ""
  while(!input %in% c("c","cont","continue"))
  {
    cat("ipdb.r>")
    # stdin connection to work outside interactive session
    input <- readLines("stdin",n=1)
    if(!input %in% c("c","cont","continue","exit"))
    {
      # parent.frame() runs command outside function environment
      print(eval(parse(text=input),parent.frame()))
    }else if(input=="exit")
    {
      stop("Exiting from ipdb.r...")
    }
  }
}

If your only problem is passing command line arguments to a script in Rstudio, you can use the following trick.

Your script probably accesses the command line arguments with something like:

args <- commandArgs(trailingOnly = TRUE)

After which args contains a character vector with all the trailing command arguments, which in your example would be c("x", "y", "z") .

The trick to make your code run in Rstudio without any changes is simply to define a local function commandArgs that simulates the arguments you want. Here I also take care of the case where trailingOnly = TRUE although it's rarely used in practice.

commandArgs <- function(trailingOnly = FALSE) {
    if (trailingOnly) {
        c("x", "y", "z")
    } else {
        c("Rscript", "--file=prog.R", "--args", "x", "y", "z")
    }
}

Once commandArgs is defined in the global environment, you can run your script in Rstudio and it will see "x", "y", "z" as its arguments.

You can even go one step further and streamline the process a bit:

#' Source a file with simulated command line arguments
#' @param file Path to R file
#' @param args Character vector of arguments
source_with_args <- function(file, args = character(0)) {
  commandArgs <- function(trailingOnly = FALSE) {
    if (trailingOnly) {
      args
    } else if (length(args)) {
      c("Rscript", paste0("--file=", file), "--args", args)
    } else {
      c("Rscript", paste0("--file=", file))
    }
  }
  # Note `local = TRUE` to make sure the script sees
  # the local `commandArgs` function defined just above.
  source(file, local = TRUE)
}

Let's imagine you want to run the following script:

# This is the content of test.R
# It simply prints the arguments it received.

args <- commandArgs(trailingOnly = TRUE)
cat("Received ", length(args), "arguments:\n")
for (a in args) {
  cat("- ", a, "\n")
}

This is the result with Rscript:

$ Rscript --vanilla path/to/test.R x y z
Received  3 arguments:
-  x 
-  y 
-  z 

And you can run the following in R or Rstudio:

source_with_args("path/to/test.R")
#> Received  0 arguments:

source_with_args("path/to/test.R", "x")
#> Received  1 arguments:
#> -  x

source_with_args("path/to/test.R", c("x", "y", "z"))
#> Received  3 arguments:
#> -  x 
#> -  y 
#> -  z

For hacking commandArgs(), it's also possible to just overwrite the base::commandArgs() with your own version instead of masking it, since the base environment comes unlocked.

For the part of your question on debugging, you can perform traceback() as usual with any source() d scripts. Also just want to refer to this function dump.frames() . Through this function you can dump the stracktrace to a file at error and look at it later.

This is what I do- it's not a formal debugging but it works for me.

top of example prog.R script:

# uncomment this section to run using Rscript from command line:
userprefs <- commandArgs(trailingOnly = TRUE)
x <- userprefs[1] 
y <- userprefs[2]
z <- userprefs[3]

# uncomment this section to run within RStudio
cat("you forgot to comment out the troubleshooting part!")
x <- 1
y <- 2
z <- 3

As you troubleshoot your script, comment out one or the other section depending on whether you're sourcing within RStudio or with RScript from the command line.

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