give 2 functions
subfun <- function(txt) gsubfn::gsubfn("§([^§]+)§", ~eval(parse(text=x)), txt) topfun <- function(id = 1l) subfun("hello §id§ world!") the following (1.) should yield "hello 1 world!"but throws error instead:
topfun() # error in eval(expr, envir, enclos) : object 'id' not found these 2 (2.) & (3.) work expected:
id <- 2l topfun() # [1] "hello 2 world!" topfun2 <- function(id = 1l) gsubfn::gsubfn("§([^§]+)§", ~eval(parse(text=x)), "hello §id§ world!") topfun2() # [1] "hello 1 world!" how can make (1.) work?
i tried several environment() , parent.frame() variations envir parameter of eval , gsubfn, including passing topfun's environment subfun via ellipsis argument. no success. (not had greater knowledge of what's going on under hood. have expected r go 1 parent environment after id...)
i'm using r version 3.3.0 , gsubfn package version 0.6.6.
thanks in advance!
i no expert @ problem seems use of formula replacement in gsubfun. @ least unable pass environment eval if in formula.
subfun_2 <- function(txt){ ev <- parent.frame() # environment in subfun_2 called gsubfn::gsubfn("§([^§]+)§", ~eval(parse(text=x), envir = ev), txt) } topfun_2 <- function(id = 1l) subfun_2("hello §id§ world!") topfun_2() # error in eval(parse(text = x), envir = ev) : # argument "ev" missing, no default if use function instead works expected:
subfun_3 <- function(txt){ ev <- parent.frame() gsubfn::gsubfn("§([^§]+)§", function(x)eval(parse(text=x), envir = ev), txt) } topfun_3 <- function(id = 1l) subfun_3("hello §id§ world!") topfun_3() # hello 1 world!
Comments
Post a Comment