-1

I do an R library with some Cpp functions, i do it using Rcpp.package.skeleton(). In the same Cpp file i have more functions. Is there a method to make the functions that i can't use invisible ?

For example i have :

#include <Rcpp.h>
// [[Rcpp::export]]

int a (int x){
return x+2;
}

// [[Rcpp::export]]
int b(int y){
z=a(y);
return(z);
}

I want to make only "b" function visibile to call.

I use Rcpp skeleton to make my package, if i do R CMD check warning appear that tell me :

Undocumented code objects:'a'

Because i do the documentation only for function "b".

Is there a method to do this ? In RStudio when i write function preview of function that i write appears and i don't want it for function "a" but only for function "b"

  • 3
    Have you read any of the excellent [Rcpp vignettes](https://cran.r-project.org/package=Rcpp)? – Joseph Wood Dec 16 '19 at 12:31
  • 1
    Specifically, [Section 1.6. Modifying Function Names](https://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-attributes.pdf#page=2) of the [_Rcpp Attributes_](https://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-attributes.pdf) vignette. – coatless Dec 16 '19 at 17:42

1 Answers1

4

If you are only going to use the C++ function within your C++ code, you don't need to use the first instance of the // [[Rcpp::export]] line at all. That way you can still call a() from within b() in your C++ code, but only b will be made available as a function in R.

If you want to be able to use a() as an R function internally within your package, but want to hide it from end-users, you need to export it with a name that starts with a period, so instead of // [[Rcpp::export]], you would type // [[Rcpp::export(.a)]]. Now you will be able to use .a() as a function in your package's R code, but it will be hidden to end-users of your package.

#include <Rcpp.h>

// [[Rcpp::export(.a)]]
int a (int x){
return x+2;
}

// [[Rcpp::export]]
int b(int y){
z = a(y);
return(z);
}

Now, in R you can do this:

> library(MatteosPackage)
> b(1)
[1] 3
>.a(1)
Error in .a(1) : could not find function ".a"
> MatteosPackage::.a(1)
Error: '.a' is not an exported object from 'namespace:MatteosPackage'
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • 1
    Dirk still thinks it is more efficient to direct to direct people to the manuals, and improve those where needed, than to repeat them here paragraph by paragraph :). Examples are good though, and this is a pretty recent one for me -- I had more or less forgotten about this naming feature. All that said, ideally users do eventually get into a self-learning groove, and some generous ones even come back and help here, so thanks for that! – Dirk Eddelbuettel Dec 16 '19 at 13:05