1
R 함수를 Rcpp로 변환하려고합니다. 간단한 테스트 코드는 다음과 같습니다. 그러나 설정 한 인수를 처리하는 방법을 모르겠습니다. 기본적으로 NULL입니다.Rcpp : Rcpp :: Nullable NumericVector의 크기를 얻는 방법
test<- function(t=NULL,tmax=NULL,tmin=NULL){
if(is.null(t)){
yout=(tmax-tmin)*(tmin+tmax)
}else{
yout=2*t
}
return(yout)
}
test(tmax=1:3,tmin=0:2)
// [[Rcpp::export]]
NumericVector cpptest(Rcpp::Nullable<Rcpp::NumericVector> t=R_NilValue,
Rcpp::Nullable<Rcpp::NumericVector> tmax=R_NilValue,
Rcpp::Nullable<Rcpp::NumericVector> tmin=R_NilValue){
int N=0;
if(t.isNotNull()) {
N=t.size(); /* which show a error*/
}else{
N=tmax.size(); /* which show a error*/
}
NumericVector yout=NumericVector(N);
if(t.isNotNull()) {
for(i=0;i<N,i++){
yout[i]=2*t[i]
}
}else{
for(i=0;i<N,i++){
yout[i]=(tmax[i]-tmin[i])*(tmin[i]+tmax[i])
}
}
return(yout)
}
정답과 형식에 대한 정확한 힌트. 이것은 단지 당신이 원하는 크기의 _instantiation_이 부족했다. 나는 실제로 [이 간단한 형태] (https://github.com/aliceyiwang/mvabund/blob/master/src/Rinterface.cpp#L52-L53)를 선호합니다. 'NumericVector'와 다른'SEXP' 호환 타입에서도 똑같이 작동합니다. –