[R] avoiding expression evaluation when calling a function
Bert Gunter
gunter.berton at gene.com
Fri Mar 30 21:57:35 CEST 2012
Start by reading the Help files,please.
?plot.formula ## and note the data argument
So dispense with the unnecessary complex constructions altogether:
plot( y~x, data = temp.i)
-- Bert
On Fri, Mar 30, 2012 at 12:42 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
> Hi Benjamin,
>
> See inline
>
> On Fri, Mar 30, 2012 at 12:31 PM, Benjamin Caldwell
> <btcaldwell at berkeley.edu> wrote:
>> 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:
>>
>
> Close, but pass a formula object. Something like (untested)
>
>
> plotter <- function(i, frm, obj, form){
> temp.i<-frm[frm$obj <=(i*.10),]
> plot(form, main=(i*.10), data = temp.i)
> mod<-lm(form, data=temp.i)
> r2<-summary(mod)$adj.r.squared
> legend("bottomright", legend=signif(r2), col="black")
> abline(mod)
> return(rsqrd)
> }
>
> and I guess you are doing this for different values of i? so maybe
>
>
> rsqrd <- lapply(1:100, function(my.i) plotter(i = my.i, frm =
> your_frm, obj = your_obj, form = y ~ x))
>
> Hope this helps,
>
> Josh
>
>
>
>> 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 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.
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> Programmer Analyst II, Statistical Consulting Group
> University of California, Los Angeles
> https://joshuawiley.com/
>
> ______________________________________________
> 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.
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list