Giter Club home page Giter Club logo

Comments (4)

gaborcsardi avatar gaborcsardi commented on August 22, 2024

I am not sure what you mean by "quote". You mean you want to call other functions from the same package? Then yes, use the package argument.

from callr.

evanliu3594 avatar evanliu3594 commented on August 22, 2024

I am not sure what you mean by "quote". You mean you want to call other functions from the same package? Then yes, use the package argument.

yes, just use functions in the Global Environment, but callr says "could not find function"

> add_1 <- \(x) return(x+1)
> loop_x <- function(){
+   i <- 1
+   while(TRUE) {
+     i = add_1(i)
+     readr::write_lines(i, "./data/x.txt")
+     Sys.sleep(1)
+   }
+ }
> rtask <- r(loop_x)
Error: 
! in callr subprocess.
Caused by error in `add_1(i)`:
! could not find function "add_1"
Type .Last.error to see the more details.

in this case, I think the add_1 function is stored in the .GlobalEnv, where package argument by default directed to?

from callr.

gaborcsardi avatar gaborcsardi commented on August 22, 2024

Yes, that is by design, only the selected objects are copied to the subprocess. Copying the global environment is often a bad idea, if you have large objects, e.g. data sets in the global environment.

If you want to copy a set of functions, here is a way:

fun1 <- function() 1
fun2 <- function() 2
funcs <- list(fun1 = fun1, fun2 = fun2)
fun <- function(x, ..., funcs) {
  x + funcs$fun1() + funcs$fun2()
}
callr::r(fun, list(x = 3, funcs = funcs))
#> [1] 6

If you want to copy the whole global environment, you can do that as well, but again, it might be a bad idea:

callr::r(fun, list(x = 4, funcs = as.list(.GlobalEnv)))
#> [1] 7

from callr.

evanliu3594 avatar evanliu3594 commented on August 22, 2024

Thanks for your patient explain! Now I understand why the global environment functions can't be read.
And your solution is quite inspiring, I almost forgot that functions can be put into a list() 😀

from callr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.