R : define a function within a function -


(about r language)

i trying declare/define function, within function. doesn't seem work. don't think bug, it's expected behavior, understand why ! answer linking relevant manual pages welcome.

thanks

code :

fun1 <- function(){   print("hello")   fun2 <- function(){ #will define fun2 when fun1 called     print(" world")   } }  fun1() #so expected fun2 defined after running line fun2() #aaand... turns out isn't 

execution :

> fun1 <- function(){ +   print("hello") +   fun2 <- function(){ #will define fun2 when fun1 called +     print(" world") +   } + } >  > fun1() #so expected fun2 defined after running line [1] "hello" > fun2() #aaand... turns out isn't error : not find function "fun2" 

this work expect considered bad practice in r:

fun1 <- function(){   print("hello")   fun2 <<- function(){ #will define fun2 when fun1 called     print(" world")   } } 

where changed <- <<- in line 3 of function definition. execution:

> fun1 <- function(){ +     print("hello") +     fun2 <<- function(){ #will define fun2 when fun1 called +         print(" world") +     } + } >  > fun1() [1] "hello" > fun2() [1] " world" 

Comments