8

I'm writing a function that uses pandoc in R through the command line. How can I use R to check if pandoc installed (I also assume it would have to be on the path which may be an issue for windows users)?

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • 1
    Testing both solutions on windows computers that had pandoc and didn't have pandoc... it looks like both work. I don't know which solution is more robust though. – Dason Feb 19 '13 at 18:31
  • 4
    One advantage I can see of the `system('pandoc - v')` approach over `Sys.which` is that it verifies that the program is not only installed, but can actually be run. – Brian Diggs Feb 19 '13 at 19:29
  • @BrianDiggs brilliant !! – agstudy Feb 20 '13 at 00:16

3 Answers3

13

I don't have pandoc to install , but generally I test if a program is installed like this :

pandoc.installed <- system('pandoc -v')==0

For example to test if java is installed:

 java.installed <- system('java -version') ==0

java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
> java.installed
[1] TRUE
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 1
    you solution was exactly what I asked for thank you. Ananda figured out what I actually needed when i didn't know I needed it. – Tyler Rinker Feb 20 '13 at 02:59
5

I suppose you could use Sys.which and see if the result is an empty string.

pandoc.location <- Sys.which("pandoc")
if(pandoc.location == ""){
    print("pandoc not available")
}else{
    print("pandoc available")
}
Dason
  • 60,663
  • 9
  • 131
  • 148
  • 1
    This works on my Windows box but `Sys.which("firefox")` returns `""`, but `system("open firefox")` launches a Firefox browser... I wonder why. – Josh O'Brien Feb 19 '13 at 18:23
  • Would have been my suggestion too (I also suggested it to Tyler for Ghostscript [here](http://stackoverflow.com/a/14376517/1270695)), but it doesn't work on my system for some reason. Perhaps because it was a cabal install? But not sure... – A5C1D2H2I1M1N2O1R2T1 Feb 19 '13 at 18:24
  • Good point. I haven't really tested this on windows. I'm wondering if pandoc might be a little different though since it's mainly a command line utility – Dason Feb 19 '13 at 18:24
  • Alright I just gave this a try on my windows box that has pandoc installed and it worked – Dason Feb 19 '13 at 18:30
  • @Dason, on my Ubuntu system, I can get the path if I run R from the terminal, but not if I run it from RStudio--seems like RStudio isn't seeing what's in my `.bashrc` file. For that matter, `system("pandoc -v")` doesn't work from within RStudio with my current setup either. Hmm... – A5C1D2H2I1M1N2O1R2T1 Feb 19 '13 at 18:35
  • @AnandaMahto Which version of Ubuntu are you using? I'm on 12.04 and it's working from RStudio for me. Which version of RStudio? – Dason Feb 19 '13 at 18:37
  • Ubuntu 12.04, RStudio 0.97.307, and pandoc a little out of date at 1.9.4.5 – A5C1D2H2I1M1N2O1R2T1 Feb 19 '13 at 18:41
  • I just checked and yeah it looks like RStudio isn't reading .bashrc. I'm using the really old version of pandoc from the repositories so it's in /usr/bin which RStudio is happy with. Maybe we should let RStudio know about this difference... – Dason Feb 19 '13 at 18:50
  • @AnandaMahto I found a solution/workaround to our shared problem [here](http://stackoverflow.com/a/14971110/1003565) – Dason Feb 20 '13 at 02:05
  • @Dason your solution works but requires the user to mess with stuff (which they may want to do) but Ananda's solution doesn't require the user to alter anything. See: https://github.com/trinker/reports/blob/master/R/html5.R – Tyler Rinker Feb 20 '13 at 03:01
  • 1
    @TylerRinker I know - and I mention that in the comments to my solution over there. My solution is nice but for deployment for a general 'user' Ananda's solution is preferable. But just to solve the issue that arises of RStudio not recognizing the path to pandoc... my solution works well. – Dason Feb 20 '13 at 03:06
5

This suggestion is based entirely on my personal experience with this question that RStudio can't seem to read what's in my .bashrc file on my Ubuntu system. I have installed Pandoc using the cabal install pandoc method described here since there were features I needed from more recent versions of Pandoc than were available with Ubuntu's package manager. Running R from the terminal could detect Pandoc as expected using Sys.which, but when using RStudio, it could not. I have no idea if this is a problem with Windows users or not though!

One alternative/workaround in this case is actually creating a vector of typical paths where you expect the Pandoc executable to be found (under the presumption that many users don't really futz around with where they install programs). This info is, again, available at the install page linked above, plus the typical C:\\PROGRA~1\\... path for Windows. Thus, you might have something like the following as the paths to Pandoc:

myPaths <- c("pandoc", 
             "~/.cabal/bin/pandoc", 
             "~/Library/Haskell/bin/pandoc", 
             "C:\\PROGRA~1\\Pandoc\\bin\\pandoc") 
             # Maybe a .exe is required for that last one?
             # Don't think so, but not a regular Windows user

Which you can use with Sys.which() (for example, Sys.which(myPaths)) and some of the other ideas already shared.

  • If the first option is uniquely matched, then there's no problem: you can use system calls to Pandoc directly.
  • If any of the other options are uniquely matched, you can write your functions in such a way that you paste in the full path to the executable in your system call instead of just "pandoc".
  • If the first option and any of the other options are matched, then you can just select the first option and proceed.
  • If none are matched, prompt the user for the path to their Pandoc installation or provide a message on how to install Pandoc.
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • 1
    this approach works and doesn't require the user to reconfigure things. Worked both on Linux and Windows +1 You didn't give me what I asked for you figured out what I needed. – Tyler Rinker Feb 20 '13 at 02:58
  • @agstudy, you're welcome. Both your and Dason's answers were actually the first that came to mind (I had made the same suggestions to a similar question [here](http://stackoverflow.com/a/14376517/1270695)) but when I tried it out before posting my answer, it didn't work, hence my need to dig a little further ;) – A5C1D2H2I1M1N2O1R2T1 Feb 20 '13 at 03:30