I want to use RStudio to edit an R-script having command line parameters, e.g.,
my_rscript --dataset mydataset
and then to read the optiion value into an R variable, say, dataset
, e.g., using optparse
library.
However, I could not find where acommand line can be provided in RStudio, so that I could use "Source on save" feature. Instead, I have to hardcode all program parameters in the program itself:
dataset <- "mydataset"
which requires modifying the script text each time I need to specify different data.
Does anybody know how to provide a command line information?
So if your main concern is not wanting to modify your script every time you run it in Rstudio, you can set up a series of these commands to input your arguments every time, like so:
When using the command line to run an R script file, we may want to pass arguments and save the output in a file. This article demonstrates these techniques using the R CMD BATCH command and the Rscript front-end.
When using the command line to run an R script file, we may want to pass arguments and save the output in a file. This article demonstrates these techniques using the R CMD BATCH command and the Rscript front-end. We will start with some details common to both methods.
It is sufficient to know a handful of commands to get started with the shell. Although we will use the terminal in RStudio on RStudio Cloud, we should still know how to launch the terminal in different operating systems. Go to the Start Menu or screen and enter Command Prompt in the search field.
For now I do it this way:
Open a new window of editing new Rscript. If I want to keep it I can save and name it something like: test_myscript.R
This is the content of test_myscript.R
:
debug(getopt) # suppose I want to debug 'getopt' function in 'myscript.R'
system("myscript.R -a -b -c")
# Debug process start after this.
# Check ?browser for help about navigating inside browser
I know this question is old and the link below is old, but it answers the question. No, it's not possible (or wasn't as of Jan 29, 2012) to access command line arguments from RStudio.
Link https://support.rstudio.com/hc/communities/public/questions/200659066-Accessing-command-line-options-in-RStudio?locale=en-us
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With