Rcpp function default values in header file - r

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.

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

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

Get error when use Rcpp remove rows of matrix

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

call r script from c++ gives cannot convert Rcpp::CharacterVector to const char*

Is there a way to call an r script from C++ ?
I have an rscript , for example:
myScript.R:
runif(100)
I want to execute this script from C++ and pass the result.
I tried:
#include <Rcpp.h>
#include <iostream>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector loadFile(CharacterVector inFile){
NumericVector one = system(inFile);
return one;
}
inFile : "C:/Program Files/R/R-3.4.2/bin/x64/Rscript C:/Rscripts/myScript.R"
but it gives me :
cannot convert Rcpp::CharacterVector (aka Rcpp::Vector<16>} to const char* for argument 1 to int system(const char*)
Usual convention is to use Rcpp so as to write expensive C++ code in R.
If you would like to invoke an R script from within c++, and then work with the result, one approach would be be to make use of the popen function.
To see how to convert a rcpp character to std::string see Converting element of 'const Rcpp::CharacterVector&' to 'std::string'
Example:
std::string r_execute = "C:/Program Files/R/R-3.4.2/bin/x64/Rscript C:/Rscripts/myScript.R"
FILE *fp = popen(r_execute ,"r");
You should be able to read the result of the operation from the file stream.

Borland C++ Builder 6 - E2316 'vector' is not a member of 'std'

Reasonably new to c++, I'm trying to use vectors in my application.
I am using
#include <vector>
in the header file, but when I compile it fails on this line:
std::vector<Shot> shot_list;
Noting the error E2316 'vector' is not a member of 'std'
If I then remove std::, It results in the Undefined symbol 'vector' compiler error message. Really at a loss with this one. Had no issues using
std::list<Shot> shot_list;
prior to using vectors.
Here is a simple example that fails to comile:
//---------------------------------------------------------------------------
#ifndef testclassH
#define testclassH
//---------------------------------------------------------------------------
#include <vector>
class TestClass {
private:
std::vector<int> testVect(1); // removing std:: and adding using namespace std; below the include for the vector it still fails to compile;
};
#endif
To me I don't see any difference between this and This Example
Without clarifying which namespace that the vector is in, you can not use "vector" by itself. (using namespace std;) Maybe you can paste your related code for more spesific help.
Edit:
You can not initialize the vector in the .h. You need to do it in .cpp maybe using the resize() function of the vector. This can be an option for you (using the constructor of the class):
#ifndef testclassH
#define testclassH
//---------------------------------------------------------------------------
#include <vector>
class TestClass {
private:
std::vector<int> testVect;
public:
TestClass()
{
testVect.resize(4);
}
};
#endif
The simple example that you have given compiles if you make the change.

Resources