[R] Obtaining partial output from a function that does not run to completion.

Duncan Murdoch murdoch at stats.uwo.ca
Thu Dec 31 16:53:04 CET 2009


John Sorkin wrote:
> Duncan,
> Thank you, however, if you will look at my function (bottom of this Email message), you will see that I have used print(). It appears that the print function is not run until the entire function completes running.
>   

No, statements are run in the order you give them.  The output of the 
prints might not appear immediately, but they will be executed.

You haven't posted any code that I can run, so I can't tell you what is 
going wrong in your case, but here's what I see with my own example:

 > f <- function(x) {
+ print(1)
+ print(2)
+ stop('error')
+ }
 > f(2)
[1] 1
[1] 2
Error in f(2) : error

Duncan Murdoch
> John
>
>
>
>
> 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)>>> Duncan Murdoch <murdoch at stats.uwo.ca> 12/31/2009 10:19 AM >>>
> 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.
>>     
>
>
> Confidentiality Statement:
> This email message, including any attachments, is for ...{{dropped:3}}




More information about the R-help mailing list