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 ma
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")
}
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
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.