mskb
mskb

Reputation: 338

Nested functions: "Error: Could not find nested function"

I am defining a function which has nested functions, like the following:

 afunc <- function(p1, p2) { 

 for loop {
   f = bfunc(p1)
 }
 g = cfunc(p2)

 bfunc <- function(p3) {
   ...
 }

 cfunc <-function(p4){
  ...
 }

}

For some reason, I am getting "ERROR: Could not find function "bfunc"". Am I missing something here? Thanks in advance.

Upvotes: 0

Views: 308

Answers (1)

Ari B. Friedman
Ari B. Friedman

Reputation: 72731

You need to move your function definition to before where it executes:

afunc <- function(p1, p2) { 
 bfunc <- function(p3) {
   ...
 }

 cfunc <-function(p4){
  ...
 }

 for loop {
   f = bfunc(p1)
 }
 g = cfunc(p2)


}

Upvotes: 4

Related Questions