1

I wish to build a package, but I write part of it using RcppArmadillo and now I am suffering the consequences. I am using roxygen2 and devtools to help me with DESCRIPTION and NAMESPACE. I am coding in R / Ubuntu. In the DESCRIPTION I include two lines to load the packages:

Depends: R (>= 3.4.4), MASS (>= 7.3-49), Rcpp (>= 1.0.5), RcppArmadillo (>= 0.9.900.2.0) LinkingTo: Rcpp, RcppArmadillo

and in the folder /src I write a script name loss_function.cpp, inside it there is:

> // [[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); 
> }

When I check the package (build -> check package) there comes an error msg:

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? 
Phil
  • 7,287
  • 3
  • 36
  • 66
  • You appear to have an error that indicated both `devtools` and `knitr` neither one of which is implied by `RcppArmadillo`. Maybe just try `R CMD build ...` and `R CMD check` for once? For what it is worth, as author and maintainer of `RcppArmadillo` I have written _a lot_ of packages with it, and I also use `roxygen2` but methinks you have an unrelated issue here. Maybe prunce and simplify? Or start from `RcppArmadillo.package,skeleton()` and build up, step by step? – Dirk Eddelbuettel Aug 18 '22 at 15:08

1 Answers1

1

As alluded in the comment above, it is not clear where you error would lie here with RcppArmadillo. So I

  • invoked RcppArmadillo.package.skeleton() (via the helper command kitten.r from littler relying on pkgKitten but those are details)

  • added in src/loss_function.cpp based on what you had

  • added one missing @export tag to you doxygen/roxygen to also have the R documentation created

  • called Rcpp::compileAttributes() (via helper compAttr.r from `littler) to have the interal glue code generated / updated

  • called roxygenize() (via helper roxy.r from littler) to have help files created

  • called R CMD build (directly, or via helper build.r from littler)

  • ran R CMD check (directly, or via helper rcc.r from littler)

and it all works:

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)$

and checks nicely too

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)$ 

I put the tarball here if you want to take it from there.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thank you @Dirk, you and Ricardo Cunha Pedroso (I sent an email to my friend) solved the problem for me. Starting by RcppArmadillo.package.skeleton() is the key for my issue. – Ian Niewicz Aug 23 '22 at 09:08