[R] passing arguments to functions within functions

Daniel E. Bunker deb37 at columbia.edu
Wed Oct 17 20:57:54 CEST 2007


Thanks for your reply.  Here is a simplified version.  What I really  
want to be able to do is summarize same data (using 'summarize') and  
pass those results to 'xYplot' for plotting, all in one wrapper.

For this example, I am simply trying to call 'summarize' from within  
another function.  The question is how to pass the arguments to  

(Note I am on OS X using R 2.5.1)

Any help 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 

#  what I would like to do inside another function
resp="resp1"; fac="fac1"
sumdf4=summarize(X=df[,resp], by=llist(df[,fac]), FUN=mean)

# but how do I name the arguments in both the function and the  
funtion call?
# in the call to "summarize", I want the summarize argument "X" to be  
the dataframe vector df[, resp1]
# 	and the "by" variable to be df[, fac1]
# I want to be able to name them by the column name (ie, resp1, not df 
$resp1) so that I can then call
# 	xYplot on the relulting dataframe sumdf4, using the formula  
"resp1~fac1", or the arguments that point to those names
xYerrbar=function(resp, fac, data) {
	sumdf4=summarize(X=resp, by=llist(fac), FUN=mean)

xYerrbar(resp=resp1, fac=fac1, data=df) # Error in llist(fac) :  
object "fac1" not found

# I have also tried this to no avail
xYerrbar=function(resp, fac, data) {
	sumdf4=summarize(X=eval(resp, data), by=llist(eval(fac, data)),  

On Oct 17, 2007, at 1:49 PM, Duncan Murdoch wrote:

> 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

More information about the R-help mailing list