Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Platform neutral way to check if a program exists (e.g. pdfcrop) while creating vignette

Tags:

package

r

I have a vignette which uses pdfcrop.

This is not going to be available on every computer or OS: The R maintainers have told me that pdfcrop is on their Debian systems, but apparently not others, for example. So I'd like to include in the vignette some logic to figure out if this program is available and use it if possible (I'm using knitr to build the vignette and hence knit_hooks$set(crop = hook_pdfcrop) to activate it).

I know I can use .Platform to get the OS, and if I'm on unix then I can use which pdfcrop via system() to tell me where/if the program is installed, but I don't know how to make this process general for OSX, linux, Windows etc, and I'm not sure how to properly grab the return value of which or the corresponding commands for other platforms.

Put another way, I'm trying to do something like this question but I'm not checking for R packages, I'm checking for non-R programs. I'm turning to SO since I have neither the knowledge nor the platforms to check it on.

like image 389
Bryan Hanson Avatar asked Jun 02 '14 13:06

Bryan Hanson


1 Answers

R has a function Sys.which() for just this purpose. It is, in the words of its help page, "an interface to the system command ‘which’, or to an emulation on Windows".

Here's what a call to it looks like on my own Windows machine:

Sys.which("pdfcrop")
#                                              pdfcrop 
# "C:\\PROGRA~2\\MIKTEX~1.9\\miktex\\bin\\pdfcrop.exe" 

To test whether an executable exists, you could do something like this:

Sys.which("pdfcrop") != ""
# pdfcrop 
#    TRUE 

Sys.which("pdfpop") != ""
# pdfpop 
#  FALSE 
like image 125
Josh O'Brien Avatar answered Oct 04 '22 14:10

Josh O'Brien