VSCode-R-Debugger icon indicating copy to clipboard operation
VSCode-R-Debugger copied to clipboard

[Feature Request/Question] command line args to R script file not R itself

Open TTTPOB opened this issue 2 years ago • 6 comments

maybe related #114 I cannot find where I can add command line arguments to the R script file itself rather than R itself, to debug a script file using argparse, this feature is needed

TTTPOB avatar Oct 28 '21 13:10 TTTPOB

Can you please give an example how the corresponding R/Rscript call would look like?

ManuelHentschel avatar Oct 31 '21 13:10 ManuelHentschel

Can you please give an example how the corresponding R/Rscript call would look like?

sorry for the late reply

it will looks like a python argparse script Rscript path/to/the/script --step 1 --dist 100 inside the script

library(argparse)
parser<-ArgumentParser(description="demo")
attach(parser)
add_argument("--step", "-s", help="step of the counter")
add_argument("--dist", "-d", help="dist of the counter")
detatch(parser)
args<-parser$parse_args()

cat(
  "the dist is: ",
  dist,
  "\n",
  "the step is: ",
  step,
  "\n"
)

as you see, the arguments lies after the script, not the R/Rscript

TTTPOB avatar Nov 04 '21 02:11 TTTPOB

I need this too!
Also I'd love to see the option to set Rscript args (like --vanilla, because my .Rprofile may mess with the debugging or the script).

tddschn avatar Apr 26 '23 02:04 tddschn

Yes IMO this is required for developing any non-trivial real-world script that needs to be configurable at runtime

kevinpauli avatar May 03 '23 15:05 kevinpauli

One can make this work by setting the --args flag, followed by whatever command line arguments one wants to supply to the script. One can then use commandArgs(trailingOnly = TRUE) to obtain the passed CL arguments. To do this, add a commandLineArgs entry into the launch.json.

Caveat is that 'proper' command line parsing with e.g. argparse isn't supporte (or at least I don't expect it to work)

image

image

karchern avatar Nov 29 '23 11:11 karchern

argparse isn't supporte (or at least I don't expect it to work)

It actually does work with argparse!

library(argparse)
parser <- ArgumentParser()
parser$add_argument("-n", "--name", help = "Whom shall I greet?", default = "World")
args <- parser$parse_args(commandArgs(trailingOnly = TRUE))
cat(paste0("Hello, ", args$name, "!\n"))
$ Rscript hello_name.R --name Kevin
Hello, Kevin!

Cursor_and_launch_json_—corn-plhtn__Dev_Container__corn-plhtn Cursor_and_hello_name_R_—corn-plhtn__Dev_Container__corn-plhtn

kevinpauli avatar Dec 04 '23 16:12 kevinpauli