[Rcpp-devel] Problems with Rcout

Iñaki Ucar iucar at fedoraproject.org
Tue Nov 27 17:16:07 CET 2018


On Tue, 27 Nov 2018 at 17:06, Barth Riley <barthriley at comcast.net> wrote:
>
> Here is a more complete example. Note that I want to output text strings for debugging purposes as the code for treatAsVector = true is never executed.

The contents of the function are irrelevant. If I compile and execute
this in a clean session, it works fine. I.e.:

Rcpp::cppFunction('
NumericVector getValidCount(Rcpp::NumericMatrix m,
                            bool treatAsVector) {

  Rcpp::Rcout << "getValidCount BEGINS" << std::endl;

  int N = m.cols();
  NumericVector u, vec;
  NumericVector count (N);

  if(!treatAsVector) {
    Rcpp::Rcout << "Treating as matrix" << std::endl;
    for(int i = 0; i < N; i++) {
      vec = m(_,i);
      vec = vec[!Rcpp::is_na(vec)];
      u = Rcpp::unique(vec);
      count[i] = u.length();
    }
  } else {
    Rcpp::Rcout << "treating as vector" << std::endl;
    vec = as<NumericVector>(m);
    vec = vec[!Rcpp::is_na(vec)];
    u = Rcpp::unique(vec);

    count.fill(u.length());
  }

  return count;
}
')

getValidCount(matrix(), FALSE)
#> getValidCount BEGINS
#> Treating as matrix
#> [1] 0

So where are you calling this? Are you redirecting the stdout? Are you
parallelising, are you calling this inside a subprocess?

Iñaki


More information about the Rcpp-devel mailing list