Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for custom user functions #191

Closed
mlascaleia opened this issue Mar 23, 2021 · 2 comments
Closed

Support for custom user functions #191

mlascaleia opened this issue Mar 23, 2021 · 2 comments

Comments

@mlascaleia
Copy link

@mlascaleia mlascaleia commented Mar 23, 2021

Hi,

I am sorry if this is a basic question, but I am wondering if callr is able to support custom user functions that are defined in the global environment? I understand that the use of "::" is essential for functions from other packages, but custom user functions exist outside of any package. Here is an example of what I would like to be able to do:

x <- 3
userFunction <- function(x){
  y <- (2 * x + 7)
  return(y)
}

z <- r_bg(function(a) userFunction(a), args = list(x))

This currently returns the error:

Error: <callr_status_error: callr subprocess failed: could not find function "userFunction">
-->
<callr_remote_error in userFunction(a):
 could not find function "userFunction">
 in process 20466 
See `.Last.error.trace` for a stack trace.

Where the code below works just fine

x <- 3
z <- r_bg(function(a) sum(a), args = list(x))

Thank you!

@gaborcsardi
Copy link
Contributor

@gaborcsardi gaborcsardi commented Mar 23, 2021

You can pass the functions as arguments:

library(callr)

x <- 3
userFunction <- function(x) {
  y <- (2 * x + 7)
  return(y)
}

xp <- list(x = x, userFunction = userFunction)

z <- r_bg(function(xp) {
  for (n in names(xp)) .GlobalEnv[[n]] <- xp[[n]]
  userFunction(x)
}, args = list(xp))

z$wait()
z$get_result()
#> [1] 13

Created on 2021-03-23 by the reprex package (v1.0.0)

@mlascaleia
Copy link
Author

@mlascaleia mlascaleia commented Mar 23, 2021

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants