From simon.urbanek at r-project.org  Tue Jan  4 18:09:06 2011
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Tue, 4 Jan 2011 12:09:06 -0500
Subject: [Rd] C code hanging and printing everything at the end
In-Reply-To: <64F9E9B1-2FF8-43FF-A1F8-4CB1E612A1A9@cam.ac.uk>
References: <DA930DC9-8A65-422A-8668-491236CD4952@cam.ac.uk>
	<64F9E9B1-2FF8-43FF-A1F8-4CB1E612A1A9@cam.ac.uk>
Message-ID: <CE81461A-189D-459D-9BC1-EB0F54E3C363@r-project.org>


On Jan 4, 2011, at 11:41 AM, Robert Lowe wrote:

> Hi,
> 
> I am currently writing an extension for R and have the need to include some C code. If I call the code with a large amount of data then it can take several minutes to complete.
> 
> The C code prints out after a certain iteration hence letting the user know it hasn't crashed.
> 
> When running in R this generally does not happen and all is printed out at the end once the program has completed successfully.
> 
> I am using Rprintf() to print out the required output.
> 
> e.g. Something simple which illustrates my point
> 
> for(int i=0; i<10000; i++){
> 	#Calculations
> 	if (i%1000==0){
> 		Rprintf("Step %d\n",i)
> 	}
> }
> 
> All I get during the program is the OS X spinning wheel in R. Is there any way to print out as the program is running?
> 

You want to add R_CheckUserInterrupt(); so that the system has a chance to run the event loop and thus display the result. But the implication is that you may be interrupted so make sure R controls any memory allocations you have made.

Cheers,
Simon



[Rd] C code hanging and printing everything at the end
Simon Urbanek