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

问题描述 投票:3回答:2

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

src/

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文件

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对象时,我在.getClassesFromCache(Class)中得到错误:找不到对象'Student'

这个问题与this SO post类似,但接受的答案似乎并没有解决我的问题。此外,我已经研究了Dirk提到的Annoy source code,但我没有看到该代码与我之间有任何有用的差异,除了我尝试在我的代码中放置的RCPP_EXPOSED_CLASS_NODECL(AnnoyEuclidean)片段,但也没有帮助。

c++ r rcpp
2个回答
3
投票

浏览这两个软件包,主要区别在于如何设置函数的导入和导出。

特别是,methods文件中缺少RcppNAMESPACE包作为完全导入。此外,不是单独导出每个函数,而是使用全局导出模式。

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

roxygen2:

#' @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


2
投票

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

UPDATE 似乎我可以添加#' @export Student给student.R for roxygen自动将export(Student)位添加到我的NAMESPACE,这样我就不必手动修改NAMESPACE了。

UPDATE2 我已经记录了我在this blog post的步骤

© www.soinside.com 2019 - 2024. All rights reserved.