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
This worked for me: My Rscript is as follows:
args <- commandArgs()
print(paste0("args:", args))
print(paste0("args[1]:",args[1]))
print(paste0("args[2]:",args[2]))
print(paste0("args[3]:",args[3]))
print(paste0("# of args:",length(args)))
'
To emulate the command line input I would use with Rscript, I entered this in RStudio:
commandArgs <- function() c("AMZN", 10, 200)
which gave the desired result:
[1] "args:AMZN" "args:10" "args:200"
[1] "args[1]:AMZN"
[1] "args[2]:10"
[1] "args[3]:200"
[1] "# of args:3"
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
You can call your programs using Rscript programname.r arg1 arg2 arg3
. The arguments are passed to commandArgs
, so the following would be true:
Rscript programname.r F N 32
> args <- commandArgs(trailingOnly=TRUE)
> args[1]
[1] F
> args[2]
[1] N
> args[3]
[1] 32
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
If you're interested in using e.g. argparser and continue developing/analyzing interactively using Rstudio, you can use the following work-around:
my_rscript
and create an object args
that contains all parsed input.args
object to file. my_rscript
from command line and specify arguments of interest. args
object from file in Rstudio and continue interactivelyExample:
library("argparser")
parser <- arg_parser(description='Process commandline arguments')
parser <- add_argument(parser, arg="--dataset", type="character", help = "Dataset argument")
args = parse_args(parser)
args_file = "tempArgObjectFile.rds"
saveRDS(args, args_file); print(args); quit(); #comment this after creating args_file
args = readRDS(args_file) #use this to load during interactive development
This is really old but I stumbled across it when trying to do the same thing and I've ended up just trying the following, and is nice and quick if people want to try it (probably only useful for commands that have a couple of easy args though):
Given my Rscript which currently starts:
args <- commandArgs(TRUE)
df <- read.csv(args[1], sep=",", check.names=FALSE, row.names = 1)
.
. # Do some analysis and plotting etc.
.
If I wanted to emulate the command line that the Rscript would otherwise receive, you can make the args
vector up 'manually':
args <- c("/path/to/my/inputfile.csv")
then args[1]
takes on the same value it always would have. and I simply run everything else in the script by highlighting and executing in RStudio.