I'm devoloping an R package and want to run some of the functions in the package under development in the background, using callr::r()
or callr::r_bg()
.
As an example, I created a package mytest with only one function
hello <- function() {
print("Hello, world!")
}
Then loaded the package with pkgload::load_all()
, the function to load a package in development using devtools package. After this, I can run the function in the console but not in the background using callr::r()
.
callr::r(function(){
mytest::hello()
})
#> Error: callr subprocess failed: there is no package called 'mytest'
#> Type .Last.error.trace to see where the error occurred
On the other hand, if I install the package and run library(mytest)
, the code above run without problems
callr::r(function(){
mytest::hello()
})
#> [1] "Hello, world!"
Please, any clue why callr::r()
does not find function mytest::hello()
?
It looks like load_all()
is not adding the path to the folder where the source code of the package mytest can be found.
I found a solution based on an issue in callr GitHub.
After loading the package mytest, that only has the function hello()
in it as defined in the question, with devtools::load_all()
the following code works
z <- list(mytest::hello)
callr::r(function(z){
z[[1]]()
}, args = list(z))
#> [1] "Hello, world!"
It looks like the function hello()
in the package loaded with load_all()
cannot be referred as mytest::hello()
in callr::r()
or in callr::r_bg()
while you can do so if the package was installed and loaded after through library(mytest)
.
Please, is there another solution?
I posted the same question in Stackoverflow with no success. Thanks a lot for you help.