I am using a sparse matrix from Eigen as a data structure for some of my functions. My function will iterate over the rows of a sparse matrix and perform some operations. I am trying to access the row of a sparse matrix as a SparseVector, but for some reason that causes R to crash.
#include <RcppEigen.h>
// [[Rcpp::depends(RcppEigen)]]
using Eigen::Map;
using Eigen::MatrixXd;
using Eigen::VectorXd;
using Eigen::SparseMatrix;
using Eigen::MappedSparseMatrix;
using Eigen::SparseVector;
using Eigen::Triplet;
using namespace Rcpp;
using namespace Eigen;
// [[Rcpp::export]]
Eigen::SparseVector<double> getSparseVec(const Eigen::MappedSparseMatrix<double>& X) {
return X.row(1);
}
The return type of .row() seems to be a ConstRowXpr, but I don't know how to get that into the form of a SparseVector, and I've been unable to find the doc page for ConstRowXpr
Related
I wish to do calculations on elements of a vector using Rcpp, but the vector is getting so large (~60 GB) that I'm resorting to memory mapping it using the mmap package, but now it's the wrong type for my Rcpp function. Can this be overcome?
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double testRcpp(NumericVector input, int index) {
return input(index);
}
/*** R
writeBin(seq(0,1,1e-6),"test.bin")
bigvector1 <- seq(0,1,1e-6)
bigvector2 <- mmap("test.bin",mode=double())
testRcpp(bigvector1,3)
testRcpp(bigvector2,3) #"Not compatible with requested type: [type=environment; target=double]"
*/
Since the mmap function in r returns an object with type=environment write bigvector2[] instead of bigvector2 to use its elements. Basically replace
testRcpp(bigvector2,3)
to
testRcpp(bigvector2[],3)
If you want to try using mmap in the cpp part of Rcpp in windows you can use my repo from https://github.com/CoderRC/libmingw32_extended .
My code is the following
#include <RcppArmadillo.h>
#include <Rcpp.h>
using namespace std;
using namespace Rcpp;
using namespace arma;
//RNGScope scope;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat hh(arma::mat Z, int n, int m){
if(Z.size()==0){
Z = arma::randu<mat>(n,m); # if matrix Z is null, then generate random numbers to fill in it
return Z;
}else{
return Z;
}
}
Error reported:
conflicting declaration of C function 'SEXPREC* sourceCpp_1_hh(SEXP, SEXP, SEXP)'
Do you have any idea about this question?
Thank you in advance!
Let's slow down and clean up, following other examples:
Never ever include both Rcpp.h and RcppArmadillo.h. It errors. And RcppArmadillo.h pulls in Rcpp.h for you, and at the right time. (This matters for the generated code.)
No need to mess with RNGScope unless you really know what your are doing.
I recommend against flattening namespaces.
For reasons discussed elsewhere at length, you probably want R's RNGs.
The code doesn't compile as posted: C++ uses // for comments, not #.
The code doesn't compile as posted: Armadillo uses different matrix creation.
The code doesn't run as intended as size() is not what you want there. We also do not let a 'zero element' matrix in---maybe a constraint on our end.
That said, once repaired, we now get correct behavior for a slightly changed spec:
Output
R> Rcpp::sourceCpp("~/git/stackoverflow/63984142/answer.cpp")
R> hh(2, 2)
[,1] [,2]
[1,] 0.359028 0.775823
[2,] 0.645632 0.563647
R>
Code
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat hh(int n, int m) {
arma::mat Z = arma::mat(n,m,arma::fill::randu);
return Z;
}
/*** R
hh(2, 2)
*/
I'm quite new to Rcpp. Sorry If I'm missing something obvious.
but when I try to use an igraph function in Rcpp I face the following obvious error on the left:
"Cannot initialize a Variable of type 'RCPP:Environment' (aka,'int') with an lvalue of type 'const char[15]'
Here is the code
#include <Rcpp.h>
// [[Rcpp::plugins(cpp11)]]
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector insideOfCommEdgeIdsCpp(CharacterVector g, CharacterVector v) {
Environment igraph("package:igraph");
Function game_er = igraph["erdos.renyi.game"];
Function get_adjacency = igraph["get.adjacency"];
}
A few small errors in your file:
declared as NumericVector but nothing is returned
Environment igraph not set up correctly.
A corrected version is below. And it it worth repeating this: Any R functions called from C++ are still R functions that run at the speed of R functions.
Corrected code
#include <Rcpp.h>
// [[Rcpp::plugins(cpp11)]]
using namespace Rcpp;
// [[Rcpp::export]]
void insideOfCommEdgeIdsCpp(CharacterVector g, CharacterVector v) {
Environment igraph = Environment("package:igraph");
Function game_er = igraph["erdos.renyi.game"];
Function get_adjacency = igraph["get.adjacency"];
}
I have a data frame out constructed. Suppose the first column in the data frame is constructed using Rcpp::DoubleVector and REAL(). I want to convert the entire column to a Rcpp::IntegerVector (much like what as.integer() does in base R).
I tried
out[0] = Rcpp::as<Rcpp::IntegerVector>(out[0]);
but R returns an error on the conversion.
Is there a way to make the conversion?
This works fine for me:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void toInt(DataFrame& x) {
x[0] = as<IntegerVector>(x[0]);
}
/*** R
toInt(iris)
typeof(iris[[1]])
*/
This question already has an answer here:
calling a user-defined R function from C++ using Rcpp
(1 answer)
Closed 6 years ago.
I have a package X in R. The package has a function foo(). I want to call the function foo() in a cpp file (using Rcpp). Is it possible?
#include <Rcpp.h>
void function01() {
// call foo() from package X ??
}
This is sort of a duplicate. Though, the majority of cases do not involve calling from a user defined package.
As a result, the mold to use is:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void function01(){
// Obtain environment containing function
Rcpp::Environment package_env("package:package_name_here");
// Make function callable from C++
Rcpp::Function rfunction = package_env["function_name"];
// Call the function and receive output (might not be list)
Rcpp::List test_out = rfunction(....);
}