I am trying to build an R
package using mlpack
. As suggested in this link I am using the following cpp
function
#include <Rcpp/Rcpp>
#include <mlpack.h>
// Two include directories adjusted for my use of mlpack 3.4.2 on Ubuntu
#include <mlpack/core.hpp>
#include <mlpack/methods/kmeans/kmeans.hpp>
#include <mlpack/methods/kmeans/random_partition.hpp>
#include <mlpack/methods/neighbor_search/neighbor_search.hpp>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(mlpack)]]
// This is 'borrowed' from mlpack's own src/mlpack/tests/kmeans_test.cpp
// and src/mlpack/tests/kmeans_test.cpp. We borrow the data set, and the
// code from the first test function. Passing data from R in easy thanks
// to RcppArmadillo, 'and left as an exercise'.
// Generate dataset; written transposed because it's easier to read.
arma::mat kMeansData(" 0.0 0.0;" // Class 1.
" 0.3 0.4;"
" 0.1 0.0;"
" 0.1 0.3;"
" -0.2 -0.2;"
" -0.1 0.3;"
" -0.4 0.1;"
" 0.2 -0.1;"
" 0.3 0.0;"
" -0.3 -0.3;"
" 0.1 -0.1;"
" 0.2 -0.3;"
" -0.3 0.2;"
" 10.0 10.0;" // Class 2.
" 10.1 9.9;"
" 9.9 10.0;"
" 10.2 9.7;"
" 10.2 9.8;"
" 9.7 10.3;"
" 9.9 10.1;"
"-10.0 5.0;" // Class 3.
" -9.8 5.1;"
" -9.9 4.9;"
"-10.0 4.9;"
"-10.2 5.2;"
"-10.1 5.1;"
"-10.3 5.3;"
"-10.0 4.8;"
" -9.6 5.0;"
" -9.8 5.1;");
// [[Rcpp::export]]
arma::Row<size_t> kmeansDemo() {
mlpack::kmeans::KMeans<mlpack::metric::EuclideanDistance,
mlpack::kmeans::RandomPartition> kmeans;
arma::Row<size_t> assignments;
kmeans.Cluster((arma::mat) trans(kMeansData), 3, assignments);
return assignments;
}
If I sourceCpp
the above in Ubuntu linux Sys.setenv("PKG_LIBS"="-lmlpack")
then it compiles successfully. However, I am unable to use it on macOS with Apple M2 architecture. I am getting the following error in macOS
/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/mlpack/include/mlpack.h:52:10: fatal error: mlpack/core.hpp: No such file or directory
52 | #include <mlpack/core.hpp>
| ^~~~~~~~~~~~~~~~~
compilation terminated.
I have installed mlpack
R
package installed as well as the system mlpack
using brew
. Seems to me that R
cannot link to the mlpack
libraries that are located in /opt/homebrew/include/
in my system. Is there a way to link to these? I have tried brew link mlpack
which shows linking is successful but still got the same compilation error. Additionally I tried the following in R
before sourceCpp
ing but no luck!
Sys.setenv("LDFLAGS"="-L/opt/homebrew/lib")
Sys.setenv("CPPFLAGS"="-I/opt/homebrew/include")
Sys.setenv("PKG_LIBS"="-lmlpack")
Please let me know if there is any way out for this in macOS.
P.S. Both R
and Rstudio
are installed in my system using brew
.