diff --git a/inst/include/RcppArmadilloAs.h b/inst/include/RcppArmadilloAs.h index 37cc8e0e..7ae27111 100644 --- a/inst/include/RcppArmadilloAs.h +++ b/inst/include/RcppArmadilloAs.h @@ -30,12 +30,29 @@ namespace traits { class Exporter< arma::field > { public: Exporter(SEXP x) : data(x){} - inline arma::field get() { - size_t n = data.size() ; - arma::field out( n ) ; - for(size_t i=0; i(data[i]) ; + size_t n = data.size(); + arma::field out(n); + # if defined(RCPP_ARMADILLO_FIX_FieldExporter) + if(!Rf_isNull(data.attr("dim"))){ + arma::ivec dims = data.attr("dim"); + if (dims.n_elem == 1) + { + out.set_size(n); + } + else if (dims.n_elem == 2) + { + out.set_size(dims(0), dims(1)); + } + else if (dims.n_elem == 3) + { + out.set_size(dims(0), dims(1), dims(2)); + } + } + # endif + for (size_t i = 0; i < n; i++) + { + out(i) = as(data[i]); } return out ; } diff --git a/inst/include/RcppArmadilloConfig.h b/inst/include/RcppArmadilloConfig.h index 49090950..fe4e2341 100644 --- a/inst/include/RcppArmadilloConfig.h +++ b/inst/include/RcppArmadilloConfig.h @@ -135,5 +135,14 @@ //#define RCPP_ARMADILLO_RETURN_ROWVEC_AS_VECTOR //#define RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR +// To preserve all dims of arma::field when passing to R the following macro +// can be defined before including RcppArmadillo.h. +// see https://github.com/RcppCore/RcppArmadillo/pull/352 +// #define RCPP_ARMADILLO_FIX_FieldImporter + +// To preserve all dims of and arma::field input argument when passing to C++ +// the following macro can be defined before including RcppArmadillo.h. +// see https://github.com/RcppCore/RcppArmadillo/pull/352 +// #define RCPP_ARMADILLO_FIX_FieldExporter #endif diff --git a/inst/include/RcppArmadilloWrap.h b/inst/include/RcppArmadilloWrap.h index 42ec8932..b290da67 100644 --- a/inst/include/RcppArmadilloWrap.h +++ b/inst/include/RcppArmadilloWrap.h @@ -149,7 +149,11 @@ namespace Rcpp{ template SEXP wrap( const arma::field& data){ RObject x = wrap( RcppArmadillo::FieldImporter( data ) ) ; - x.attr("dim" ) = Dimension( data.n_rows, data.n_cols ) ; + # if defined(RCPP_ARMADILLO_FIX_FieldImporter) + x.attr("dim" ) = Dimension( data.n_rows, data.n_cols, data.n_slices ) ; + #else + x.attr("dim" ) = Dimension( data.n_rows, data.n_cols ) ; + #endif return x ; } diff --git a/inst/tinytest/test_rcpparmadillo.R b/inst/tinytest/test_rcpparmadillo.R index 59cd1b1b..e286ee3e 100644 --- a/inst/tinytest/test_rcpparmadillo.R +++ b/inst/tinytest/test_rcpparmadillo.R @@ -37,6 +37,7 @@ expect_equal( res[[2]][[2]], matrix(0, ncol = 5, nrow=1))#, msg = "zeros(5 expect_equal( res[[3]][[1]], matrix(0, ncol = 1, nrow=5))#, msg = "zeros(1,5)" ) expect_equal( res[[3]][[2]], matrix(0, ncol = 1, nrow=5))#, msg = "zeros(1,5)" ) +# TODO: Revisit field tests later and fix see https://github.com/RcppCore/RcppArmadillo/pull/352 expect_equal( res[[4]][[1]], matrix(0:3, ncol = 2, nrow=2))#, msg = "field" ) expect_equal( res[[4]][[2]], matrix(letters[1:4], ncol = 2, nrow=2))#, msg = "field" ) diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 35eaa69b..024c6bb7 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -6,6 +6,11 @@ using namespace Rcpp; +#ifdef RCPP_USE_GLOBAL_ROSTREAM +Rcpp::Rostream& Rcpp::Rcout = Rcpp::Rcpp_cout_get(); +Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); +#endif + // armadillo_version Rcpp::IntegerVector armadillo_version(bool single); RcppExport SEXP _RcppArmadillo_armadillo_version(SEXP singleSEXP) {