如何在Windows上使用Sys.which查找正确的可执行文件

前端之家收集整理的这篇文章主要介绍了如何在Windows上使用Sys.which查找正确的可执行文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有什么办法在 Windows上使它成为Sys.which找到适当的可执行文件?两个重新解决问题的案例:

> convert.exe这是一个Windows程序和ImageMagik程序,但Sys.which只找到一个从来没有想过R的窗口,无论我怎么在我的PATH上安排东西.
> tar.exe与各种东西一起打包,比如git或mingw等等,即使我在路径中首先使用Rtools和Rbuildtools,也从未找到Rtools的tar程序,例如从源代码安装包时.

所以,每当我在Windows上时,我都会编写一个调用7-zip的包装器.这不可能做的事情可以吗?

编辑

实际上只是将一个环境变量添加到.Renviron:TAR = path / to / tar.exe适用于install.packages示例,我无法记住tar.exe咬我的地方,但Josh回答了主要问题.,convert.exe.

解决方法

我在今年早些时候提出了一个相同的问题 over on R-devel.在回复中,this one由Henrik Bengtsson提供,他们提供了以下有用的功能

Sys.which2 <- function(cmd) {
    stopifnot(length(cmd) == 1)
    if (.Platform$OS.type == "windows") {
        suppressWarnings({
            pathname <- shell(sprintf("where %s 2> NUL",cmd),intern=TRUE)[1]
        })
        if (!is.na(pathname)) return(setNames(pathname,cmd))
    }
    Sys.which(cmd)
}

## Trying out Sys.which & Sys.which2 on my Windows Box gives the following:
Sys.which("convert")
#                              convert 
# "C:\\Windows\\system32\\convert.exe" 
Sys.which2("convert")
#                                                 convert 
# "C:\\Program Files\\ImageMagick-6.8.8-Q16\\convert.exe"

我真的不确定为什么R-core不仅修复Sys.which()以使其实际上是可移植的,但它们至少会在系统中记录这种行为的根本原因(其功能受到同一问题的影响) :

The search path for ‘command’ may be system-dependent: it will include the R ‘bin’ directory,the working directory and the Windows system directories before ‘PATH’.

猜你在找的Windows相关文章