On 27 April 2022 at 20:58, Roberto Spadim wrote:
| Hello folks,
| I'm converting some R functions to RCpp at
| https://github.com/OVVO-Financial/NNS/
| 
| My problem is with a R "cast" function
| 
| 
https://github.com/OVVO-Financial/NNS/blob/d3f37c6f217a41ce15b31f7a840b6b1ad63e5f36/R/Partial_Moments.R#L20
| 
| basically this line:
| 
| ---
| LPM <- function(degree, target, variable){
| if(any(class(variable)%in%c("tbl","data.table"))) variable <-
| as.vector(unlist(variable))
| ...
| }
| ---
| 
| My Rcpp version don't accept data.table type, it return an error of
| type not allow:
| 
| Error in LPM(2, 0, tail(appended_returns, 1)) :
|   Not compatible with requested type: [type=list; target=double].
| 
| Is there some way to accept a "anytype" at Rcpp, and convert it to
| numericvector when need?

In a way. We have a more-or-less FAQ about how to accomodate 'different
run-time types' (think: int vector vs double vector) at compile time. The
easiest is to use a SEXP which *is* already your "anytype" as it (most
literally) covers "any" type in R.  See for example

   https://gallery.rcpp.org/articles/rcpp-wrap-and-recurse/

which shows the basic pattern of switching on TYPEOF().

Otherwise, a tip: decompose compound expressions

 if(any(class(variable)%in%c("tbl","data.table"))) variable <- 
as.vector(unlist(variable))

into more basic assignments (on the C++ side).  We are getting some wonderful
help from the compiler but the template meta-programming driving this is
finicky. Do NOT assume you can stack expressions as you might in R. No shame
in doing multiple basic expressions -- the compiler (once it knows what to
do) may well optimise temporaries away.

Dirk

| My Rcpp version is:
| 
| 
https://github.com/OVVO-Financial/NNS/blob/NNS-Beta-Version/src/partial_moments_rcpp.cpp#L68
| 
| and fucntion signature is:
| NumericVector LPM_CPv(const double degree, const NumericVector target,
| const NumericVector variable) {
| 
| i think i should use SEXP as "anytype" and at function body check if I
| should "cast" to NumericVector
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel@lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to