使用 Rcpp 模块将简单的 C++ Student class 暴露给 R

Expose simple C++ Student class to R using Rcpp modules

我正在尝试使用包中的 Rcpp 将 C++ 中的简单 Student class 公开给 R。这是我的设置...

源/

Student.hpp
//  Student.hpp

#ifndef Student_hpp
#define Student_hpp

#include <string>
#include <vector>

class Student
{
public:

    // Constructor
    Student(std::string name, int age, bool male);

    // Getters
    std::string GetName();
    int GetAge();
    bool IsMale();
    std::vector<int> GetFavoriteNumbers();

    // Methods
    bool LikesBlue();

private:

    // Member variables
    std::string name;
    int age;
    bool male;
    std::vector<int> favoriteNumbers;
};

#endif /* Student_hpp */
Student.cpp
//  Student.cpp

#include "Student.hpp"

// Constructor
Student::Student(std::string name, int age, bool male) {
  this->name = name;
  this->age = age;
  this->male = male;
  this->favoriteNumbers = {2, 3, 5, 7, 11};
}

// Getters
bool Student::IsMale() { return male; }
int Student::GetAge() { return age; }
std::string Student::GetName() { return name; }
std::vector<int> Student::GetFavoriteNumbers() { return favoriteNumbers; }

// Methods
bool Student::LikesBlue() {
    return (male || age >= 10);
}
glue.cpp
// glue.cpp
// To use c++11, first run: Sys.setenv("PKG_CXXFLAGS"="-std=c++11")  ...or use a Makevars file

#include <Rcpp.h>
#include "Student.hpp"
using namespace Rcpp;

//' Simulate a student
//'
//' @export
// [[Rcpp::export]]
std::vector<int> simulate_student() {
  Student s = Student("bob", 10, true);
  return s.GetFavoriteNumbers();
}

// Expose (some of) the Student class
RCPP_MODULE(Student){
  class_<Student>("Student")
  .constructor<std::string,int,bool>()
  .method("LikesBlue", &Student::LikesBlue)
  ;
}

R/

student.R
#' student
#'
#' A cool package
#'
#' Imports
#' @useDynLib student, .registration = TRUE
#' @importFrom Rcpp sourceCpp
"_PACKAGE"

Rcpp::loadModule(module = "Student", TRUE)

调用 devtools::document() 后,我得到以下 NAMESPACE 文件

命名空间
# Generated by roxygen2: do not edit by hand

export(simulate_student)
importFrom(Rcpp,sourceCpp)
useDynLib(student, .registration = TRUE)

现在,在从 RStudio(即 R CMD INSTALL --preclean --no-multiarch --with-keep.source student)执行 清理和重建 之后,包编译和加载没有问题。如果我 运行 simulate_student() 函数,我会得到预期的结果。但是,当我尝试使用 stud <- new(Student) 创建一个新的 Student 对象时,我得到 Error in .getClassesFromCache(Class) : object 'Student' not found

这个问题似乎与 Dirk 提到的 but the accepted answer does not seem to solve my issue. Furthermore, I've looked into the Annoy source code 相似,但我没有发现该代码与我的代码之间有任何有用的区别,除了我尝试放置在我的代码中的 RCPP_EXPOSED_CLASS_NODECL(AnnoyEuclidean) 片段代码,但也没有帮助。

this SO post 的回答和评论对我很有帮助。看来我需要

1) 将 @importFrom Rcpp sourceCpp 更改为 @import Rcpp
2) 将 export(Student) 添加到我的 NAMESPACE 文件中(尽管我正在搜索如何使用 roxygen2 很好地执行此操作而不是手动插入)

经过上面的改动我可以运行

> stud <- new(Student, name = "Ben", age = 26, male = T)
> stud$LikesBlue()
[1] TRUE

更新
看来我可以将 #' @export Student 添加到 student.R 以便 roxygen 自动将 export(Student) 位添加到我的 NAMESPACE,这样我就不必手动修改 NAMESPACE。

更新2
我在 this blog post

中记录了我的步骤

扫一眼这两个包,主要区别在于函数的导入和导出设置方式。

特别是,methodsRcpp 包都丢失了,因为 完全导入 NAMESPACE 文件中。此外,不是单独导出每个函数,而是使用全局导出模式。

RcppAnnoy

useDynLib(RcppAnnoy, .registration=TRUE)
import(methods, Rcpp)
exportPattern("^[[:alpha:]]+")          # export all identifiers starting with letters

https://github.com/eddelbuettel/rcppannoy/blob/1ce871ae52730098ddc7355597613e8313e23ac9/NAMESPACE#L1-L3

RcppStudent

氧气2:

#' @useDynLib RcppStudent, .registration = TRUE
#' @import methods Rcpp
#' @exportPattern "^[[:alpha:]]+"

NAMESPACE:

# Generated by roxygen2: do not edit by hand

exportPattern("^[[:alpha:]]+")
import(Rcpp)
import(methods)
useDynLib(RcppStudent, .registration = TRUE)

https://github.com/r-pkg-examples/rcpp-modules/blob/ca5d13ddd391d9fd4ffb50b4306131d2839f8af5/NAMESPACE#L5-L7