Ramiro Magno
Ramiro Magno

Reputation: 3175

How to set the class attribute of an element of an Rcpp::List?

In the C++ source file below, in function glfw_get_monitors how to set the class attribute of each element of the object monitor_ptrs?

The line monitor_ptrs[i].attr("class") = "GLFWmonitor"; throws a compilation error:

‘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++ source file

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

Upvotes: 5

Views: 425

Answers (1)

duckmayr
duckmayr

Reputation: 16930

The problem here is when you're trying to assign the class. Consider the following more minimal example:

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

enter image description here

You can't directly add the class to the element access syntax like that. However, you can class an object, then add it to the list:

#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"

Update

Take note that this is an issue in many other contexts with Rcpp::Lists as well. Consider the following:

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

enter image description here

Compare this with using an std::vector of 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

As discussed in several places (will try to come back later and add some links), you generally have to be more explicit when it comes to an Rcpp::List because its elements could be almost anything.

Upvotes: 5

Related Questions