tuomastik
tuomastik

Reputation: 4916

How to avoid parsing command line arguments when building R package?

I'm developing an R package that includes a command line interface. When building the package, I would like to avoid parsing the command line arguments (build flags) since my command line argument parser doesn't recognize these build-related arguments and produces an error.

To overcome this issue, I'm currently using an approach where my build flags are hard coded to an if statement before trying to parse the arguments:

if (length(commandArgs(trailingOnly = TRUE)) > 0 &&
        !(grepl("--no-multiarch", commandArgs(trailingOnly = TRUE)))) {
    argv <- GetCmdlineArguments()
    DoStuff(argv$parsed.argument)
}

Another approach which I haven't experimented with yet includes putting the argument parsing in a different R file which is ignored by the build via .Rbuildignore. This would, however, lead to an unfavorable situation where an extra file is needed for each R file that has a command line interface.

Is there more elegant and robust way of detecting if the package is being built instead of actually executed from the command line by the user?

Upvotes: 1

Views: 227

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368389

I do not fully understand what you are trying to achieve, but allow me to provide some context:

  • first off, all R use is generally from the R prompt, not the command-line
  • as such, a package will only every contain R functions etc
  • that said, command-line work is very powerful and popular
  • the R ecosystem has both Rscript and r from littler both of which facilitate command-line use
  • typically, we ship scripts for use these frontends in inst/scripts/ or inst/examples
  • all those scripts can use one of the many packages to parse command-line options -- I favour docopt
  • consequently, my littler package has lots of examples using docopt

Could you not just do the same?

Upvotes: 1

Related Questions