Thursday, 15 April 2010

r - Scoping in custom lattice functions (group argument) -



r - Scoping in custom lattice functions (group argument) -

please consider function:

tf <- function(formula = null, info = null, groups = null) { grv <- eval(substitute(groups), data, environment(formula)) # values grn <- as.character(match.call()$groups) # name gr <- match.call()$groups # unquoted name p <- xyplot(formula, data, # draws info not in groups # seek these options: # p <- xyplot(formula, data, groups, # can't find 'cat2' # p <- xyplot(formula, data, groups = data[,grn], # can't fine grn # p <- xyplot(formula, data, groups = grv, # can't find grv panel = function(x, y) { panel.stripplot(x, y, jitter.data = true, pch = 20) } ) p }

which can run with:

tf(formula = mpg~vs, groups = am, info = mtcars)

what doing wrong in passing groups argument xyplot - why can't found? can't figure out how wants group information. thanks.

update:

@agstudy's reply helpful, if add together panel function in original example, groups still not recognized (no grouping, no error occurs either):

tf <- function(formula = null, info = null, groups = null) { ll <- as.list(match.call(expand.dots = false)[-1]) p <- xyplot(as.formula(ll$formula), info = eval(ll$data), groups = eval(ll$groups), panel = function(x, y) { panel.stripplot(x, y, jitter.data = true, pch = 20) } ) p }

something still missing... thanks.

you can utilize eval here since match.call returns symbols.

tf <- function(formula = null, info = null, groups = null) { ll <- as.list(match.call(expand.dots = false)[-1]) p <- xyplot(as.formula(ll$formula), info = eval(ll$data), groups = eval(ll$groups), panel = function(x, y,...) { ## here ... contains groups , subscripts ## here can transform x or y before giving them jitter panel.stripplot(x, y, jitter.data = true, pch = 20,...) } ) p }

r lattice

No comments:

Post a Comment