10

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.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • Similar question here: http://stackoverflow.com/q/14366406/1270695 – A5C1D2H2I1M1N2O1R2T1 Jun 02 '14 at 16:16
  • And an answer I posted to another similar question: http://stackoverflow.com/a/14971683/1270695 – A5C1D2H2I1M1N2O1R2T1 Jun 02 '14 at 16:22
  • @AnandaMahto Thanks for those cross-refs. I did not see those appear in the 'related answers' area as I composed the question, nor did I find those in my initial search. That's probably due to the frequency of the word 'which' in many contexts is unrelated to what I was looking for. Much appreciated. – Bryan Hanson Jun 02 '14 at 17:42

1 Answers1

12

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 
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455