代码之家  ›  专栏  ›  技术社区  ›  F. Privé

如何在RCPP中打印原始值

  •  4
  • F. Privé  · 技术社区  · 7 年前
    #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)
    */
    

    我希望rcpp以与r相同的方式打印“raw”类型的值。 有可能吗?使用当前代码,我只得到一个空行。

    2 回复  |  直到 7 年前
        1
  •  6
  •   Konrad Rudolph    7 年前

    int <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';
    }
    

    Rbyte which is a typedef for unsigned char

        2
  •  4
  •   Dirk is no longer here    7 年前

    print()

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    void print_raw(RawVector x) {
      print(x);
    }
    
    /*** R
    x <- as.raw(0:10)
    print(x)
    print_raw(x)
    */
    

    R> sourceCpp("/tmp/so51169994.cpp")
    
    R> x <- as.raw(0:10)
    
    R> print(x)
     [1] 00 01 02 03 04 05 06 07 08 09 0a
    
    R> print_raw(x)
     [1] 00 01 02 03 04 05 06 07 08 09 0a
    R>