Get error when use Rcpp remove rows of matrix - r

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat fed(arma::mat x){
arma::mat zz=x.shed_rows(0,2);
return(zz);
}
Just want remove some rows from matrix, get error as follows.
conversion from 'void' to non-scalar type 'arma::Mat} requested'

Two points:
Please don't post error messages as image. Use Text instead.
As the error indicates, the shed_rows() method does not return anything. Instead it alters the matrix it acts on, c.f. the documentation.
The following works:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat fed(arma::mat x){
x.shed_rows(0,2);
return(x);
}
/*** R
fed(matrix(1:16, 4 ,4))
*/

Related

RcppArmadillo: conflicting declaration of C function 'SEXPREC* sourceCpp_1_hh(SEXP, SEXP, SEXP)'

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)
*/

Rcpp: Obvious error when using igraph functions

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"];
}

RcppArmadillo and shed_col() [duplicate]

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat fed(arma::mat x){
arma::mat zz=x.shed_rows(0,2);
return(zz);
}
Just want remove some rows from matrix, get error as follows.
conversion from 'void' to non-scalar type 'arma::Mat} requested'
Two points:
Please don't post error messages as image. Use Text instead.
As the error indicates, the shed_rows() method does not return anything. Instead it alters the matrix it acts on, c.f. the documentation.
The following works:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat fed(arma::mat x){
x.shed_rows(0,2);
return(x);
}
/*** R
fed(matrix(1:16, 4 ,4))
*/

Rcpp function default values in header file

I whant to specify default values for my Rcpp function arguments in header file. So I have header file foo.h:
#ifndef foo_H
#define foo_H
#include <Rcpp.h>
int foo(int k = 3);
#endif
I also have foo.cpp file:
#include "foo.h"
#include <Rcpp.h>
using namespace Rcpp;
//'Some description
//'
//' #export
// [[Rcpp::export]]
int foo(int k)
{
return(k);
}
I compile the package and use this function from R:
foo()
Then I get error "argument "k" is missing, with no default" while I am expecting 3 to be returned.
Please help we to figure out how to define Rcpp default values in header file.
Will be very greatfull for help!
P.S. I need to specify default values in header file only and not any other place. It is clear to me how to specify default values in .cpp but I need to specify them in .h.

Rcpp trouble importing 'hessian' from R package 'numDeriv'

I am trying to build a package that uses the function 'hessian' from the package 'numDeriv'. However, when I build the package and run the code I get the error
Cannot convert object to an environment: [type=character; target=ENVSXP].
Example simplified Rcpp code below
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
#include <stdio.h>
#include<armadillo>
using namespace Rcpp;
using namespace std;
double testfunc(double X){
return pow(X+1,2);
}
double hessian_rcpp(double X){
Rcpp::Environment numDeriv("package:numDeriv");
Rcpp::Function hessian = numDeriv["hessian"];
Rcpp::List hessian_results = hessian(
Rcpp::_["func"] = Rcpp::InternalFunction(testfunc),
Rcpp::_["x"] = X);
arma::vec out = Rcpp::as<arma::vec>(hessian_results[0]);
return out[0];
}
// [[Rcpp::export]]
double returnhess(double X){
double out = hessian_rcpp(X);
return out;
}
Then after building the package running the following R code results in the error.
library(test)
returnhess(X=3)
Error in returnhess(X = 3) :
Cannot convert object to an environment: [type=character; target=ENVSXP].
My NAMESPACE is
useDynLib(test, .registration=TRUE)
importFrom(Rcpp, evalCpp)
exportPattern("^[[:alpha:]]+")
My DESCRIPTION is
Package: test
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself#somewhere.net>
Description: More about what it does (maybe more than one line) Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Imports: Rcpp, RcppArmadillo, numDeriv
LinkingTo: Rcpp, RcppArmadillo, numDeriv
Encoding: UTF-8
LazyData: true
My R version is 3.5.1, RStudio version is 1.1.456, Rcpp version is 0.12.19, RcppArmadillo version is 0.9.100.5.0, numDeriv version is 2016.8.1. My operating system is windows 10.
I was able to successfully import 'optimize' from the R package 'stats' with an analogous approach. Example simplified code below
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
#include <stdio.h>
#include<armadillo>
using namespace Rcpp;
using namespace std;
double testfunc(double X){
return pow(X+1,2);
}
double optim_rcpp(){
Rcpp::Environment stats("package:stats");
Rcpp::Function optimize = stats["optimize"];
Rcpp::List opt_results = optimize(
Rcpp::_["f"] = Rcpp::InternalFunction(testfunc),
Rcpp::_["lower"] = -10,
Rcpp::_["upper"] = 10);
arma::vec out = Rcpp::as<arma::vec>(opt_results[0]);
return out[0];
}
// [[Rcpp::export]]
double returnoptim(){
double out = optim_rcpp();
return out;
}
Same NAMESPACE and DESCRIPTION as above
Then running the following R code works
returnoptim()
[1] -1
As a workaround you can add
Depends:numDeriv
to your DESCRIPTION. This ensures that the numDeriv package is loaded together with your package.
BTW: I would refrain from using using namespace Rcpp; in a package. And I would never use using namespace std;. I cannot think of a good reason to use #include <stdio.h> and #include<armadillo>is unnecessary when RcppArmadillo is used.
In addition to #RalfStubner's answer, I would like to point out the following. The Rcpp::Environment namespace has a function namespace_env("the_package_name").
This gives you the same functionality as in R's approach packagename::package_functionX(...). In combination with Depends or Imports in your DESCRIPTION file, you are on the safe side.
Based on it you can proceed as follows:
#include <iostream>
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
double testfunc(double X){
return std::pow(X+1,2);
}
double optim_rcpp(){
Rcpp::Environment stats = Rcpp::Environment::namespace_env("stats");
Rcpp::Function optimize = stats["optimize"];
Rcpp::List opt_results = optimize(
Rcpp::_["f"] = Rcpp::InternalFunction(testfunc),
Rcpp::_["lower"] = -10,
Rcpp::_["upper"] = 10);
arma::vec out = Rcpp::as<arma::vec>(opt_results[0]);
return out[0];
}
// [[Rcpp::export]]
double returnoptim(){
return optim_rcpp();
}
/*** R
returnoptim() # -1
*/
And to answer #mike's question directly, you can also do so:
#include <iostream>
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
double testfunc(double X){
return std::pow(X+1,2);
}
double hessian_rcpp(double X){
Rcpp::Environment numDeriv = Rcpp::Environment::namespace_env("numDeriv");
Rcpp::Function hessian = numDeriv["hessian"];
Rcpp::List hessian_results = hessian(
Rcpp::_["func"] = Rcpp::InternalFunction(testfunc),
Rcpp::_["x"] = X);
arma::vec out = Rcpp::as<arma::vec>(hessian_results[0]);
return out[0];
}
// [[Rcpp::export]]
double returnhess(double X){
return hessian_rcpp(X);
}
/*** R
returnhess(X=3) # 2
*/

Resources