ManuelHentschel / VSCode-R-Debugger

R Debugger Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=RDebugger.r-debugger
MIT License
168 stars 11 forks source link

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

Open TTTPOB opened 3 years ago

TTTPOB commented 3 years ago

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

ManuelHentschel commented 3 years ago

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

TTTPOB commented 3 years ago

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

tddschn commented 1 year ago

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).

kevinpauli commented 1 year ago

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

karchern commented 11 months ago

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

kevinpauli commented 11 months ago

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_