问题
I am trying to create a R package that uses random draws from a truncated normal in a .cpp script. I am using the rtn1 function from the pckage RcppTN. If I source the code, the function works fine. Once I build the package I get the error:
> library(testtruncnorm)
> testtruncnorm()
Error in testtruncnorm::testtruncnorm() :
function 'RcppTN_rtn1' not provided by package 'RcppTN'
Simplified .cpp code is here
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppTN.h>
// [[Rcpp::depends(RcppTN)]]
#include<armadillo>
using namespace Rcpp;
//' draw truncated normal
//'
//' testtruncnorm()
//' @return returns 2 draws from a truncated normal
// [[Rcpp::export]]
arma::vec testtruncnorm()
{
arma::vec result = arma::ones(2);
result[1] = RcppTN::rtn1(1, 1, 0,HUGE_VAL);
result[2] = RcppTN::rtn1(1, 1, 0,HUGE_VAL);
return result;
}
My NAMESPACE file is
useDynLib(testtruncnorm, .registration=TRUE)
importFrom(Rcpp, evalCpp)
exportPattern("^[[:alpha:]]+")
My DESCRIPTION file is
Package: testtruncnorm
Type: Package
Title: What the Package Does Using Title Case
Version: 1.0
Date: 2018-10-23
Author: Your Name
Maintainer: Your Name <your@email.com>
Description: More details about what the package does. See
<http://cran.r-project.org/doc/manuals/r-release/R-exts.html#The-
DESCRIPTION-file>
for details on how to write this part.
License: GPL (>= 2)
Imports: Rcpp (>= 0.12.19), RcppTN
LinkingTo: Rcpp, RcppArmadillo, RcppTN
I am using RStudio create "R Package with RcppArmadillo" to get started. RStudio version 1.1.456. R version 3.5.1. Windows 10.
回答1:
You have to make sure that RcppTN
gets attached. You can do this using
importFrom(RcppTN, rtn)
in NAMESPACE
. In the documentation for RcppTN
it says that one should add
Depends: RcppTN
which should have the same effect.
来源:https://stackoverflow.com/questions/52952775/rcpptn-cpp-script-works-when-sourced-but-not-when-compiled-in-library