[R] passing arguments to functions within functions
Duncan Murdoch
murdoch at stats.uwo.ca
Wed Oct 17 19:49:12 CEST 2007
On 10/17/2007 1:12 PM, Daniel E. Bunker wrote:
> Dear R Users,
>
> I am trying to write a wrapper around summarize and xYplot from Hmisc
> and am having trouble understanding how to pass arguments from the
> function I am writing to the nested functions.
>
> There must be a way, but I have not been able to figure it out.
>
> An example is below.
I think you need to simplify your example. I can't tell what you want
to do.
It would be helpful if you gave us a simpler example showing what you'd
like to use as arguments to your function, and what you'd like the
function it calls to see. Then you'll probably get several suggestions
of how to do it.
Duncan Murdoch
> Any advice would be greatly appreciated.
>
> Thanks, Dan
>
>
>
>
>
> # some example data
> df=expand.grid(rep=1:4, fac1=c("a","b"), fac2=c("c","d"), fac3=c
> ("e","f"))
> df$resp1=rnorm(length(df$fac1))
> df
>
> # define a funciton to compute mean, std error and n.
> meanstderr <- function(x) c(Mean=mean(x,na.rm=TRUE),se=sqrt(var
> (x,na.rm=TRUE)/length(na.omit(x))), n=length(na.omit(x)))
>
>
> # what I want to wrap into a single funtction:
> sumdf=with(df, summarize(X=resp1, by=llist(fac1, fac2, fac3),
> stat.name="resp1", FUN=meanstderr))
> xYplot(Cbind(resp1, resp1-se, resp1+se) ~ as.numeric(fac1)|fac2,
> groups=fac3, data=sumdf, type='b',
> scales=list(x=list(labels=levels(df$fac1), at=1:length(levels(df
> $fac1)))), xlim=c(0,length(levels(df$fac1))+1))
>
> # here is a function to make the call to "summarize", but I cannot
> understand how to pass the arguments to "summarize"
> # I've tried passing them as names, text and vectors, but to no avail
> xYerrbar=function(formula, data = sys.frame(sys.parent()), groups,
> subset,
> xlab = NULL, ylab = NULL, ylim = NULL, panel = panel.xYplot,
> prepanel = prepanel.xYplot, scales = NULL, minor.ticks = NULL,
> ...)
> { # data=df; groups=fac2
> require("Hmisc")
> require("grid")
> require("lattice")
> ytext <- as.character(formula[2])
> yvname <- formula[[2]]
> y <- eval(parse(text = ytext), data)
> xexp <- formula[[3]]
> if (length(xexp) > 1 && as.character(xexp[[1]]) == "|") {
> xvname <- xexp[[2]]
> byname <- xexp[[3]]
> xtext <- as.character(xexp[2])
> bynametext <- as.character(xexp[3])
> }
> else {xvname <- xexp; xtext <- as.character(xexp)}
> xv <- eval(xvname, data)
> byfac <- eval(byname, data)
> if (!missing(groups))
> {groups <- eval(substitute(groups), data)}
> # none of the below seem to work
> sumdf1=do.call("summarize", args=list(X=y, by=llist(xvname),
> FUN=meanstderr))
> sumdf2=do.call("summarize", args=list(X=y, by=xv, FUN=meanstderr))
> sumdf4=summarize(y, by=llist(xv), FUN=meanstderr)
> }
>
> xYerrbar(formula=resp1~fac2|fac1, data=df, groups=fac3)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list