[Rcpp-devel] Simplest Class Example For Starters

ivo welch ivo.welch at gmail.com
Fri May 31 03:08:30 CEST 2013


hi dirk---it is indeed excellent.  alas, it doesn't cover my confusion.

*general*: I now realize my first problem with the print function.  I need
to double-escape '\'.
   cppFunction('int hello(int i) { printf("hello, world\n"); }')
is wrong.  it needs to be
   cppFunction('int hello(int i) { printf("hello, world\\n"); }')
gotcha!

*
S2.9*: the cppFunction works without the attributes in this examples.  what
determines whether one needs to declare attributes vs. Rcpp being smart
enough to figure it out itself?  in any case, this works:

library( Rcpp )

cppFunction('
struct silly { int a; double *b; };
// [[Rcpp::export]]
long initme(int i) {
  silly *s= new silly;
  s->a=i; s->b=new double[i+1];
  return (long) s;
}
')

cat("Compiled OK\n")
x <- initme(5)


although this works, I still get a warning.

   No function found for Rcpp::export attribute at file284a5ab1f09.cpp:5

but R has picked up the initme() function.  it's a good start.

I presume long is the wrong return type.  (void*) did not work.  what is
the recommended pointer type?

And somehow I need to figure out how to tell R to call a destructor when
the x object goes out of scope.


I don't want to take your time any longer, so I think it is better for me
to give up here.

regards,

/iaw
----
Ivo Welch (ivo.welch at gmail.com)


On Thu, May 30, 2013 at 3:39 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

>
> On 30 May 2013 at 15:08, ivo welch wrote:
> |
> | thx, dirk.  first, apologies for multiposting earlier.  I thought it did
> not
> | allow me to post from my non-registered email address.  second,
> apologies for
> | the private email.  I wasn't expecting any answers.  just wanted to help
> with
> | some feedback.  I will buy your Rcpp book when it comes out.
> |
> | I think you are correct that modules are too tough for me.  heck, I
> don't even
> | write R packages.  I am just an end user.  I would have to learn how to
> write
> | and build those first.  so, I think I will want to go the accessor
> function
> | route.  can cppFunction() allow me to define structures?  I am just
> trying
> | something super-simple here:
> |
> |
> |     library( Rcpp )
> |     cppFunction('
> |     struct Silly {
> |       double *v; int k;
> |     };
> |     void hello(int i) { printf("hello, world\n"); }
> |     ')
> |     cat("Compiled OK\n")
> |
> |
> | but it fails, both on the printf and on the structure definition.  I am
> | wondering whether cppFunction can just handle single functions and
> without
> | quotation marks.
>
> You really should consider reading the excellent vignette 'Rcpp
> attributes'.
>
> Dirk
>
> |
> | regards,
> |
> | /iaw
> |
> | ----
> | Ivo Welch (ivo.welch at gmail.com)
> |
> |
> |
> | On Thu, May 30, 2013 at 10:02 AM, Dirk Eddelbuettel <edd at debian.org>
> wrote:
> |
> |
> |     Ivo,
> |
> |     Appreciate that you post here.  Am a little pressed for time but in a
> |     nutshell, you missed that the new (and very nice) Rcpp Attributes
> which you
> |     use for sourceCpp do NOT currently mix with Rcpp Modules.
> |
> |     So either concentrate on easily writing accessor function, OR
> concentrate
> |     on
> |     using Modules (which is not th easiest topic to start with). You
> cannot mix
> |     and match.
> |
> |     As for simplest examples: they are eg in the package itself, look at
> |
> |        /usr/local/lib/R/site-library/Rcpp/unitTests/testRcppModule/
> |
> |     which provides a small but complete working package using modules.
> |
> |     Hope this helps,  Dirk
> |
> |     --
> |     Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> |
> |
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130530/54b59efd/attachment.html>


More information about the Rcpp-devel mailing list