[R] Capturing output of a C executable

William Dunlap wdunlap at tibco.com
Mon Feb 3 18:05:30 CET 2014


> 	READSAS 	<- function(sourcefile) .C("readsas", sourcefile)
> 	OUTPUT 		<- READSAS("../SASFILES/sdrug.sas7bdat")
> R / C then reads a sas7bdat file and sends the contents to the terminal window.
> 
> I expected OUTPUT to contain the text that appear in the terminal window (i.e., the
> contents of the file).  But, that is not the case; OUTPUT contains:
> 	[[1]]
> 	[1] "../SASFILES/sdrug.sas7bdat"

READSAS returns the value of .C(...), which returns a list of its arguments,
perhaps modified by the C function you called.  Thus you should expect
READSAS to return the filename you gave it (in a list).

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Dennis Fisher
> Sent: Sunday, February 02, 2014 5:10 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Capturing output of a C executable
> 
> R 3.0.1
> OS X
> 
> Colleagues,
> 
> I am experimenting with incorporating C code into R.  After compiling the C code with:
> 	R CMD SHLIB -o FILE.so FILE.c
> and executing:
> 	dyn.load("FILE.so")
> (without any errors), I execute the following R functions in a terminal window:
> 	READSAS 	<- function(sourcefile) .C("readsas", sourcefile)
> 	OUTPUT 		<- READSAS("../SASFILES/sdrug.sas7bdat")
> R / C then reads a sas7bdat file and sends the contents to the terminal window.
> 
> I expected OUTPUT to contain the text that appear in the terminal window (i.e., the
> contents of the file).  But, that is not the case; OUTPUT contains:
> 	[[1]]
> 	[1] "../SASFILES/sdrug.sas7bdat"
> It is not clear to my how to capture that appears in the terminal window.
> 
> Ultimately, I may need to modify the C code so that the output goes to a file, which I
> then read into R.  However, it would be better if I did not need to modify the C code.
> Does anyone have any ideas of how I can capture this output within R?
> 
> Dennis
> 
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.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.




More information about the R-help mailing list