Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

system.file() goes well, but base::system.file() not

Tags:

r

Now, I develop some R package, to avoid double names between different packages, I want to use base::system.file(),

But once I run base::system.file() in my package, named "aa", the first time is well, but the second running, it return "", that is empty.

By eliminating base, that is system.file() goes well. Please let me know why return value is "" (empty).

 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"
like image 629
Jean Billie Avatar asked Nov 20 '25 08:11

Jean Billie


1 Answers

You can use ?? to see which packages implement the function, devtools overwrites the system.file so it could be that system.file is different function than base:system.file

??system.file 

also if you type just the function it should display the package of the function

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

it seems that system.file is running devtools:system.file which is different that base::system.file

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

One is returning the path relative to the package

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.

like image 157
Carlos Santillan Avatar answered Nov 21 '25 22:11

Carlos Santillan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!