0

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.

1 Answer 1

1

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

Another option may be to install the new package in the new process started by {callr}:

callr::r(function(){
  devtools::install("./")
  mytest::hello()
})
#> [1] "Hello, world!"

Please, is there another solution that can be used to iterate the new package development using load_all() and use those package functions in a new R session open by {callr}?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.