如何在Rcpp中打印原始值

F. *_*ivé 4 r rcpp

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
void print_raw(RawVector x) {

  for (int i = 0; i < x.size(); i++) {
    Rcout << x[i] << " ";
  }
  Rcout << std::endl;
}

/*** R
x <- as.raw(0:10)
print(x)
print_raw(x)
*/
Run Code Online (Sandbox Code Playgroud)

我希望Rcpp以与R相同的方式打印"raw"类型的值.可能吗?使用当前代码,我只得到一个空行.

Kon*_*lph 6

您需要先将各个值转换为int1.此外,为了获得十六进制,零填充输出,您需要使用<iomanip>函数.

使用范围for循环,转换可以在循环变量的初始化中隐式发生:

// [[Rcpp::export]]
void print_raw(RawVector x) {
  for (int v : x) {
    Rcout << std::hex << std::setw(2) << std::setfill('0') << v << ' ';
  }
  Rcout << '\n';
}
Run Code Online (Sandbox Code Playgroud)

1Rbyte,这是一个typedefunsigned char.