# [Rcpp-devel] question from a novice

Søren Højsgaard sorenh at math.aau.dk
Tue Mar 18 16:15:45 CET 2014

```Don't know, but the code below seems to work.
Cheers
Søren

#include <math.h>       /* log */
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double LRTrow(IntegerVector x)
{
LogicalVector z = x>0;
IntegerVector y = x[z];
double lrtc = sum(y);
if (lrtc>0) lrtc = - lrtc*log(lrtc);
for (int i=1; i<= y.size(); i++)
lrtc += y[i-1]*log(y[i-1]);
return lrtc;
}

/*** R

LRTrow(c(2,1,0))

*/

From: rcpp-devel-bounces at lists.r-forge.r-project.org [mailto:rcpp-devel-bounces at lists.r-forge.r-project.org] On Behalf Of David Edwards
Sent: 18. marts 2014 15:51
To: 'rcpp-devel at lists.r-forge.r-project.org'
Subject: [Rcpp-devel] question from a novice

Sorry to bother you with a novice question, but I can't figure out why, when I try to sourceCpp (with verbose=TRUE) the code

#include <math.h>       /* log */
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double LRTrow(IntegerVector x)
{
IntegerVector y;
y = x[x>0];
double lrtc = sum(y);
if (lrtc>0) lrtc = - lrtc*log(lrtc);
for (int i=1; i<= y.size(); i++) lrtc += y[i-1]*log(y[i-1]);
return lrtc;
}

I get the error message

C:/PROGRA~1/R/R-30~1.2/library/Rcpp/include/Rcpp/internal/wrap.h:525:11: error: cannot convert 'const Rcpp::SubsetProxy<13, Rcpp::PreserveStorage, 10, true, Rcpp::sugar::Comparator_With_One_Value<13, Rcpp::sugar::greater<13>, true, Rcpp::Vector<13, Rcpp::PreserveStorage> > >' to 'SEXP' in initialization
make: *** [temp.o] Error 1

What am I doing wrong?

Best regards
David Edwards

sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Danish_Denmark.1252  LC_CTYPE=Danish_Denmark.1252    LC_MONETARY=Danish_Denmark.1252 LC_NUMERIC=C
[5] LC_TIME=Danish_Denmark.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] gRapfa_1.0   igraph_0.7.0 Rcpp_0.11.1

loaded via a namespace (and not attached):
[1] tools_3.0.1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140318/734e4e69/attachment-0001.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ded.cpp
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140318/734e4e69/attachment-0001.ksh>
```