I am working with R and C++. I am trying to include C++ in R , I have the following code:
install.packages('inline')
install.packages('Rcpp')
install.packages('rstan')
library('inline')
library('Rcpp')
library('rstan')
### # start with pure C/C++ function and assign the source code to
### # a variable which we call here includesrc
includesrc <- '
int fibonacci(const int x){
if (x == 0) return(0);
if (x == 1) return(1);
return fibonacci(x-1) + fibonacci(x-2);
}'
### # define the body of the C/C++ program
fibCppBody <- '
int x = Rcpp::as<int>(xs);
return Rcpp::wrap( fibonacci(x) );'
### # pass the above C/C++ function as an argument
### # to cxxfunction()
fibRcpp <- inline::cxxfunction(sig = signature(xs = "int"),
plugin = "Rcpp",
incl = includesrc,
body = fibCppBody)
sapply(1:10, fibRcpp, USE.NAMES = FALSE)
I ran this code which is supposed to include C++ code in R function , it is supposed to run (it is an example from here but it's not working for me.
And I got this error:
Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) :
Warning message:In system(cmd) : 'make' not found
Any idea? Thanks
Related
I am trying to use these codes :
R:
library(Rcpp)
library(inline)
myplugin <- getPlugin("Rcpp")
myplugin$env$PKG_CXXFLAGS<-"-lsequence -lz"
cppFile2<-"Codes.cpp"
path<-"/home/samman/Documents/publishing/popstructMeta/LibseqR/"
sourceCpp(paste(path,cppFile2,sep=""))
basic_function()
C++
#include <Rcpp.h>
#include <Sequence/Translate.hpp>
// [[Rcpp::export]]
Rcpp::StringVector basic_function( ) {
std::string codon;
codon.resize(3);
// add code here
codon[0] = 'A';
codon[1] = 'G';
codon[2] = 'C';
Sequence::Translate(codon.begin(),codon.end());
return Rcpp::StringVector::create( "foo", "bar" ) ;
}
The same code is running very good in g++ after adding to the .bashrc file "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib".
The R code gives me this error :
Error in dyn.load("/tmp/RtmpJZm4Sc/sourceCpp-x86_64-pc-linux-gnu-1.0.10/sourcecpp_1257a68dd7ef5/sourceCpp_2.so") :
unable to load shared object '/tmp/RtmpJZm4Sc/sourceCpp-x86_64-pc-linux-gnu-1.0.10/sourcecpp_1257a68dd7ef5/sourceCpp_2.so':
/tmp/RtmpJZm4Sc/sourceCpp-x86_64-pc-linux-gnu-1.0.10/sourcecpp_1257a68dd7ef5/sourceCpp_2.so: undefined symbol: _ZN8Sequence9TranslateEN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESA_NS_12GeneticCodesERS2_
Calls: sourceCpp -> source -> withVisible -> eval -> eval -> dyn.load
I'm new using Lua in C++. I have some problem with printing error of script.
My purpose is :
Find errors of syntax, function name using luaL_dofile or luaL_dostring without running code like
build codes in Visual studio.
(When using luaL_loadfile or luaL_loadstring, it just find syntax error like 'a == 1', but can't find function name error like 'prit(a)')
Want to print all errors.(In lua, if there was 2 or more errors, it only prints error that was occurred first)
For example I made lua script like this :
test.lua
function sayHello(name)
print("Hello, " .. name .. "!")
end
for a=0, 10
print(a)
end
sayHello()
and in c++ code :
test.cpp
...
int ret;
ret = luaL_dofile(LuaState, filename);
if(ret != 0) {
cout << "error : " << lua_tostring(LuaState, -1) << endl;
lua_pop(LuaState, 1);
}
I want to print errors like :
lua: test.lua:2: attempt to concatenate local `name' (a nil value)
lua: test.lua:7: `do' expected near `print'
Could anybody teach me?
I am having some trouble working through the examples inside:
http://dirk.eddelbuettel.com/code/rcpp/Rcpp-modules.pdf
Specifically, section 2.2 where the author goes to expose a C++ class in RCPP and making it available to call in R.
I copied the code in to a new RCPP file and sourced it with no problem but got an error when I ran the following in R:
require(Rcpp)
require(inline)
unif_module <- Module( "unif_module", getDynLib(fx_unif ) )
The error:
Error in getDynLib(fx_unif) :
error in evaluating the argument 'x' in selecting a method for function 'getDynLib': Error: object 'fx_unif' not found
I then went though the other parts of the document where getDynLib is used and found that none of them initialized the arguments passed in to getDynLib. My question is what is fx_unif?
Thanks,
Update:
So following another example within the paper i tried:
inc = '
class Uniform {
public:
Uniform(double min_, double max_) : min(min_), max(max_) {}
NumericVector draw(int n) const {
RNGScope scope;
return runif( n, min, max );
}
double min, max;
};
double uniformRange( Uniform* w) {
return w->max - w->min;
}
RCPP_MODULE(unif_module) {
class_<Uniform>( "Uniform" )
.constructor<double,double>()
.field( "min", &Uniform::min )
.field( "max", &Uniform::max )
.method( "draw", &Uniform::draw )
.method( "range", &uniformRange )
;
}
'
fx_unif = cxxfunction(signature(), plugin="Rcpp", include=inc)
error:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! Warning message:
running command 'make -f "C:/PROGRA~1/R/R-32~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-32~1.2/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="filef4028245a6f.dll" WIN=64 TCLBIN=64 OBJECTS="filef4028245a6f.o"' had status 127
In addition: Warning message:
running command 'C:/PROGRA~1/R/R-32~1.2/bin/x64/R CMD SHLIB filef4028245a6f.cpp 2> filef4028245a6f.cpp.err.txt' had status 1
I'm trying to reproduce an R example based in R help.
This is an example for cxxfunction from inline package.
require(Rcpp)
require(inline)
# Rcpp plugin
if( require( Rcpp ) ){
fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
return wrap( as<int>(x) * as<double>(y) ) ;
', plugin = "Rcpp" )
fx( 2L, 5 )
## equivalent shorter form using rcpp()
fx <- rcpp(signature(x = "integer", y = "numeric"),
' return wrap( as<int>(x) * as<double>(y) ) ; ')
}
I got this message:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! Warning message:
running command 'make -f "C:/PROGRA~1/R/R-30~1.3/etc/i386/Makeconf" -f "C:/PROGRA~1/R/R-30~1.3/share/make/winshlib.mk" ....
And in addition:
Warning message:
running command 'C:/PROGRA~1/R/R-30~1.3/bin/i386/R CMD SHLIB file3a86e316ef8.cpp ...
I'm using:
platform i386-w64-mingw32 and R-3.0.3
As Kevin said, its easier to use attributes. The attribute // [[Rcpp::export]] does all the work for you.
With the current versions on R and Rcpp installed create a file called test.cpp to put this code:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double fx(int x, double y){
return x*y;
}
/*** R
fx(2L,5)
fx(2L,5.1)
*/
Then in a R session run: Rcpp::sourceCpp('test.cpp')
That should work, if you have followed the instructions for installing R, R-tools and set PATH variables (if on windows) correctly.
I'm currently trying to compile a Matlab function into an exe file and I'm having trouble to get rid on the Inputdlg part.
The original Matlab code is :
prompt={'Charge équivalente TNT :','X foyer :','Y foyer :','Z foyer :'};
title='Données';
answer=inputdlg(prompt,title);
Wcharge = str2double(answer{1});
Xfoyer = str2double(answer{2});
Yfoyer = str2double(answer{3});
Zfoyer = str2double(answer{4});
valide1 = ~ isempty(Wcharge) && Wcharge>0 && isnumeric(Wcharge);
valide2 = ~ isempty(Xfoyer) && isnumeric(Xfoyer);
valide3 = ~ isempty(Yfoyer) && isnumeric(Yfoyer);
valide4 = ~ isempty(Zfoyer) && isnumeric(Zfoyer);
check = valide1 + valide2 +valide3 + valide4;
if check < 4
disp('Données incorrectes')
return
else
end
As you now I need to get rid of the curly brackets to compile this code and I can't find an alternative to prompt={'Charge équivalente TNT :','X foyer :','Y foyer :','Z foyer :'};.
Do you have any suggestion on how to proceed ?