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