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

前端 未结 6 1978
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 23:10

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

6条回答
  •  感情败类
    2020-12-08 23:53

    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.

提交回复
热议问题