如何在 R 中检查包时修复 "Unable to find GhostScript executable to run checks on size reduction" 错误?

How to fix "Unable to find GhostScript executable to run checks on size reduction" error upon package check in R?

在 Revolution R Enterprise 控制台中,

devtools::check("C:/Users/User/Documents/Revolution/mypackage")

已制作

checking sizes of PDF files under 'inst/doc' ... NOTE
Unable to find GhostScript executable to run checks on size reduction

没有任何其他 warnings/errors/notes。所以,(尽管 AFAIK 这个注释对于最终检查并不是那么重要),我想摆脱这个警告(因为我想把 .PDF 文件放入 mypackage\inst\doc 在 R 之外生成的文件夹中)。

我的笔记本上安装了 Ghostscript。我通过以下方式获得帮助:

> help("R_GSCMD")
R_GSCMD: Optional. The path to Ghostscript, used by dev2bitmap, bitmap and embedFonts. 
Consulted when those functions are invoked. 
Since it will be treated as if passed to system, spaces and shell metacharacters should be escaped.


> Sys.getenv("R_GSCMD")
[1] ""

我所做的(并再次出错)是:

> Sys.setenv("R_GSCMD") <- "C:\Program Files (x86)\gs\gs9.19\bin\gswin32c.exe"
Error in Sys.setenv("R_GSCMD") <- "C:\Program Files (x86)\gs\gs9.19\bin\gswin32c.exe" : 
  target of assignment expands to non-language object

深入后发现:["These errors occur when one tries to assign a value to a variable that doesn't exist, or that R can't treat as a name. (A name is a variable type that holds a variable name."]

我基本上想做的是将我的 GS 可执行文件 (C:\Program Files (x86)\gs\gs9.19\bin\gswin32c.exe) 设置为 "R_GSCMD"。 任何帮助将不胜感激。

咨询 ?Sys.setenv 它证实了我的期望,即调用应该是:

Sys.setenv(R_GSCMD = "C:\Program Files (x86)\gs\gs9.19\bin\gswin32c.exe")

因为 gs 版本一直在变化,您可能会喜欢一些 R 脚本!

system.partition = 'c:'
dirs = c('Program Files', 'Program Files (x86)')
for (dir in dirs) {
    dir.list = list.dirs(file.path(system.partition, dir), recursive = FALSE)
    GsinList = grepl(pattern = 'gs', x = dir.list)
    if (sum(GsinList) > 0) {
        gsDirectory = which(GsinList == TRUE)
        GsExeFiles = list.files(
            dir.list[gsDirectory],
            recursive = TRUE,
            pattern = 'gswin',
            include.dirs = TRUE,
            full.names = TRUE
        )[1]
        message('Gs found! ~> ',GsExeFiles)
        Sys.setenv(R_GSCMD = GsExeFiles)
        break
    }
}


Gs found! ~> c:/Program Files/gs/gs9.21/bin/gswin64.exe