[R] Calling C Code from R
Hi R Users, Thanks in advance. I am using R-2.5.1 on Windows XP. I am trying to call C code (testCX1.C) from R. testCX1.c calls another C code (funcC1.c) and returning a value to testCX1.c. I like to have this value in R. My steps are below: 1. R CMD SHLIB testCX1.c funcC1.c (at command propmt) 2. It creates testCX1.dll with warning (but testCX1.dll works): testCX1.c:38: warning: implicit declaration of function 'func' How to get rid off this error ? What is the best way to call funcC1.c from testCX1.c? I have provided the codes below: Once again thank you very much for the time you have given. Regards, Debabrata Midya (Deb) Statistician NSW Department of Commerce Sydney, Australia testCX1.C -- /* * testCX1.c * */ #include R.h #include Rdefines.h #include Rmath.h SEXP testC1(SEXP a) { int i, nr; double *xa, *xw, tmp; SEXP w; PROTECT(a = coerceVector(a, REALSXP)); nr = length(a); printf( Length : %d \n, nr); PROTECT(w = allocVector(REALSXP, 1)); xa = REAL(a); xw = REAL(w); tmp = 0.0; for (i = 0; i nr; i++) { tmp += xa[i]; } // tmp = 0.0; xw[0] = func(xa); UNPROTECT(2); return(w); } funcC1.c /* * funcC1.c * */ #include R.h #include Rdefines.h #include Rmath.h SEXP func(SEXP a) { int i, nr = 3; double *xa, *xw, tmp; SEXP w; PROTECT(a = coerceVector(a, REALSXP)); PROTECT(w = allocVector(REALSXP, 1)); xa = REAL(a); xw = REAL(w); tmp = 0.0; for (i = 0; i nr; i++) { tmp += xa[i] * xa[i]; } xw[0] = tmp; UNPROTECT(2); return(w); } R script --- dyn.load(testCX1.dll) xL - 1:5 xL - as.double(as.vector(t(xL))) .Call(testC1,xL) [1] 55 - [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling C Code from R
On Wed, Jul 04, 2007 at 04:39:18AM -0700, Deb Midya wrote: Hi R Users, Thanks in advance. I am using R-2.5.1 on Windows XP. I am trying to call C code (testCX1.C) from R. testCX1.c calls another C code (funcC1.c) and returning a value to testCX1.c. I like to have this value in R. My steps are below: 1. R CMD SHLIB testCX1.c funcC1.c (at command propmt) 2. It creates testCX1.dll with warning (but testCX1.dll works): testCX1.c:38: warning: implicit declaration of function 'func' How to get rid off this error ? By adding the prototype of 'func' to testCX1.c: SEXP func(SEXP a); Probably it is simplest to collect all prototypes in a single header file and include that from all .c files. What is the best way to call funcC1.c from testCX1.c? See .C and .Call and in particular the 'Writing R Extensions' manual, 5 System and foreign language interfaces. Gabor [...] -- Csardi Gabor [EMAIL PROTECTED]MTA RMKI, ELTE TTK __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling C Code from R
Gabor, Thank you very much for such a quick response. As I am new to this area, will you please explain where can I put SEXP func(SEXP a); in my program. Once again, thank you very much for your quick response. Regards, Deb Gabor Csardi [EMAIL PROTECTED] wrote: On Wed, Jul 04, 2007 at 04:39:18AM -0700, Deb Midya wrote: Hi R Users, Thanks in advance. I am using R-2.5.1 on Windows XP. I am trying to call C code (testCX1.C) from R. testCX1.c calls another C code (funcC1.c) and returning a value to testCX1.c. I like to have this value in R. My steps are below: 1. R CMD SHLIB testCX1.c funcC1.c (at command propmt) 2. It creates testCX1.dll with warning (but testCX1.dll works): testCX1.c:38: warning: implicit declaration of function 'func' How to get rid off this error ? By adding the prototype of 'func' to testCX1.c: SEXP func(SEXP a); Probably it is simplest to collect all prototypes in a single header file and include that from all .c files. What is the best way to call funcC1.c from testCX1.c? See .C and .Call and in particular the 'Writing R Extensions' manual, 5 System and foreign language interfaces. Gabor [...] -- Csardi Gabor MTA RMKI, ELTE TTK - Boardwalk for $500? In 2007? Ha! [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling C Code from R
On Wed, Jul 04, 2007 at 05:15:15AM -0700, Deb Midya wrote: Gabor, Thank you very much for such a quick response. As I am new to this area, will you please explain where can I put SEXP func(SEXP a); in my program. Deb, anywhere before calling it. (Well outside a function definition.) Typically after the #include lines. Or put all these prototypes into a header file called myfuncs.h and add line #include myfuncs.h just after the other #include lines at the beginning of the file. Gabor Once again, thank you very much for your quick response. Regards, Deb -- Csardi Gabor [EMAIL PROTECTED]MTA RMKI, ELTE TTK __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
[R] Calling C code from R
Hi! Thanks in advance. I am using R-2.4.0 on Windows XP. I am trying to create dll file. My C code: /* useC1.c */ void useC(int *i) { i[6] = 100; } I have tried to create useC1.dll. C:\R-2.4.0\binR CMD SHLIB useC1.c 'perl' is not recognized as an internal or external command, operable program or batch file. Then I have tried: C:\R-2.4.0\binRcmd SHLIB useC1.c 'perl' is not recognized as an internal or external command, operable program or batch file. I am looking forward for your reply. Regards, Deb Statistician NSW Department of Commerce Sydney Australia. - [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling C code from R
You need to install perl and MinGW, at least. If you have them installed, then you need to properly set PATH environment variable and, probably, restart your command line session. See chapter 5 of the manual Writing R extensions (installed in R_HOME/doc/manual) and these two links http://www.murdoch-sutherland.com/Rtools/ http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/ Also, it would be great to upgrade R to 2.4.1 Deb Midya wrote: I am using R-2.4.0 on Windows XP. I am trying to create dll file. My C code: /* useC1.c */ void useC(int *i) { i[6] = 100; } I have tried to create useC1.dll. C:\R-2.4.0\binR CMD SHLIB useC1.c 'perl' is not recognized as an internal or external command, operable program or batch file. Then I have tried: C:\R-2.4.0\binRcmd SHLIB useC1.c 'perl' is not recognized as an internal or external command, operable program or batch file. -- View this message in context: http://www.nabble.com/-R--Calling-C-code-from-R-tf3154058.html#a8746593 Sent from the R help mailing list archive at Nabble.com. __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calling C code from R
Deb Midya wrote: Hi! Thanks in advance. I am using R-2.4.0 on Windows XP. I am trying to create dll file. My C code: /* useC1.c */ void useC(int *i) { i[6] = 100; } I have tried to create useC1.dll. C:\R-2.4.0\binR CMD SHLIB useC1.c 'perl' is not recognized as an internal or external command, operable program or batch file. Then I have tried: C:\R-2.4.0\binRcmd SHLIB useC1.c 'perl' is not recognized as an internal or external command, operable program or batch file. I am looking forward for your reply. Did you install Perl? and did you read http://cran.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset and http://cran.r-project.org/doc/manuals/R-exts.html#Creating-R-packages? -- O__ Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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 and provide commented, minimal, self-contained, reproducible code.