check if a program is installed

只谈情不闲聊 提交于 2019-11-28 01:04:51

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.

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

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")
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!