首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用RcppArmadillo构建包?

如何使用RcppArmadillo构建包?
EN

Stack Overflow用户
提问于 2022-08-18 14:57:08
回答 1查看 91关注 0票数 1

我希望构建一个包,但是我使用RcppArmadillo编写了其中的一部分,现在我正在承受后果。我正在使用roxygen2和devtools来帮助我处理描述和命名空间。我正在用R/ Ubuntu编码。在描述中,我包含了两行来加载包:

视情况:r (>= 3.4.4),质量(>= 7.3-49),Rcpp (>= 1.0.5),RcppArmadillo (>= 0.9.900.2.0) LinkingTo: Rcpp,RcppArmadillo

在文件夹/src中,我写了一个脚本名loss_function.cpp,里面有:

代码语言:javascript
复制
> // [[Rcpp::depends(RcppArmadillo)]]
> 
> #include <RcppArmadillo.h>
> 
> using namespace Rcpp;
> 
> //' Check function. 
> //' 
> //' @param x vector 
> //' @param tau percentile
> //' @return y new vector 
> // [[Rcpp::export(rho_koenker)]] 
> arma::vec rho_koenker(arma::vec x, double tau){   
> int n = x.n_elem;   
> arma::vec y(n);   
> for(int i = 0; i < n; ++i){
>     if(x(i)<0){
>       y(i) = x(i)*(tau-1);
>     } else {
>       y(i) = x(i)*tau;
>     }   
>  }   
> return(y); 
> }
> 
> //' Quantile regression loss function 
> //' 
> //' @param beta parameter
> //' @param x matrix 
> //' @param y vector 
> //' @param tau percentile 
> //' @param N total number of observations 
> //' @param d beta's length 
> //' @return eta numeric 
> // [[Rcpp::export(loss_qr)]] 
> double loss_qr(arma::vec beta, arma::mat x, arma::vec y, double tau, int N, int d){
>   double eta = 0;   
>   arma::vec res(N);   
>   arma::vec rho(N);  
>   res = y - (x * beta);   
>   rho = rho_koenker(res,tau);   
>   eta = accu(rho);
> return(eta); 
> }

当我检查包(构建->检查包)时,会出现一个错误消息:

代码语言:javascript
复制
Error in .Call("_pqfe_loss_qr", PACKAGE = "pqfe", beta, x, y, tau, N,  : 
  "_pqfe_loss_qr" not available for .Call() for package "pqfe"
Calls: qr ... optim_qr -> <Anonymous> -> <Anonymous> -> fn -> .Call
Execution halted
Warning message:
Can't find generic `sew` in package knitr to register S3 method.
 This message is only shown to developers using devtools.
 Do you need to update knitr to the latest version? 
EN

回答 1

Stack Overflow用户

发布于 2022-08-18 16:34:43

正如上面的注释所暗示的那样,RcppArmadillo并不清楚您的错误在哪里。所以我

  • 调用RcppArmadillo.package.skeleton() (通过littler的助手命令kitten.r依赖于pkgKitten,但这些都是详细信息)
  • 根据您拥有的内容在src/loss_function.cpp中添加
  • 向doxygen/roxygen添加了一个缺失的@export标记,以创建R文档
  • 名为Rcpp::compileAttributes() (通过来自``littler的助手compAttr.r )来生成/更新内部胶水代码
  • 名为roxygenize() (通过从littler生成的助手roxy.r )来创建帮助文件
  • 称为R CMD build (直接,或通过来自littler的助手build.r )
  • ran R CMD check (直接,或通过来自littler的助手rcc.r )

这一切都很有效:

代码语言:javascript
复制
edd@rob:~/git/stackoverflow/73405262(master)$ R CMD build soDemo                                          
* checking for file ‘soDemo/DESCRIPTION’ ... OK
* preparing ‘soDemo’:        
* checking DESCRIPTION meta-information ... OK
* cleaning src                                     
* installing the package to process help pages     
* saving partial Rd database        
* cleaning src               
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories                                                             
* building ‘soDemo_1.0.tar.gz’             
                                                    
edd@rob:~/git/stackoverflow/73405262(master)$

而且检查得也很好

代码语言:javascript
复制
edd@rob:~/git/stackoverflow/73405262(master)$ R CMD check soDemo_1.0.tar.gz 
* using log directory ‘/home/edd/git/stackoverflow/73405262/soDemo.Rcheck’
* using R version 4.2.1 (2022-06-23)            
* using platform: x86_64-pc-linux-gnu (64-bit)
* using session charset: UTF-8 
* checking for file ‘soDemo/DESCRIPTION’ ... OK
* checking extension type ... Package                                                                    
* this is package ‘soDemo’ version ‘1.0’
* package encoding: UTF-8
* checking package namespace information ... OK
* checking package dependencies ... OK 
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK   
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘soDemo’ can be installed ... OK
* checking installed package size ... OK
* checking package directory ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking R files for non-ASCII characters ... OK
* checking R files for syntax errors ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking dependencies in R code ... OK
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... OK
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd cross-references ... OK
* checking for missing documentation entries ... OK
* checking for code/documentation mismatches ... OK
* checking Rd \usage sections ... OK
* checking Rd contents ... OK
* checking for unstated dependencies in examples ... OK
* checking line endings in C/C++/Fortran sources/headers ... OK
* checking line endings in Makefiles ... OK
* checking compilation flags in Makevars ... OK
* checking for GNU extensions in Makefiles ... OK
* checking for portable use of $(BLAS_LIBS) and $(LAPACK_LIBS) ... OK
* checking use of PKG_*FLAGS in Makefiles ... OK
* checking compilation flags used ... OK
* checking compiled code ... OK
* checking examples ... OK
* checking for unstated dependencies in ‘tests’ ... OK
* checking tests ...
  Running ‘tinytest.R’
 OK
* checking PDF version of manual ... OK
* DONE

Status: OK

edd@rob:~/git/stackoverflow/73405262(master)$ 

如果你想从那里拿走的话,我就把它放在这里上。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73405262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档