best practice to check if an executable is installed


I'm developing a package for a research project. It uses an external executable. I don't want to ship it inside this package but I want to check if it is installed (present in the system PATH, maybe using Sys.which) or if the user set the path to the executable with options.

I don't know what's the best to do in this case, should I check once with .onAttach, when loading the main functions. Should I store the path in options or in an environment.

Do you know any good practice for that or god examples

1 Like

@JeanM I agree that using Sys.which() is the best option for checking if the executable is installed. From ?system:

There are many pitfalls in using system to ascertain if a command can be run — Sys.which is more suitable.

As an example, I explored the rmarkdown package, which relies on pandoc. It uses the environment option. Here's how the rmarkdown package solves this problem:

  1. It creates an environment .pandoc to store the location and version of pandoc - pandoc.R#L705
  2. Each time the user executes render() to build a document, the function pandoc_available() is called, which in turn calls find_pandoc()
  3. find_pandoc() first checks the internal package environment. If this is the first time that render() has been run, it calls find_program("pandoc")
  4. find_program() searches for pandoc using Sys.which(): util.R#L286
  5. Finally, if pandoc is found, then the environment is updated so that it doesn't need to be searched for again (until the R session is restarted) - pandoc.R#L575

yes, great example, I will look at rmarkdown. Thank you very much

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.