Is it possible to specify command line parameters

2020-02-22 02:08发布

问题:

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?

回答1:

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



回答2:

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


回答3:

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


回答4:

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"


回答5:

If you're interested in using e.g. argparser and continue developing/analyzing interactively using Rstudio, you can use the following work-around:

  1. Write your command line parser in my_rscript and create an object args that contains all parsed input.
  2. Add a line that saves args object to file.
  3. Run my_rscript from command line and specify arguments of interest.
  4. Load the args object from file in Rstudio and continue interactively

Example:

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


回答6:

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.



标签: r rstudio