Hi, I'm trying to build a simple R package 'helloWorld' with just one function that prints 'hello World' on the C side. I agree that it is completely useless, but I just start mixing R and C. My C file is as follows : #include <stdio.h> void helloWorld() { printf("hello world !\n") ; } When I call it from R, here is what happens : R> .C("helloWorld", PACKAGE = "helloWorld") hello world ! list() is it normal that 'list()' is printed ? Thanks. Romain -- visit the R Graph Gallery : http://addictedtor.free.fr/graphiques mixmod 1.7 is released : http://www-math.univ-fcomte.fr/mixmod/index.php +---------------------------------------------------------------+ | Romain FRANCOIS - http://francoisromain.free.fr | | Doctorant INRIA Futurs / EDF | +---------------------------------------------------------------+
See the `Value' section of ?.C. Also, it's better to use the i/o provided by the R API; i.e., something like: #include "R.h" void helloworld() { Rprintf("Hello world!\n"); } Andy From: Romain Francois> > Hi, > > I'm trying to build a simple R package 'helloWorld' with just one > function that prints 'hello World' on the C side. > I agree that it is completely useless, but I just start > mixing R and C. > > My C file is as follows : > > #include <stdio.h> > void helloWorld() { > printf("hello world !\n") ; > } > > When I call it from R, here is what happens : > R> .C("helloWorld", PACKAGE = "helloWorld") > hello world ! > list() > > is it normal that 'list()' is printed ? > > Thanks. > > Romain > > -- > visit the R Graph Gallery : http://addictedtor.free.fr/graphiques > mixmod 1.7 is released : > http://www-math.univ-> fcomte.fr/mixmod/index.php > > > +---------------------------------------------------------------+ > | Romain FRANCOIS - http://francoisromain.free.fr | > | Doctorant INRIA Futurs / EDF | > +---------------------------------------------------------------+ > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >
On Thu, 12 Jan 2006, Romain Francois wrote:> Hi, > > I'm trying to build a simple R package 'helloWorld' with just one > function that prints 'hello World' on the C side. > I agree that it is completely useless, but I just start mixing R and C. > > My C file is as follows : > > #include <stdio.h> > void helloWorld() { > printf("hello world !\n") ; > } > > When I call it from R, here is what happens : > R> .C("helloWorld", PACKAGE = "helloWorld") > hello world ! > list() > > is it normal that 'list()' is printed ?Yes. That is the return value of .C(). (It is not normal to call .C() at the toplevel, rather as part of a function.) The value section of the help page says The functions '.C' and '.Fortran' return a list similar to the '...' list of arguments passed in, but reflecting any changes made by the C or Fortran code. You have no ... args, so get an empty list. -- 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
On 1/12/2006 10:46 AM, Romain Francois wrote:> Hi, > > I'm trying to build a simple R package 'helloWorld' with just one > function that prints 'hello World' on the C side. > I agree that it is completely useless, but I just start mixing R and C. > > My C file is as follows : > > #include <stdio.h> > void helloWorld() { > printf("hello world !\n") ; > } > > When I call it from R, here is what happens : > R> .C("helloWorld", PACKAGE = "helloWorld") > hello world ! > list() > > is it normal that 'list()' is printed ?Yes, because that is the return value from .C. If you don't want to print it, you could call invisible(.C( ... )) or, more likely, you'd embed this call in a function that produced its own return value after calling .C(). By the way, you should call Rprintf() rather than printf(), if you want your function to work in environments like Windows Rgui. See the Writing R Extensions manual for the details. Duncan Murdoch