John Sorkin
2009-Dec-31 14:29 UTC
[R] Obtaining partial output from a function that does not run to completion.
I have written a function that contains runs lm() vif() and glm() When the glm() blows up with an error message, I don't get the output from either the lm() or vf() even thought neither lm() nor vif() have any problems . How can I force the function to print sequential results rather than wait for the entire function to complete before listing the functhion's output? Thanks, John minBMI<-function(SS,SimData) { SampleData<-sample(1:SS,size=SS,replace=TRUE) fitBMIEpiRevlm<-lm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI+jMEDICAID+H_AGE+jMARSTAT+factor(jEDUCATION)+factor(jsmokercat)+factor(jrace)+log(INCOME_C+1),data=SimData[SampleData,],x=TRUE) print(summary(fitBMIEpiRevlm)) print(vif(fitBMIEpiRevlm)) fitBMIEpiRev<- glm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI+jMEDICAID+H_AGE+jMARSTAT+factor(jEDUCATION)+factor(jsmokercat)+factor(jrace)+log(INCOME_C+1),data=SimData[SampleData,],family=Gamma(link="log")) print(summary(fitBMIEpiRev)) } minBMI(SS,SimData) John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:6}}
Duncan Murdoch
2009-Dec-31 15:19 UTC
[R] Obtaining partial output from a function that does not run to completion.
On 31/12/2009 9:29 AM, John Sorkin wrote:> I have written a function that contains runs > lm() > vif() and > glm() > > When the glm() blows up with an error message, I don't get the output from either the lm() or vf() even thought neither lm() nor vif() have any problems . How can I force the function to print sequential results rather than wait for the entire function to complete before listing the functhion's output?Call print, e.g. print(x <- lm(...)) print(y <- vif(...)) etc. Duncan Murdoch> Thanks, > John > > > > minBMI<-function(SS,SimData) > { > SampleData<-sample(1:SS,size=SS,replace=TRUE) > fitBMIEpiRevlm<-lm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI+jMEDICAID+H_AGE+jMARSTAT+factor(jEDUCATION)+factor(jsmokercat)+factor(jrace)+log(INCOME_C+1),data=SimData[SampleData,],x=TRUE) > print(summary(fitBMIEpiRevlm)) > print(vif(fitBMIEpiRevlm)) > fitBMIEpiRev<- glm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI+jMEDICAID+H_AGE+jMARSTAT+factor(jEDUCATION)+factor(jsmokercat)+factor(jrace)+log(INCOME_C+1),data=SimData[SampleData,],family=Gamma(link="log")) > > print(summary(fitBMIEpiRev)) > } > minBMI(SS,SimData) > > > John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for th...{{dropped:6}} > > ______________________________________________ > 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.
David Winsemius
2009-Dec-31 15:21 UTC
[R] Obtaining partial output from a function that does not run to completion.
On Dec 31, 2009, at 8:29 AM, John Sorkin wrote:> I have written a function that contains runs > lm() > vif() and > glm() > > When the glm() blows up with an error message, I don't get the > output from either the lm() or vf() even thought neither lm() nor > vif() have any problems . How can I force the function to print > sequential results rather than wait for the entire function to > complete before listing the functhion's output? > Thanks, > JohnI thought the usual method for graceful error recovery was to use the try or tryCatch functions.> >minBMI<-function(SS,SimData) { SampleData<-sample(1:SS,size=SS,replace=TRUE) fitBMIEpiRevlm<-lm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI +jMEDICAID+H_AGE+jMARSTAT +factor(jEDUCATION) +factor(jsmokercat) +factor(jrace)+log(INCOME_C+1),data=SimData[SampleData,],x=TRUE) print(summary(fitBMIEpiRevlm)) print(vif(fitBMIEpiRevlm)) #Perhaps (untested): try( { fitBMIEpiRev<- glm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI +jMEDICAID+H_AGE +jMARSTAT+factor(jEDUCATION) +factor(jsmokercat) +factor(jrace)+log(INCOME_C +1),data=SimData[SampleData,],family=Gamma(link="log")) print(summary(fitBMIEpiRev)) } ) } minBMI(SS,SimData) -- David.> John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics
William Dunlap
2009-Dec-31 17:34 UTC
[R] Obtaining partial output from a function that does not run to completion.
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of John Sorkin > Sent: Thursday, December 31, 2009 6:30 AM > To: r-help at r-project.org > Subject: [R] Obtaining partial output from a function that > does not run to completion. > > I have written a function that contains runs > lm() > vif() and > glm() > > When the glm() blows up with an error message, I don't get > the output from either the lm() or vf() even thought neither > lm() nor vif() have any problems . How can I force the > function to print sequential results rather than wait for the > entire function to complete before listing the functhion's output? > Thanks, > JohnIf you are using the Windows GUI version of R you may be running into "output buffering", in which output is not printed until the top level expression is done or until enough output is generated. The Misc\Buffered Output menu item or pressing control-W will toggle output buffering. If that fixes your problem, I think you can use dialog box under the Edit\GUI Preferences ... menu item to set the default value for output buffering. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > > > minBMI<-function(SS,SimData) > { > SampleData<-sample(1:SS,size=SS,replace=TRUE) > fitBMIEpiRevlm<-lm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI+jMEDICAID+H_AGE+jMARSTAT+factor(jEDUCATION)+factor(jsmokerc> at)+factor(jrace)+log(INCOME_C+1),data=SimData[SampleData,],x=TRUE)> print(summary(fitBMIEpiRevlm)) > print(vif(fitBMIEpiRevlm)) > fitBMIEpiRev<- > glm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI+jMEDICAID+ > H_AGE+jMARSTAT+factor(jEDUCATION)+factor(jsmokercat)+factor(jr > ace)+log(INCOME_C+1),data=SimData[SampleData,],family=Gamma(link="log"))> > print(summary(fitBMIEpiRev)) > } > minBMI(SS,SimData) > > > John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for > th...{{dropped:6}} > > ______________________________________________ > 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. >
Eric
2009-Dec-31 19:31 UTC
[R] Obtaining partial output from a function that does not run to completion.
To heck with print(), use R's debugging capabilities instead: trace ( minBMI, browser ) Be sure to ?trace and ?browser so you can figure out how to interactively debug. Eric On 12/31/09 6:29 AM, John Sorkin wrote:> I have written a function that contains runs > lm() > vif() and > glm() > > When the glm() blows up with an error message, I don't get the output from either the lm() or vf() even thought neither lm() nor vif() have any problems . How can I force the function to print sequential results rather than wait for the entire function to complete before listing the functhion's output? > Thanks, > John > > > > minBMI<-function(SS,SimData) > { > SampleData<-sample(1:SS,size=SS,replace=TRUE) > fitBMIEpiRevlm<-lm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI+jMEDICAID+H_AGE+jMARSTAT+factor(jEDUCATION)+factor(jsmokercat)+factor(jrace)+log(INCOME_C+1),data=SimData[SampleData,],x=TRUE) > print(summary(fitBMIEpiRevlm)) > print(vif(fitBMIEpiRevlm)) > fitBMIEpiRev<- glm(AAMTCARE~BMIEpiRevAdjc+BMIEpiRevAdjcSq+SEX+jPHI+jMEDICAID+H_AGE+jMARSTAT+factor(jEDUCATION)+factor(jsmokercat)+factor(jrace)+log(INCOME_C+1),data=SimData[SampleData,],family=Gamma(link="log")) > > print(summary(fitBMIEpiRev)) > } > minBMI(SS,SimData) > > > John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for th...{{dropped:6}} > > ______________________________________________ > 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. >