[英]Executing R scripts during package installation
Hopefully this has a straightforward answer, but I haven't been able to find it as yet.希望这有一个简单的答案,但我还没有找到它。
I'm writing an R package, and when installed on Windows I want it to execute a script that searches for a system file ie list.files(path = "C:/Program Files/, ...)
and then saves that path to the package directory as a text file for later reference.我正在编写一个 R 包,当安装在 Windows 上时,我希望它执行一个搜索系统文件的脚本,即list.files(path = "C:/Program Files/, ...)
然后保存该路径到包目录作为文本文件供以后参考。
I tried saving the script as src/install.libs.R
but that stopped my package from building.我尝试将脚本保存为src/install.libs.R
但这阻止了我的包的构建。
In case there is an alternative solution, I'm trying to save the path to the javaw.exe file that resides in the Program files directory (somewhere!), so that I can quickly call it in functions via system2()
.如果有替代解决方案,我会尝试保存位于 Program files 目录(某处!)中的 javaw.exe 文件的路径,以便我可以通过system2()
在函数中快速调用它。
There is no hook in R for this: executing code during installation. R 中没有钩子:在安装期间执行代码。
There is, however, an entire set of hooks for package load or attachment.然而,有一整套用于装载或连接包裹的挂钩。 I often use .onLoad()
for this. .onLoad()
我经常使用.onLoad()
。 See eg how RcppGSL remembers what linker and compiler flag to use -- from R/inline.R
:见例如,如何RcppGSL会记住链接器和编译标志使用-从R/inline.R
:
.pkgglobalenv <- new.env(parent=emptyenv())
.onLoad <- function(libname, pkgname) {
if (.Platform$OS.type=="windows") {
LIB_GSL <- Sys.getenv("LIB_GSL")
gsl_cflags <- sprintf( "-I%s/include", LIB_GSL )
gsl_libs <- sprintf( "-L%s/lib -lgsl -lgslcblas", LIB_GSL )
} else {
gsl_cflags <- system( "gsl-config --cflags" , intern = TRUE )
gsl_libs <- system( "gsl-config --libs" , intern = TRUE )
}
assign("gsl_cflags", gsl_cflags, envir=.pkgglobalenv)
assign("gsl_libs", gsl_libs, envir=.pkgglobalenv)
}
Next in this file are how to use them:此文件中的下一个是如何使用它们:
LdFlags <- function(print = TRUE) {
if (print) cat(.pkgglobalenv$gsl_libs) else .pkgglobalenv$gsl_libs
}
CFlags <- function(print = TRUE) {
if (print) cat(.pkgglobalenv$gsl_cflags) else .pkgglobalenv$gsl_cflags
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.