Discussion:
[Rcpp-devel] Problems with Rcout
Barth Riley
2018-11-27 15:35:10 UTC
Permalink
Dear Rcppers

I am encountering a problem with Rcout. Even with basic string output, when I run the function containing the call to Rcout, no output is generated. Here is a simple example of what I’m trying to do:

// [[Rcpp::export]]
void testFunc() {
Rcpp::Rcout << "testFunc begins" << std:endl;
. . .
}

My code is part of a package I’m developing, using R 3.51 and Rcpp 0.12.19. The Rcpp code compiles without a problem.

Thanks

Barth
Iñaki Ucar
2018-11-27 15:50:13 UTC
Permalink
Post by Barth Riley
Dear Rcppers
// [[Rcpp::export]]
void testFunc() {
Rcpp::Rcout << "testFunc begins" << std:endl;
. . .
}
Note that it should be "std::endl", with double colon. Assuming this
is a transcription error, you'll have to provide more context (and,
ideally, some kind of reproducible example), because this works just
fine.

Iñaki
Barth Riley
2018-11-27 16:06:47 UTC
Permalink
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.

Barth

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;
}
Iñaki Ucar
2018-11-27 16:16:07 UTC
Permalink
Post by Barth Riley
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
Barth Riley
2018-11-27 16:21:57 UTC
Permalink
Thanks for your reply. No, I’m not calling this using parallelization, and to my knowledge at least I have not redirected stdout (how would I check this?). The function is meant to be called by another function, but I am testing it directly from the R console.

Barth
Iñaki Ucar
2018-11-27 16:31:31 UTC
Permalink
Thanks for your reply. No, I’m not calling this using parallelization, and to my knowledge at least I have not redirected stdout (how would I check this?). The function is meant to be called by another function, but I am testing it directly from the R console.
Is this publicly available somewhere? If not, you could write a
script, or a dummy package if that's not possible, so that we can
execute it and reproduce the issue. Otherwise, we cannot help you,
because the function and your Rcout usage is fine.

Iñaki
Jeff Newmiller
2018-11-27 16:38:46 UTC
Permalink
Time for you to document your working environment, Barth. Things that occur to me are the outputs of sessionInfo() and and system2( "gcc", "--version").
Thanks for your reply. No, I’m not calling this using parallelization,
and to my knowledge at least I have not redirected stdout (how would I
check this?). The function is meant to be called by another function,
but I am testing it directly from the R console.
Barth
--
Sent from my phone. Please excuse my brevity.
Serguei Sokol
2018-11-27 15:51:28 UTC
Permalink
Post by Barth Riley
Dear Rcppers
I am encountering a problem with Rcout. Even with basic string output,
when I run the function containing the call to Rcout, no output is
// [[Rcpp::export]]
void testFunc() {
           Rcpp::Rcout << "testFunc begins" << std:endl;
           . . .
}
This example works for me:

library(Rcpp)
sourceCpp(code="#include <Rcpp.h>\n// [[Rcpp::export]]\nvoid testFunc()
{\nRcpp::Rcout << \"testFunc begins\" << std::endl;\n}")
testFunc()
#testFunc begins

May be in your session you have redirected stdout elsewhere?

Serguei.
Post by Barth Riley
My code is part of a package I’m developing, using R 3.51 and Rcpp
0.12.19. The Rcpp code compiles without a problem.
Thanks
Barth
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Loading...