I am writing the R package and trying to use external files. I placed it in inst/extdata
and use system.file("extdata", "file.csv", package = "mypackage")
to load the file in my function. The official manuals describe only this way to get data from inst/extdata
.
But during building I got the error
ERROR: hard-coded installation path: please report to the package maintainer and use '--no-staged-install'
Forums said that system.file()
is bad practice, but how I should use row data in my package?
This problem is occurred after updates in 2018. I found that I can use StagedInstall: no
in DESCRIPTION file, but this is cheating, isn't it?
I want to use raw files inside functions (as precalculated static tables) and in examples as input files. My R version is 3.6.2.
The error occurs because the package source code is executed at installation time, not when the package is loaded. Furthermore, starting with R 3.6, packages are installed inside a temporary path, not at their actual, final installation location.
As a consequence, system.file
will return a bogus path when called directly inside a package at file scope (i.e. not inside a function). This is what the error message you’re getting is trying to convey.
Once you know this, the solution is rather straightforward: don’t call system.file
during package building. Instead, call it during package loading; that is, inside .onLoad
:
.onLoad = function (libname, pkgname) {
ns = topenv()
ns$datafile = system.file("extdata", "file.csv", package = "mypackage")
}
(More details can be found in a related answer.)
This causes the variable datafile
to be created inside your package namespace, and you can now access it from elsewhere.
Forums said that
system.file()
is bad practice
No, using system.file
is definitely not bad practice; on the contrary, it is required to access your package extdata. What’s bad practice is calling the function at file scope. But calling it inside a function is fine.
I found that I can use
StagedInstall: no
in DESCRIPTION file, but this is cheating, isn't it?
Indeed, that’s “cheating” and definitely not recommended as a proper solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With