Another question on functions - I have something that looks like

plotter<-function(i){
temp.i<-rwb[rwb$vector1 <=(i*.10),]
 with(temp.i, plot(vector2, vector3, main=(i*.10),))
mod<-lm(vector3~vector3-1,data=temp.i)
 r2<-summary(mod)$adj.r.squared
rsqrd[i]<-r2
legend("bottomright", legend=signif(r2), col="black")
 abline(mod)
rsqrd<<-rsqrd
}


I'd rather not have to go into the function and re-type each vector and
dataframe as I apply it to different vectors. My idea is something like:

plotter<-function(i,frm,obj,x,y){
 temp.i<-frm[frm$obj <=(i*.10),]
with(temp.i, plot(x, y, main=(i*.10),))
 mod<-lm(y~x-1,data=temp.i)
r2<-summary(mod)$adj.r.squared
rsqrd[i]<-r2
 legend("bottomright", legend=signif(r2), col="black")
abline(mod)
 rsqrd<<-rsqrd
}

However, it seems that when I do this the function is calling x, y from the
global environment rather than simply passing the text and using the frame
temp.i I create in the function. One idea I had was to go from read.csv to
frm, obj entirely within the function, but that's about 110 lines of code
each time.

I'm hoping there's a way to have some of the expressions passed within the
function without evaluation, in this case without calling data from the
global environment.

I've tried substitute(),

plotter<-function(i,frm,obj,x,y){
temp.i<-frm[frm$obj <=(i*.10),]
with(temp.i, plot(substitute(x), substitute(y), xlim=c(0,250),
ylim=c(0,35), main=(i*.10),))
 mod<-lm(substitute(y)~substitute(x)-1,data=temp.i)
r2<-summary(mod)$adj.r.squared
 rsqrd[i]<-r2
legend("bottomright", legend=signif(r2), col="black")
 abline(mod)
rsqrd<<-rsqrd
}
but am told "cannot coerce type 'symbol' to vector of type 'double'"


Any suggestions appreciated.

Thanks

Ben

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to