Hi, I am hoping some one can help me. I am learning to use C and would like to learn how to call c code in R. I have look at Writing R Extensions and I tried to copy the example on page 38 void convolve(double *a, int *na, double *b, int *nb, double *ab) { int i, j, nab = *na + *nb - 1; for(i = 0; i < nab; i++) ab[i] = 0.0; for(i = 0; i < *na; i++) for(j = 0; j < *nb; j++) ab[i + j] += a[i] * b[j]; } called from R by conv <- function(a, b) .C("convolve", as.double(a), as.integer(length(a)), as.double(b), as.integer(length(b)), ab = double(length(a) + length(b) - 1))$ab and I got the error "C" function name not in load table. Do I need to compile the C code first? Do I need a c copmiler at all? Any suggestions for a begginner? Thanks, Elizabeth Lawson I --------------------------------- Click here to donate to the Hurricane Katrina relief effort. [[alternative HTML version deleted]]
Hi, I am hoping some one can help me. I am learning to use C and would like to learn how to call c code in R. I have look at Writing R Extensions and I tried to copy the example on page 38 void convolve(double *a, int *na, double *b, int *nb, double *ab) { int i, j, nab = *na + *nb - 1; for(i = 0; i < nab; i++) ab[i] = 0.0; for(i = 0; i < *na; i++) for(j = 0; j < *nb; j++) ab[i + j] += a[i] * b[j]; } called from R by conv <- function(a, b) .C("convolve", as.double(a), as.integer(length(a)), as.double(b), as.integer(length(b)), ab = double(length(a) + length(b) - 1))$ab and I got the error "C" function name not in load table. Do I need to compile the C code first? Do I need a c copmiler at all? Any suggestions for a begginner? Thanks, Elizabeth Lawson --------------------------------- Click here to donate to the Hurricane Katrina relief effort. [[alternative HTML version deleted]]
On Fri, 30 Sep 2005, Elizabeth Lawson wrote:> Hi, > I am hoping some one can help me. I am learning to use C and would > like to learn how to call c code in R. I have look at Writing R > Extensions > > > > > > and I tried to copy the example on page 38 > > > > void convolve(double *a, int *na, double *b, int *nb, double *ab) > > { > > int i, j, nab = *na + *nb - 1; > > for(i = 0; i < nab; i++) > > ab[i] = 0.0; > > for(i = 0; i < *na; i++) > > for(j = 0; j < *nb; j++) > > ab[i + j] += a[i] * b[j]; > > } > > called from R by > > conv <- function(a, b) > > .C("convolve", > > as.double(a), > > as.integer(length(a)), > > as.double(b), > > as.integer(length(b)), > > ab = double(length(a) + length(b) - 1))$ab > > > > > > and I got the error "C" function name not in load table. > > > > Do I need to compile the C code first? > > Do I need a c copmiler at all? >Yes, you need to compile the C code first, into a file in the correct format to dyn.load() into R. The format varies with operating system, and when the appropriate compilers and other software components are present, can be done convieniently by entering R CMD SHLIB convolve.c at the shell command prompt for your system. On Linux systems, the software needed is usually installed already, on Windows visit http://www.murdoch-sutherland.com/Rtools/ for advice on where to get and how to install the C compiler used to build R itself. Do look at documentation about C, having a compiler at hand to try out examples and exercises is very helpful.> > > Any suggestions for a begginner? > > > > Thanks, > > > > Elizabeth Lawson > > > > > --------------------------------- > > Click here to donate to the Hurricane Katrina relief effort. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
See http://www.stats.uwo.ca/faculty/murdoch/software/compilingDLLs/ and "R Installation and Administration" manual for details that will have to be followed very precisely. My own 2 cents below should be taken as fuzzy recollections, compared to detailed directions in 2 sources above. I do not know if they are the easiest or even correct, but they work. There are 2 ways to compile that I tried (both use MinGW compiler): - compile C into dll. Copy dll to the same directory as R functions. I was compiling on WIN XT machine by double clicking on "any-name.bat" text file containing: cl /MT /Ox /D "WIN32" /c runfunc.c link /dll /EXPORT:runquantile /EXPORT:runmean /EXPORT:runmad /out:runfunc.dll *.obj pause My code was in "runfunc.c". C Functions I was planning on calling were: "runquantile" "runmean" "runmad". DLL name was "runfunc.dll" - follow the steps as if you were building a package (even if you are not planing on releasing it). This way was much easier, but only after I installed many other tools needed for package instalation/compilation. After everything was correctly set up the actual package compilation is done by double-clicking on "C:\programs\R\rw2011\src\gnuwin32\install_caTools.bat" text file containing: RCMD install C:/programs/R/rw2011/src/library/caTools pause Both of those approaches (I think) required R beeing installed in a path with no spaces (for example R can not be installed in default spot "c:\Program Files\R" since "program files" have space between two words. Also I remember having a lot of trouble ordering directories on my PATH (envirinmental variable - see "my computer/properties/advanced/Environment Variables"). Jarek ====================================================\==== Jarek Tuszynski, PhD. o / \ Science Applications International Corporation <\__,| (703) 676-4192 "> \ Jaroslaw.W.Tuszynski at saic.com ` \ -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Elizabeth Lawson Sent: Friday, September 30, 2005 2:46 PM To: r-help at stat.math.ethz.ch Subject: [R] .C help Hi, I am hoping some one can help me. I am learning to use C and would like to learn how to call c code in R. I have look at Writing R Extensions and I tried to copy the example on page 38 void convolve(double *a, int *na, double *b, int *nb, double *ab) { int i, j, nab = *na + *nb - 1; for(i = 0; i < nab; i++) ab[i] = 0.0; for(i = 0; i < *na; i++) for(j = 0; j < *nb; j++) ab[i + j] += a[i] * b[j]; } called from R by conv <- function(a, b) .C("convolve", as.double(a), as.integer(length(a)), as.double(b), as.integer(length(b)), ab = double(length(a) + length(b) - 1))$ab and I got the error "C" function name not in load table. Do I need to compile the C code first? Do I need a c copmiler at all? Any suggestions for a begginner? Thanks, Elizabeth Lawson I --------------------------------- Click here to donate to the Hurricane Katrina relief effort. [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html