Martin Olivier wrote:> Hi all, > > I would like to call a C program from R function. I tried to use the > .C() function > without success. I need a very simple example (such as the hello > program) to understandDo you want to call a C program (executable) or a shared library? If you want to run a compiled program simply use system() or shell()> how it works. Could you give a such example? > (I use the 1.7.1 Version of R with linux) > > Does it work in the case of a C++ program instead of a C programm?If you want to call a shared library (or DLL), please look into the Manual "writing R extensions" and look for the "convolve example". For a very simple "hello-function" see the "C++" example below. Please check if you have installed the correct compiler on Linux and look into the manual how to compile a shared library. On Linux it is: R CMD SHLIB test.cpp Thomas P. ----------------------------------------------------------- A simple example, but there are better in the docs ;-) e.g. building a package. ==========================================================// File test.cpp: #include <R.h> #include <Rinternals.h> extern "C" { void testarr(int* n, double* x) { for (int i = 0; i < *n; i++) { x[i] = 2 * i; } } } ==========================================================## file test.R dyn.load("test.so") testarr <- function(x) { .C("testarr", as.integer(length(x)), x=as.double(x))$x } print(testarr(1:10)) dyn.unload("test.so")
On Thu, 28 Aug 2003, Martin Olivier wrote:> Hi all, > > I would like to call a C program from R function. I tried to use the > .C() function > without success. I need a very simple example (such as the hello > program) to understand > how it works. Could you give a such example?There is one in Writing R Extensions. `Hello world' be too simple: you do need to know how to return results.> (I use the 1.7.1 Version of R with linux) > > Does it work in the case of a C++ program instead of a C programm?See Writing R extensions for details and examples. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hi all, I would like to call a C program from R function. I tried to use the .C() function without success. I need a very simple example (such as the hello program) to understand how it works. Could you give a such example? (I use the 1.7.1 Version of R with linux) Does it work in the case of a C++ program instead of a C programm? Best regards, Olivier -- ------------------------------------------------------------- Martin Olivier INRA - Unit? prot?omique LIRMM - IFA/MAB 2, Place Viala 161, rue Ada F-34060 Montpellier C?dex 1 34392 Montpellier C?dex 5 Tel : 04 99 61 26 02 Tel : O4 67 41 86 71 martinol at ensam.inra.fr