r - system.file() 运行良好,但 base::system.file() 不行

标签 r

现在,我开发了一些R包,以避免不同包之间的重名, 我想使用 base::system.file(),

但是一旦我在名为 "aa" 的包中运行 base::system.file(), 第一次还好, 但第二次运行时,它返回 "",即空。

通过消除base,即system.file(),效果会很好。请告诉我为什么返回值为 ""(空)。

 base::system.file("extdata", "aaaaa.stan", package="aa")
[1] ""
> system.file("extdata", "aaaaa.stan", package="aa")
[1] "C:/Users/xxxxx/Desktop/aa/inst/extdata/aaaaa.stan"

最佳答案

你可以使用??查看哪些包实现了该功能, devtools 会覆盖 system.file,因此 system.file 的功能可能与 base:system.file 不同

??system.file 

如果您只键入函数,它应该显示函数的包

> base::system.file
function (..., package = "base", lib.loc = NULL, mustWork = FALSE) 
{
    if (nargs() == 0L) 
        return(file.path(.Library, "base"))
    if (length(package) != 1L) 
        stop("'package' must be of length 1")
    packagePath <- find.package(package, lib.loc, quiet = TRUE)
    ans <- if (length(packagePath)) {
        FILES <- file.path(packagePath, ...)
        present <- file.exists(FILES)
        if (any(present)) 
            FILES[present]
        else ""
    }
    else ""
    if (mustWork && identical(ans, "")) 
        stop("no file found")
    ans
}
<bytecode: 0x2342e00>
<environment: namespace:base>

似乎 system.file 正在运行 devtools:system.file ,它与 base::system.file 不同

https://www.rdocumentation.org/packages/devtools/versions/1.13.6/topics/system.file

一个是返回相对于包的路径

When system.file is called from the R console (the global envrironment), this function detects if the target package was loaded with load_all, and if so, it uses a customized method of searching for the file. This is necessary because the directory structure of a source package is different from the directory structure of an installed package.

关于r - system.file() 运行良好,但 base::system.file() 不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52415821/

相关文章:

评估两个(纬度、经度)点之间的视线 (LOS) 的 R 代码

r - 从 R 控制台获取密码

r - 如何将分钟数据转换为小时平均数据

r - 按位置 [R] 从另一个向量填充向量上的空元素

r - 在 r 中格式化绘图轴

r - 在 data.table 中使用 eval

r - 在 R 中计算蚕食/归因

r - 水平 ggplot - 如何在条内定位数据标签

r - 在 ggplot 中创建正态分布上的单色点

r - 传单/ Shiny : cannot draw reactive polygons