how can I handle vectors without knowing the type in Rcpp

前端 未结 2 1154
你的背包
你的背包 2020-12-05 11:44

I want to replicate the following R function in Rcpp:

fR = function(x) x[1:2]

fR(c(1,2,3))
#[1] 1 2
fR(c(\'a\',\'b\',\'c\'))
#[1] \"a\" \"b\"
<         


        
2条回答
  •  不思量自难忘°
    2020-12-05 12:16

    You need to pick a type (ie do not use signature="SEXP" [ oh and you should look into Attributes anyway ]).

    Or you keep the SEXP type, and dispatch internally. See for example this post on the Rcpp Gallery.

    Edit: And C is of course statically typed. These very switches depending on the type are all over the R sources too. No free lunch here.

提交回复
热议问题