Во втором примере вы можете просто вызвать исходную функцию и присвоить ей List
, элементы которого можно получить по имени (или позиции):
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List max_argmax_cpp(NumericVector x){
double max = x[0];
int argmax = 0 + 1;
for(int i = 1; i < x.length(); i++){
if(x[i]>x[i-1]){
max = x[i];
argmax = i+1;
}
}
List Output;
Output["Max"] = max;
Output["Argmax"] = argmax;
return(Output);
}
// [[Rcpp::export]]
double max_only(NumericVector x){
List l = max_argmax_cpp(x);
double max = l["Max"];
return(max);
}
/*** R
set.seed(42)
x <- runif(100)
max_argmax_cpp(x)
max_only(x)
*/
Выход:
> set.seed(42)
> x <- runif(100)
> max_argmax_cpp(x)
$Max
[1] 0.7439746
$Argmax
[1] 99
> max_only(x)
[1] 0.7439746