Reload R package with compiled extensions
Is it possible to reload an R package with native extensions in an R session?
I am developing Rliblinear, which uses several C
functions.
If I make a change to R code in the package, I can just reinstall and reload;
$ R CMD build R开发者_运维技巧liblinear
$ R CMD INSTALL Rliblinear
and then in an R
shell;
> detach("package:Rliblinear", unload=TRUE)
> library(Rliblinear)
However, the C
functions are not affected unless I restart the R
interpreter.
Is there a way I can force the interpreter to reload the shared object, Rliblinear.so
?
This will list your loaded dynamic link libraries:
library.dynam()
and this will unload Rliblinear.*
in the Rliblinear package.
library(Rliblinear)
# ... run package ...
detach("package:Rliblinear", unload = TRUE)
library.dynam.unload("Rliblinear", system.file(package = "Rliblinear"))
You can issue library.dynam()
again just to check that its no longer listed.
I tend to do my tests on the command-line with littler to be sure I get a fresh R session. You can do this with Rscript
too.
So my work flow would be
$ R CMD INSTALL Rliblinear/ ## alternatively, install from tarball
$ r -lRliblinear -e'someExpressionFromThePackage()'
which you can also wrap into a single line with &&
and/or precede with a clean step.
here is an extension to Mr G. Grothendieck answer, if embedded in "yourpackage" one can recompile it with one shot from R console:
reload <- function( path ){
detach("package:yourpackage", unload = TRUE)
library.dynam.unload("yourpackage", system.file(package = "yourpackage"))
path <- paste( "--vanilla CMD INSTALL ", path )
system2( 'R', path )
require("yourpackage")
}
then from R:
yourpackage::reload( '/home/me/yourpackage' )
精彩评论