C-Code in R (running Windows)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

C-Code in R (running Windows)

K.A.
Hello,

I need to include C (or C++)-Code in R. I'm searching for a tutorial, how to do this. It is only a very small algorith (one function). Using google, I read somethig about including dll-Files and so on (on windows).

Is there no possibility to include a C-File directly?!?
Reply | Threaded
Open this post in threaded view
|

Re: C-Code in R (running Windows)

Tuszynski, Jaroslaw W.
The simplest way to add C (C++) code in R, that I know is by creating a
package ( even if you are not planning on distributing it). See 'Writing
R Extensions' manual (http://cran.r-project.org/manuals.html) for
details. Download source codes of packages for more examples, many of
them will have .c files. See also FAQ
http://cran.r-project.org/bin/windows/base/rw-FAQ.html#Building-from-Sou
rce .

Jarek Tuszynski

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of K.A.
Sent: Wednesday, March 01, 2006 7:14 AM
To: [hidden email]
Subject: [R] C-Code in R (running Windows)


Hello,

I need to include C (or C++)-Code in R. I'm searching for a tutorial,
how to
do this. It is only a very small algorith (one function). Using google,
I
read somethig about including dll-Files and so on (on windows).

Is there no possibility to include a C-File directly?!?
--
View this message in context:
http://www.nabble.com/C-Code-in-R-%28running-Windows%29-t1205196.html#a3
182004
Sent from the R help forum at Nabble.com.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: C-Code in R (running Windows)

Liaw, Andy
In reply to this post by K.A.
The simplest interface is to use .C() in R to call the C function.  The
details (with examples) are in the Writing R Extensions manual that comes
with every version of R.  To compile and load the .dll/.so into R, do:

- Type R CMD SHLIB file1.c file2.c  (at the command prompt, not the R
prompt)
-...start up R in that directory
- Type dyn.load("file1")
- You can now use .C() to call the function

Andy

From: K.A.

>
> Hello,
>
> I need to include C (or C++)-Code in R. I'm searching for a
> tutorial, how to
> do this. It is only a very small algorith (one function).
> Using google, I
> read somethig about including dll-Files and so on (on windows).
>
> Is there no possibility to include a C-File directly?!?
> --
> View this message in context:
> http://www.nabble.com/C-Code-in-R-%28running-Windows%29-t12051
96.html#a3182004
Sent from the R help forum at Nabble.com.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: C-Code in R (running Windows)

K.A.
I got it!!!
As Andy said, I generated an dll-File (using Dev-C++, it was quite easy) and imported some R-header. This could be opened in R with dyn.load(dll-file) und executed with .C()!!


Thanks for your help!!

PS: I've taken the Files of my (Dev C++ -)project behind:


dllmain.c:
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
          #include <R.h>
          #include <Rinternals.h>
          #include <R_ext/Rdynload.h>
         
          void
          R_init_mylib(DllInfo *info)
          {
            /* Register routines, allocate resources. */
          }
         
          void
          R_unload_mylib(DllInfo *info)
          {
            /* Release resources. */
          }

 
/* useC1.c                                    */
/* Calling C with an integer vector using .C  */  

DLLIMPORT void useC(int *i) {
    i[3] = 11;  //This can be used in R!!
}


DLLIMPORT void HelloWorld ()
{
    //This can be used in R!!
    MessageBox (0, "Hello World from DLL!\n", "Hi", MB_ICONINFORMATION);
}




BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}



dll.h:
#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */


DLLIMPORT void HelloWorld (void);
DLLIMPORT void useC(int *i);

#endif /* _DLL_H_ */