如何设置 Rcpp::List 元素的类属性?

rma*_*gno 5 r rcpp

在下面的C++源文件中,在函数中glfw_get_monitors如何设置对象每个元素的类属性monitor_ptrs

\n\n

该行monitor_ptrs[i].attr("class") = "GLFWmonitor";抛出编译错误:

\n\n
\xe2\x80\x98Rcpp::Vector<19>::Proxy\xe2\x80\x99 {aka \xe2\x80\x98class Rcpp::internal::generic_proxy<19>\xe2\x80\x99} has no member named \xe2\x80\x98attr\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n\n

glfw_types.h

\n\n
#ifndef RCPP_GLFW_TYPES_H\n#define RCPP_GLFW_TYPES_H\n\n#include <Rcpp.h>\n#include <GLFW/glfw3.h>\n\nvoid glfw_destroy_monitor(GLFWmonitor*);\n\n// /sf/ask/2884741681/\n\ntypedef Rcpp::XPtr<GLFWwindow, Rcpp::PreserveStorage, glfwDestroyWindow> GLFWwindow_ptr;\ntypedef Rcpp::XPtr<GLFWmonitor, Rcpp::PreserveStorage, glfw_destroy_monitor> GLFWmonitor_ptr;\n\n\n#endif\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

C++源文件

\n\n
#include "glfw_types.h"\nusing namespace Rcpp;\n\n// [[Rcpp::export]]\nGLFWmonitor_ptr glfw_get_primary_monitor() {\n\n  GLFWmonitor_ptr new_monitor = GLFWmonitor_ptr(glfwGetPrimaryMonitor(), true);\n\n  new_monitor.attr("class") = "GLFWmonitor";\n\n  return new_monitor;\n}\n\n// [[Rcpp::export]]\nRcpp::List glfw_get_monitors() {\n\n  int nr_monitors;\n  GLFWmonitor** monitors = glfwGetMonitors(&nr_monitors);\n  Rcpp::List monitor_ptrs(nr_monitors);\n\n  for(int i = 0; i < nr_monitors; i++) {\n    monitor_ptrs[i] = GLFWmonitor_ptr((GLFWmonitor*)monitors[i], true);\n    monitor_ptrs[i].attr("class") = "GLFWmonitor";\n  }\n\n  return monitor_ptrs;\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

duc*_*ayr 5

这里的问题是你尝试分配班级时。考虑以下更简单的示例:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::List foo() {
    // Setup the list
    Rcpp::List result(1);
    // Setup the object that will go in the list
    Rcpp::IntegerVector x = Rcpp::seq(1, 10);
    // Your approach was to add it to the list, THEN set the class attribute
    result[0] = x;
    result[0].attr("class") = "bar";
    return result;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

您不能像这样直接将类添加到元素访问语法中。但是,您可以对对象进行分类,然后将其添加到列表中:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::List foo() {
    // Setup the list
    Rcpp::List result(1);
    // Setup the object that will go in the list
    Rcpp::IntegerVector x = Rcpp::seq(1, 10);
    // Your approach was to add it to the list, THEN set the class attribute
    // result[0] = x;
    // result[0].attr("class") = "bar";
    // What we need to do is set the class of that object
    x.attr("class") = "bar";
    // BEFORE adding it to the list
    result[0] = x;
    return result;
}

/*** R
foo()
*/
Run Code Online (Sandbox Code Playgroud)
foo()
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10
attr(,"class")
[1] "bar"
Run Code Online (Sandbox Code Playgroud)

更新

请注意,在许多其他情况下,这也是Rcpp::Lists 的一个问题。考虑以下:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::List baz() {
    Rcpp::List result(1);
    Rcpp::IntegerVector x = Rcpp::seq(1, 10);
    result[0] = x;
    Rcpp::Rcout << result[0][1] << std::endl;
    result[0][2] += 1;
    return result;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

将此与使用 an std::vectorof进行比较Rcpp::IntegerVector

#include <Rcpp.h>

// [[Rcpp::export]]
void qux() {
    std::vector<Rcpp::IntegerVector> result(1);
    Rcpp::IntegerVector x = Rcpp::seq(1, 10);
    result[0] = x;
    Rcpp::Rcout << result[0][0] << std::endl;
    result[0][2] += 1;
    Rcpp::Rcout << result[0] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
qux()
1
1 2 4 4 5 6 7 8 9 10
Run Code Online (Sandbox Code Playgroud)

正如在几个地方所讨论的(稍后将尝试返回并添加一些链接),当涉及到 an 时,您通常必须更加明确,Rcpp::List因为它的元素几乎可以是任何东西