Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to specify command line parameters to R-script in RStudio?

Tags:

r

rstudio

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?

like image 300
Dmitry K. Avatar asked Feb 20 '13 12:02

Dmitry K.


People also ask

Should I modify my script every time I run it in RStudio?

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:

How do I pass arguments to run an R script file?

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.

How do I run an R script from the command line?

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.

How to get started with the shell in RStudio?

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.


2 Answers

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
like image 130
biocyberman Avatar answered Sep 28 '22 04:09

biocyberman


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

like image 28
rrs Avatar answered Sep 28 '22 04:09

rrs