I'm quite new to programming with Rcpp so I'm trying out new things to see how everything works. I wrote a small programm to compare two NumericVectors with the match() function. I also wanted to print out the input Vectors and the Output but it doesn't seem to work since I don't get the entries of the Vectors back but the storage place (or something like that). I haven't found any kind of "print" function for NumericVectors but maybe there's another way? Any help will be appreciated.
This is my code:
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
IntegerVector vergl(NumericVector eins, NumericVector zwei){
 IntegerVector out = match(eins, zwei);
 cout << eins << endl;
 cout << zwei << endl;
 cout << match(eins, zwei) << endl;
 return out;
 }
A small example:
vergl(c(1,2,3),c(2,3,4))
The output:
> vergl(c(1,2,3),c(2,3,4))
0xa1923c0
0xa192408
0xb6d7038
[1] NA  1  2
Thank you.
Read gallery.rcpp.org/articles/using-rcout .
#include <RcppArmadillo.h>   
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector vergl(NumericVector eins, NumericVector zwei){
 IntegerVector out = match(eins, zwei);
 Rcout << as<arma::rowvec>(eins) << std::endl;
 Rcout << as<arma::rowvec>(zwei) << std::endl;
 Rcout << as<arma::rowvec>(out) << std::endl;
 return out;
}
In R:
vergl(c(1,2,3),c(2,3,4))
#   1.0000   2.0000   3.0000
#
#   2.0000   3.0000   4.0000
#
#      nan   1.0000   2.0000
#
#[1] NA  1  2
                        Try this Rf_PrintValue(eins); or Function print("print"); print(eins); or similar as shown in the display function in the example.
For example, assuming this is in Rf_PrintValue.cpp:
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
IntegerVector vergl(NumericVector eins, NumericVector zwei) {
 IntegerVector out = match(eins, zwei);
 Rf_PrintValue(eins);
 Rf_PrintValue(zwei);
 Rf_PrintValue(match(eins, zwei));
 Function print("print");
 print(out);
 Function display("display");
 display("out", out);
 return out;
}
/*** R
display <- function(name, x) { cat(name, ":\n", sep = ""); print(x) }
vergl(c(1,2,3),c(2,3,4))
*/
we can run it like this.  The first three output vectors come from the Rf_PrintValue statements, the fourth from the print statement and the fifth is from the display function and the last is the output of the verg1 function:
> library(Rcpp)
> sourceCpp("Rf_PrintValue.cpp")
> display <- function(name, x) { cat(name, ":\n", sep = ""); print(x) }
> vergl(c(1,2,3),c(2,3,4))
[1] 1 2 3
[1] 2 3 4
[1] NA  1  2
[1] NA  1  2
out:
[1] NA  1  2
[1] NA  1  2
Revised Changed the second solution and added an example.  Also added display to example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With