0
votes

I'm following this guide: http://www.biostat.jhsph.edu/~rpeng/docs/interface.pdf

I want to create code copiled in C to use it in R So I created my hello.c file:

#include <R.h>
void hello(int *n)
{
int i;
for(i=0; i < *n; i++) {
Rprintf("Hello, world!\n");
}
}

And I saved it in c:\temp Having done that, I must then compile the C code.

I honestly don't have any idea what I'm doing but I'm tring to write in windows cmd:

cd C:\Program Files\R\R-3.0.2\bin\
R CMD SHLIB c:\temp\hello.c

And I get the error:

cygwin warning: MS-DOS style path detected: C:/PROGRA~1/R/R-30~1.2/etc/i386/Makeconf Preferred POSIX equivalent is: /cygdrive/c/PROGRA~1/R/R-30~1.2/etc/i386/Makeco nf CYGWIN environment variable option "nodosfilewarning" turns off this warning. Consult the user's guide for more details about POSIX paths: http://cygwin.com/cygwin-ug-net/using.html#using-pathnames make: * No rule to make target c:\temp\hello.o', needed byc:\temp\hello.dll '. Stop.

This should create a file names "hello.so". So I can call it in R with:

        dyn.load("hello.so")

        hello2 <- function(n) {
                                .C("hello", as.integer(n))
                           }

        hello2(5)

But my command doesn't create the hello.so file. Maybe I need to install another compiler, but I can't install software in the PC that I'm using. Is there a way to do it with the windows cmd? Where is my mistake?

2
You need to follow the instructions in Appendix D: The Windows Toolset of the R Installation and Administration manual.Joshua Ulrich
Related question here: stackoverflow.com/questions/13987667/r-external-interface with some other resourcesRusan Kax
I think .C is going away soon. Dirk mentions it in his answer here stackoverflow.com/questions/23453373/…. Recommending .Call and .External insteadRich Scriven
Not going away (as in: existing interfaces remain available) but stronger and stronger recommendations against use -- .C() is limited whereas .Call() allows full objects access.Dirk Eddelbuettel

2 Answers

3
votes

Assuming C:\temp\hello.c exists try this from the windows cmd line (carefully checking that Rtools, R and the paths used exist):

cd c:\temp
path C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin;%ProgramFiles%\R\R-3.0.2\bin\x64;%path%
R CMD SHLIB hello.c
Rgui

Now in R:

dyn.load("hello.dll")
.C("hello", 3L)

Note: Also there are some batch files here that may help:

http://batchfiles.googlecode.com

http://code.google.com/p/batchfiles/source/browse/#svn%2Ftrunk

See Rpathset.bat, R.bat and the documentation batchfiles.md .

Update Corrections and improvements.

0
votes

This does not answer your question directly, but have you looked into the Rcpp package, which allows for C++ to be integrated (called from) R? It might make life a lot easier for you, depending upon what you are doing.

Look here, and also at the R docs and this from a presentation by Dirk E