Search code examples
rrcpp

How to make invisible functions in R library


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"


Solution

  • 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'