Writing R package functions to create an API to command-line tools

Sys.which("toolname") should do what you want, even on a Windows system (which uses where toolname on the command line).

In my opinion, it's better to use existing solutions for user-specific values:

  • Expect the command toolname ... to just work; if it doesn't, print an informative error message suggesting the user update their PATH.

  • Possibly offering another way with a package option. So, if your package is named toolnamer, let the user provide a path to the executable with the toolnamer.exe option. Then one of your function parameters could be:

    exe_path = getOption("toolnamer.exe", default = "toolname")
    

    This lets them control it with an .Rprofile script. If they don't set the option, then it falls back to looking for toolname along the PATH.

3 Likes