首页 > 解决方案 > 在 Linux 中查找软件包的二进制文件

问题描述

我正在整理一个包,但是我似乎没有找到让包在系统中找到二进制文件的方法,请注意以下代码:

    find_nim <- function(message=TRUE) {
  nimexe <- ""
  if (.Platform$OS.type == "unix") {
      nimexe <- try(system2("which", "nim", stdout=TRUE))
      if (message) message("Nim found at ", nimexe)
  } else {
    message("Unrecognized operating system.")
  }
#  if (nimexe=="") message("nim executable not found.\n Specify the location of your nim executable.")
  return(nimexe)
}

这将在安装包时返回:

Warning in system2("which", "nim", stdout = TRUE) :
  running command ''which' nim' had status 1
Nim found at 
Error: package or namespace load failed for ‘nimrmarkdown’:
 .onAttach failed in attachNamespace() for 'nimrmarkdown', details:
  call: if (nimexe != "") {
  error: argument is of length zero

相同的代码在 R 中本地工作。

标签: r

解决方案


R 本身有Sys.which()你可以使用:

R> Sys.which("emacs")
           emacs 
"/usr/bin/emacs" 
R> Sys.which("does_not_exist")
does_not_exist 
            "" 
R> 

编写 R 扩展手册也提到了它:

 Usage of external commands should always be conditional on a test
 for existence (perhaps using 'Sys.which'), as well as declared in
 the 'SystemRequirements' field.

 example in some locales.  (Use e.g. 'capabilities()' or
 'nzchar(Sys.which("someprogram"))' to test for features needed in
 the examples wherever possible, and you can also use 'try()' or

推荐阅读