c++ - 使用 Rcpp 模块将简单的 C++ 学生类暴露给 R

标签 c++ r rcpp

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

来源/

学生.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 */
学生.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/

学生.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

这个问题似乎类似于 this SO post但接受的答案似乎并没有解决我的问题。此外,我调查了 Annoy source code德克提到,但我没有看到该代码和我的代码之间有任何有用的区别,除了 RCPP_EXPOSED_CLASS_NODECL(AnnoyEuclidean)我尝试将这些片段放入我的代码中,但也无济于事。

最佳答案

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

特别是,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:]]+"

命名空间:

# 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

关于c++ - 使用 Rcpp 模块将简单的 C++ 学生类暴露给 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53659500/

相关文章:

c++ - 使用 std::transform 从特定目标偏移量插入到目标中

r - ggplot 为 stat_binhex 设置 bin 位置

rcpp 函数不在 r 包中导出

c++ - Rcpp 和 move 语义

c++ - 是否有重构 C++ 以符合编码标准的工具?

c++ - 将指针传递给需要数组的函数是否合法?

c++ - 为什么这个宏接受带有 1 个参数的模板而拒绝带有 2 个参数的模板?

r - 计算行意味着仅针对在 R 中具有多个数据点的行

r:使用所有可能的选项和变量组合数量创建数据框

R:设置初始条件的for循环的dplyr解决方案