[R] calling a c function from R

Matt Shotwell shotwelm at musc.edu
Wed Jul 14 16:18:25 CEST 2010


Fahim, 

Please see the Writing R Extensions manual
http://cran.r-project.org/doc/manuals/R-exts.pdf

There are simple instructions in this document under the heading "System
and foreign language interfaces."

-Matt


On Wed, 2010-07-14 at 01:21 -0400, Fahim Md wrote:
> Hi,
> I am trying to call a C function, that I wrote to parse a flat file,  into
> R. The argument that will go into this function is an input file that I need
> to parse and write the desired output in an output file.  I used some hit
> and trial approach but i keep on getting the "file not found" or
> "segmentation fault" error. I know that the error is in passing the argument
> but I could not solve it.
> 
> After reading  some of the tutorials, I understood how to do this if the
> arguments are integers or floats. I am stuck when i am trying to send the
> files. I am attaching stub of each file.
> Help appreciated.
> Thanks
> 
> ---------------------------------------
> My function call would be:
> source("parse.R")
> parseGBest('./gbest/inFile.seq',   './gbest/outFile.out');
> ---------------------------------------
> I wrote a wrapper function (parse.R) as follows:
> 
> dyn.load("parse.so");
> parseGBest = function(inFile, outFile)
> {
> .C( "parse" , inFile , outFile);
> }
> 
> How to write receive the filenames in function( , ) above. and how to call
> .C
> 
> ----------------------------------------
> parse.c file is as below:  How to receive the argument in funcion and how to
> make it compatible with my argv[ ].
> 
> 
> void parse( int argc, char *argv[] )  //This is working as standalone C
> program. How to receive
>                                                   // the above files so that
> it become compatible with my argv[ ]
>     {
> 
>     FILE *fr, *of;
>     char line[81];
> 
> 
>  if ( *argc == 3 )*/
>         {
>         if ( ( fr = fopen( argv[0], "r" )) == NULL )
>             {
>             puts( "Can't open input file.\n" );
>             exit( 0 );
>             }
>         if ( ( of = fopen( argv[1], "w" )) == NULL )
>             {
>             puts( "Output file not given.\n" );
>             }
>       }
>    else
>         {printf("wrong usage: Try Agay!!! correct usage is:=  functionName
> inputfileToParse outFileToWriteInto\n");
>        }
>     while(fgets(line, 81, fr) != NULL)
> 
> --
> ---
> --
> }
> 
> 
> 
> Thanks again
> Fahim
> 
-- 
Matthew S. Shotwell
Graduate Student
Division of Biostatistics and Epidemiology
Medical University of South Carolina
http://biostatmatt.com



More information about the R-help mailing list