Shan Tulshi
Shan Tulshi

Reputation: 11

I can call a function, but another function's internal call cannot find the function

I have an object, suppose it's called obj. I can call a function, obj$a() and that works. However, when I call obj$b(), which internally calls self$a(), it throws an error saying that it cannot find the a function. What can I do?

Upvotes: 0

Views: 44

Answers (1)

AEF
AEF

Reputation: 5650

You need to make sure that the functions share an environment / are in the same closure. You could encapsulate them in a dummy function. Look at this example:

gives_error <- list(a = function() { 
                          print("Hello from a")
                        },
                    b = function(){
                          print("Hello from b")
                          a()
                    })

gives_error$b()


will_work <-
    (function() {
        a = function(){ 
            print("Hello from a")
        }
        b = function(){
            print("Hello from b")
            a()
        }
        list(a = a, b = b)
    })()

will_work$b()

Upvotes: 2

Related Questions