Initializing function arguments in the global environment R -


i'm looking simple function speed ability write , debug r functions. consider following blocks of code:

# part a: myfun = function(a, b = 5, out = "hello"){     if(a>b) print(out)     return(a-b)     }  # part b: b = 5 out = "hello"  # part c: do.args = function(f){     #intialize arguments of myfun in parent environment     ???     } 

the function myfun trivial example of bigger problem: have complicated function many arguments. efficiently write , debug such function, find useful initialize arguments of function, , 'step through' function line-by-line. initializing arguments, in part b above, hassle, when there lots of arguments, , prefer have function in part c, takes string myfun arguments , produces same effect running part b in current environment.

this works functions arguments defined. in other words, myfun has have value defined in function.

some.func <- function(infunc){   forms <- formals(infunc)   for(i in 1:length(forms)){     assign(names(forms)[i],forms[[i]],envir=globalenv())   }  } 

you add qualifier deal variables not have default values, may not work in examples. in example defined missing variables na - , change definition. note: assigning missing variables null not work.

some.func <- function(infunc){   forms <- formals(infunc)   for(i in 1:length(forms)){     if(class(forms[[i]])=="name") forms[[i]] <- na     assign(names(forms)[i],forms[[i]],envir=globalenv())   } } 

you adjust function , skip assigning missing variables using next after if statement rather defining missing variables na, or other value. next example:

some.func <- function(infunc){   forms <- formals(infunc)   for(i in 1:length(forms)){     if(class(forms[[i]])=="name") next     assign(names(forms)[i],forms[[i]],envir=globalenv())   } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -