Skip to content

[R binding] - Should the R pkg supply an inline plugin helper? #3613

@cgiachalis

Description

@cgiachalis

What is the desired addition or change?

Include an inline plugin in order to make easier to create R package using mlpack library at C++ level via LinkingTo directive.

What is the motivation for this feature?

With CRAN package - mlpack 4.3.0, the headers are installed within the package so we can integrate the mlpack functionality with custom C++ routines via Rcpp. But that works nicely for stand-alone C++ files via the plugins route ( as demonstrated in this SO ). But it is not so sleek solution via LinkingTo directive when creating an R package.

In addition, as per mlpack 's page in CRAN there is no any R package that uses the mlpack library via LinkingTo route. The feature will make easier the R package development.

If applicable, describe how this feature would be implemented.

For that to work, the R mlpack package has to include an inline plugin in order to help Rcpp::compileAttributes() generate the right code; this approach was used in RcppArnadillio, RcppEnsmallen and RcppEigen.

I've created a demo R package here that should work fine if the r-mlpack had a similar helper like the one further below; the helper was taken from RcppArmadillo and added only the mlpack name.

You can test it by rebuilding the r-mlpack with the inclusion of the inline.R file and then install and run the demo:

# demo R package
remotes::install_github("cgiachalis/mlpack.Rpkg")

library(mlpack.Rpkg)

set.seed(1234)
x <- matrix(rnorm(10*5), ncol = 5)

res <- knnDemo(x, k = 3)

# mlpack's knn
res2 <- mlpack::knn(query = x, reference = x, k = 3)

# sanity check
all.equal(res, res2[-3]) 

Inline.R helper

inlineCxxPlugin <- function(...) {
    ismacos <- Sys.info()[["sysname"]] == "Darwin"
    openmpflag <- if (ismacos) "" else "$(SHLIB_OPENMP_CFLAGS)"
    plugin <-
        Rcpp::Rcpp.plugin.maker(
                  include.before = "#include <mlpack.h>",
                  libs           = paste(openmpflag, "$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)"),
                  LinkingTo      = c("RcppArmadillo", "Rcpp", "RcppEnsmallen", "mlpack"),
                  package        = "mlpack"
              )
    settings <- plugin()
    settings$env$PKG_CPPFLAGS <- paste("-I../inst/include", openmpflag)
    if (!ismacos) settings$env$USE_CXX11 <- "yes"
    settings
}

Additional information?

Related #3488

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions