[Rcpp-devel] Rcpp-devel Digest, Vol 82, Issue 13

Fatima Batool gbatoolfatima at gmail.com
Mon Aug 29 18:42:21 CEST 2016


replacing macros with functions has make the task completion quite simple
and easy. Thank you! and I think its still a good idea to have a look some
where for calling C++ macros in R for learning. One small think I want to
ask here is

I want too keep "object" as a function formal parameter while keeping fun
type as void and do not want to return it as return object. Because in my
function there are other returning values and from this function pass its
output to some other function. Therefore, now If I try this

#include <Rcpp.h>


using namespace Rcpp;


NumericVector fillVector(NumericVector v, double amount) {

    for (int i=0; i<v.size(); i++)

        v(i) = amount;

    return v;

}


// [[Rcpp::export]]

void fun(double fillamount, NumericVector array1, double object) {

    array1 = fillVector(array1, 1.1);

    for (int i=0; i<array1.size(); i++)

        object += array1(i);

    return;

}


/***R
fun(1.1, 1:5, 0.0)
*/
I'm getting nothing as output.

Regards
Fatima

On Mon, Aug 29, 2016 at 11:00 AM, <
rcpp-devel-request at lists.r-forge.r-project.org> wrote:

> Send Rcpp-devel mailing list submissions to
>         rcpp-devel at lists.r-forge.r-project.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://lists.r-forge.r-project.org/cgi-bin/mailman/
> listinfo/rcpp-devel
>
> or, via email, send a message with subject or body 'help' to
>         rcpp-devel-request at lists.r-forge.r-project.org
>
> You can reach the person managing the list at
>         rcpp-devel-owner at lists.r-forge.r-project.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Rcpp-devel digest..."
>
>
> Today's Topics:
>
>    1. Re: Calling C++ Macros in R using Rcpp (Dirk Eddelbuettel)
>    2. Re: Calling C++ Macros in R using Rcpp (Dirk Eddelbuettel)
>    3. Re: Calling C++ Macros in R using Rcpp (Dale Smith)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 28 Aug 2016 08:45:57 -0500
> From: Dirk Eddelbuettel <edd at debian.org>
> To: Fatima Batool <gbatoolfatima at gmail.com>
> Cc: rcpp-devel at lists.r-forge.r-project.org
> Subject: Re: [Rcpp-devel] Calling C++ Macros in R using Rcpp
> Message-ID: <22466.60181.651504.191769 at max.nulle.part>
> Content-Type: text/plain; charset=us-ascii
>
>
> Fatima,
>
> You are writing very much like a C programmer. While C and C++ pretty much
> get along, we have conflicts here over the macro use --- we make extensive
> use of templates, and the cost of that is that some structures (which may
> be
> valid, but really are no longer recommended style) conflict.
>
> That happens here. Let me offer two different solutions.
>
> The first simply splits your macro off into an initializer function and
> calls
> it.  We have
>
> ------------------------------------------------------------
> -----------------
> #include <Rcpp.h>
>
> using namespace Rcpp;
>
> NumericVector fillVector(NumericVector v, double amount) {
>     for (int i=0; i<v.size(); i++)
>         v(i) = amount;
>     return v;
> }
>
> // [[Rcpp::export]]
> double fun(double fillamount, NumericVector array1) {
>     array1 = fillVector(array1, 1.1);
>     double object = 0.0;
>     for (int i=0; i<array1.size(); i++)
>         object += array1(i);
>     return object;
> }
> ------------------------------------------------------------
> -----------------
>
> Very straightforward: we pass a vector and an amount, and set the amount in
> each element. (But see below.)
>
> We then use that in function fun() to initialize the vector, after which we
> loop over it and summarize.
>
> But there is more.
>
> Initializing a vector with a constant in each element is so common that we
> have constructor just for that.  And summing each element is so common that
> the sugar function sum() does just that.  So now it all is just this
> two-liner:
>
> ------------------------------------------------------------
> -----------------
> // [[Rcpp::export]]
> double simplerfun(double fillamount, int n) {
>     NumericVector arr(n, fillamount);
>     return sum(arr);
> }
> ------------------------------------------------------------
> -----------------
>
> When I run this I get:
>
>
> ------------------------------------------------------------
> -----------------
>
> R> sourceCpp("/tmp/rcppdevel20160828.cpp")
>
> R> fun(1.1, 1:5)
> [1] 5.5
>
> R> simplerfun(1.1, 5)
> [1] 5.5
> R>
>
> ------------------------------------------------------------
> -----------------
>
> As expected.  Hope this helps.  I re-include the whole file below.
>
> Dirk
>
>
> ------------------------------------------------------------
> -----------------
>
> #include <Rcpp.h>
>
> using namespace Rcpp;
>
> NumericVector fillVector(NumericVector v, double amount) {
>     for (int i=0; i<v.size(); i++)
>         v(i) = amount;
>     return v;
> }
>
> // [[Rcpp::export]]
> double fun(double fillamount, NumericVector array1) {
>     array1 = fillVector(array1, 1.1);
>     double object = 0.0;
>     for (int i=0; i<array1.size(); i++)
>         object += array1(i);
>     return object;
> }
>
>
> /*** R
> fun(1.1, 1:5)
> */
>
>
>
> // [[Rcpp::export]]
> double simplerfun(double fillamount, int n) {
>     NumericVector arr(n, fillamount);
>     return sum(arr);
> }
>
> /*** R
> simplerfun(1.1, 5)
> */
>
> ------------------------------------------------------------
> -----------------
>
> --
> http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
>
>
> ------------------------------
>
> Message: 2
> Date: Sun, 28 Aug 2016 08:58:42 -0500
> From: Dirk Eddelbuettel <edd at debian.org>
> To: Dirk Eddelbuettel <edd at debian.org>
> Cc: rcpp-devel at lists.r-forge.r-project.org, Fatima Batool
>         <gbatoolfatima at gmail.com>
> Subject: Re: [Rcpp-devel] Calling C++ Macros in R using Rcpp
> Message-ID: <22466.60946.899249.432040 at max.nulle.part>
> Content-Type: text/plain; charset=us-ascii
>
>
> Fatima,
>
> Oh, and just to clarify:  Macros are still possible.  In fact, if you study
> the Rcpp source code you will find _extensive_ use of them.  But as a
> matter
> of programming style I would recommend avoiding them in top-level
> user-facing
> code.
>
> Dirk
>
> --
> http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
>
>
> ------------------------------
>
> Message: 3
> Date: Sun, 28 Aug 2016 13:46:02 -0400
> From: Dale Smith <dtsmith at mindspring.com>
> To: Dirk Eddelbuettel <edd at debian.org>
> Cc: rcpp-devel at lists.r-forge.r-project.org, Fatima Batool
>         <gbatoolfatima at gmail.com>
> Subject: Re: [Rcpp-devel] Calling C++ Macros in R using Rcpp
> Message-ID: <29A23146-8D5C-480C-935A-A8C3C24A94C0 at mindspring.com>
> Content-Type: text/plain;       charset=us-ascii
>
> Agree about macros.
>
> Sent from my iPad
>
> > On Aug 28, 2016, at 9:58 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
> >
> >
> > Fatima,
> >
> > Oh, and just to clarify:  Macros are still possible.  In fact, if you
> study
> > the Rcpp source code you will find _extensive_ use of them.  But as a
> matter
> > of programming style I would recommend avoiding them in top-level
> user-facing
> > code.
> >
> > Dirk
> >
> > --
> > http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
> > _______________________________________________
> > Rcpp-devel mailing list
> > Rcpp-devel at lists.r-forge.r-project.org
> > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
>
> ------------------------------
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
> End of Rcpp-devel Digest, Vol 82, Issue 13
> ******************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160829/730ef5e0/attachment.html>


More information about the Rcpp-devel mailing list