Re: [R] Capturing output of a C executable
On 14-02-03 5:44 PM, Phil Spector wrote: Dennis - The return value from .C will almost never be useful. Are you limiting this to the specific situation Dennis described, or making a more general claim? The more general claim is clearly false. Lots of packages return useful results using .C calls. The idea is that all arguments to the C function are passed by reference and may be modified in place, so you just pass a vector to receive the result. That's equivalent to what you describe below, but I think it seems simpler, since passing an address from R sounds like an exotic operation, not the norm. Duncan Murdoch If you want to bring results from the C environment into R, you need to do it by passing an address to .C which will receive the result. You may find this document helpful when interfacing R to C: http://www.stat.berkeley.edu/classes/s243/calling.pdf - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Sun, 2 Feb 2014, Dennis Fisher wrote: 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@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. __ R-help@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. __ R-help@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.
Re: [R] Capturing output of a C executable
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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Dennis Fisher Sent: Sunday, February 02, 2014 5:10 PM To: r-h...@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@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. __ R-help@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.
Re: [R] Capturing output of a C executable
On Feb 2, 2014, at 5:09 PM, Dennis Fisher wrote: 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? I wonder if this is a follow-up to the suggestion to look at ReadStat?: https://github.com/WizardMac/ReadStat I could imagine calling it from R but then directing results to a comma separated file along the lines of the second example. If my guess is correct, you might assist the R community and get valuable advice by posting your source and Makefile on R-devel. -- David. 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 David Winsemius Alameda, CA, USA __ R-help@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.
Re: [R] Capturing output of a C executable
Dennis - The return value from .C will almost never be useful. If you want to bring results from the C environment into R, you need to do it by passing an address to .C which will receive the result. You may find this document helpful when interfacing R to C: http://www.stat.berkeley.edu/classes/s243/calling.pdf - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Sun, 2 Feb 2014, Dennis Fisher wrote: 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@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. __ R-help@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.
[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@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.
Re: [R] Capturing output of a C executable
You really need to read the Writing R Extensions document. It warns you against performing I/O from C code linked to R. You probably ought to read the Posting Guide, also, since this question is off topic here --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. On February 2, 2014 5:09:33 PM PST, Dennis Fisher fis...@plessthan.com wrote: 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@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. __ R-help@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.
Re: [R] Capturing output of a C executable
?sink Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Sun, Feb 2, 2014 at 8:09 PM, Dennis Fisher fis...@plessthan.com wrote: 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@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. __ R-help@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.