检查是否安装了程序 [英] check if a program is installed

查看:59
本文介绍了检查是否安装了程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个通过命令行在 R 中使用 pandoc 的函数.我如何使用 R 来检查是否安装了 pandoc(我还假设它必须在路径上,这对 Windows 用户来说可能是一个问题)?

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)?

推荐答案

此建议完全基于我对这个问题的个人经验,即 RStudio 似乎无法读取我的 .bashrc 文件中的内容在我的 Ubuntu 系统上.我已经使用 here 描述的 cabal install pandoc 方法安装了 Pandoc,因为那里是我需要从更新版本的 Pandoc 中获得的功能,而不是 Ubuntu 的包管理器.从终端运行 R 可以使用 Sys.which 按预期检测 Pandoc,但是在使用 RStudio 时,它不能.我不知道这是否是 Windows 用户的问题!

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!

在这种情况下,一个替代方案/解决方法实际上是创建一个典型路径的向量,您希望在其中找到 Pandoc 可执行文件(假设许多用户并没有真正关注他们在哪里安装程序).此信息再次在上面链接的安装页面上提供,以及 Windows 的典型 C:\\PROGRA~1\\... 路径.因此,您可能有以下内容作为 Pandoc 的路径:

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

您可以将其与 Sys.which()(例如,Sys.which(myPaths))和一些其他已经共享的想法一起使用.

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

  • 如果第一个选项唯一匹配,则没有问题:您可以直接使用系统调用 Pandoc.
  • 如果任何其他选项唯一匹配,您可以编写函数,将完整路径粘贴到系统调用中的可执行文件中,而不仅仅是pandoc".
  • 如果第一个选项和任何其他选项匹配,那么您只需选择第一个选项并继续.
  • 如果没有匹配,提示用户输入其 Pandoc 安装路径或提供有关如何安装 Pandoc 的消息.

这篇关于检查是否安装了程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆