making commandargs comma delimited or parsing spaces

后端 未结 3 1963
旧时难觅i
旧时难觅i 2020-12-19 07:02

I\'m trying to run R from the command line using command line arguments. This includes passing in some filepaths as arguments for use inside the script. It all works most of

3条回答
  •  天涯浪人
    2020-12-19 07:43

    Using eval(parse()) is probably not the best and most efficient way to parse command line arguments. I recommend to use a package like the optparse to do the parsing for you. Parsing command line args has already been solved, no need to reimplement this. I could imagine that this solves your problems. Although, spaces in path names are a bad idea to begin with.

    Alternatively, you could take a very simple approach and pass the arguments like this:

    R CMD BATCH --slave arg1 arg2
    

    Where you can retrieve them like:

    ca = commandArgs(TRUE)
    arg1 = ca[2]
    arg2 = ca[3]
    

    This avoids the eval(parse which I think is causing the issues. Finally, you could try and escape the space like this:

    R CMD BATCH --slave "C:/spam\ bla"
    

    You could also give Rscript a try, R CMD BATCH seems to be less favored than Rscript.

提交回复
热议问题