1

I'm trying out Rstudio after using R from the command line for years. I'm liking the debugger and editor. However, I'm struggling with running my typical scripts that use command line args (parsing them using commandArgs). I would expect some ability in Rstudio to hard code some arguments I want passed in when I want to source -- but I can't find that and there must be a different approach. I also was wondering if source had a way to pass in command line arguments but I don't see any such option on the man page.

How do I source a single R script in Rstudio (in the console) passing in command line arguments to be read with commandArgs?

Kulluk007
  • 902
  • 2
  • 10
  • 24
  • I think you may be out of luck. I would use the `Terminal` pane if I were you. Note from `commandArgs()` in the RStudio console that RStudio is not actually running `R` but `RStudio --interactive`. You might be able to do something with your `.Rprofile` in the project directory but I'm not sure exactly what you're trying to accomplish. – MichaelChirico Sep 29 '19 at 12:26
  • I might be barking up the wrong tree here (due to misunderstanding or trying to change your habits), but: that sounds like either (1) a function, or (2) careful use of global variables. While the first is idiomatic and much more palatable to me, the latter can be done with `.myCmdArgs <- list("-q", 1234, pi)` (or whatever args you need), and within the script: `if ("..." %in% commandArgs() || (exists(".myCmdArgs") && "..." %in% .myCmdArgs)) { ... }`. – r2evans Sep 29 '19 at 12:36
  • Ok there doesn't seem to be an option like this to source in Rstudio or R direct. I'm able to use the first answer at https://stackoverflow.com/questions/14525580/how-to-pass-command-line-arguments-when-source-an-r-file via `system` and that works for my purposes. – Kulluk007 Sep 29 '19 at 13:31

0 Answers0