hello World problem

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

hello World problem

Romain François
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                                  |
+---------------------------------------------------------------+

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: hello World problem

Liaw, Andy
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                                  |
> +---------------------------------------------------------------+
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: hello World problem

Prof Brian Ripley
In reply to this post by Romain François
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,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: hello World problem

Duncan Murdoch
In reply to this post by Romain François
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel