R and R Studio: How to debug R command line scripts with R Studio: mock commandArgs and use debugSource()
Pontifications
- Put the main function in a function e.g.
script_main()
- Invoke
debug()
on that function i.e.debug(script_main)
. You can see this in: plot-antivirus-mentions-by-3weeks.r - Create a test script that mocks up
commandArgs
which is the built-in command line parsing function anddebugSource
s the file - e.g. testRScript.R
commandArgs <- function(...) return(c("FF61", "2018", "6", "26", "FF62", "2018", "9", "5", "no"))
debugSource("plot-antivirus-mentions-by-3weeks.r")
rm(commandArgs)
-
and then source
testRScript.R
in R Studio to debug with all the great R Studio debug utilities -
Important note when you run it using RScript,
debug()
is ignored! Here’s how to invoke it with the command line again:
Rscript ./plot-antivirus-mentions-by-3weeks.r FF61 2018 6 26 FF62 2018 9 5 yes