首页 > 解决方案 > 如何设置 Rcpp::List 元素的类属性?

问题描述

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

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

‘Rcpp::Vector<19>::Proxy’ {aka ‘class Rcpp::internal::generic_proxy<19>’} has no member named ‘attr’

glfw_types.h

#ifndef RCPP_GLFW_TYPES_H
#define RCPP_GLFW_TYPES_H

#include <Rcpp.h>
#include <GLFW/glfw3.h>

void glfw_destroy_monitor(GLFWmonitor*);

// https://stackoverflow.com/questions/41210595/s4-object-with-a-pointer-to-a-c-struct

typedef Rcpp::XPtr<GLFWwindow, Rcpp::PreserveStorage, glfwDestroyWindow> GLFWwindow_ptr;
typedef Rcpp::XPtr<GLFWmonitor, Rcpp::PreserveStorage, glfw_destroy_monitor> GLFWmonitor_ptr;


#endif

C++ 源文件

#include "glfw_types.h"
using namespace Rcpp;

// [[Rcpp::export]]
GLFWmonitor_ptr glfw_get_primary_monitor() {

  GLFWmonitor_ptr new_monitor = GLFWmonitor_ptr(glfwGetPrimaryMonitor(), true);

  new_monitor.attr("class") = "GLFWmonitor";

  return new_monitor;
}

// [[Rcpp::export]]
Rcpp::List glfw_get_monitors() {

  int nr_monitors;
  GLFWmonitor** monitors = glfwGetMonitors(&nr_monitors);
  Rcpp::List monitor_ptrs(nr_monitors);

  for(int i = 0; i < nr_monitors; i++) {
    monitor_ptrs[i] = GLFWmonitor_ptr((GLFWmonitor*)monitors[i], true);
    monitor_ptrs[i].attr("class") = "GLFWmonitor";
  }

  return monitor_ptrs;
}

标签: rrcpp

解决方案


这里的问题是您尝试分配课程时。考虑以下更简单的示例:

#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;
}

在此处输入图像描述

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

#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()
*/
foo()
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10
attr(,"class")
[1] "bar"

更新

请注意,在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;
}

在此处输入图像描述

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

#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;
}
qux()
1
1 2 4 4 5 6 7 8 9 10

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


推荐阅读