How to get name of variable in R (substitute)? -
i stacked trying pass variable through few functions, , on final function want name of original variable. seems substitute function in r looked in "local" environment, or 1 level up. well, allow me explain code:
fun1 <- function (some_variable) {deparse(substitute(some_variable)} fun2 <- function (var_pass) { fun1 (var_pass) } my_var <- c(1,2) # want 'my_var' in end fun2 (my_var) # > "var_pass" well, seems printing name of variable pass fun1. documentation of substitute tells us, can utilize env argument, specify can look. passing .global or .basenamespaceenv argument substitute got more unusual results - "some_variable"
i believe reply in function using env argument, so, please explain me how works , how can need. in advance!
i suggest consider passing optional name value these functions. because seems want utilize name label in end result; it's not variable matters much name. do
fun1 <- function (some_variable, name=deparse(substitute(some_variable))) { name } fun2 <- function (var_pass, name=deparse(substitute(var_pass))) { fun1 (var_pass, name) } my_var <- c(1,2) fun2(my_var) # [1] "my_var" fun1(my_var) # [1] "my_var" this way if end having odd variable name , give improve name result, @ to the lowest degree have option. , default should want without having require name parameter.
r variables substitution
No comments:
Post a Comment