check if a program is installed

前端 未结 3 518
梦谈多话
梦谈多话 2020-12-19 04:03

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

相关标签:
3条回答
  • 2020-12-19 04:10

    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")
    }
    
    0 讨论(0)
  • 2020-12-19 04:17

    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
    
    0 讨论(0)
  • 2020-12-19 04:27

    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.
    0 讨论(0)
提交回复
热议问题