/ / Rcpp Beginner Error Message - rcpp

Rcpp Beginner Error Message - rcpp

Jestem początkujący dla Rcpp. Miałem ten komunikat o błędzieuruchom następujący kod R. Używam systemu Windows 10. "Błąd w kodzie kompilacji (f, code, language = language, verbose = verbose): Kompilacja BŁĄD, funkcja / funkcje / metody nie zostały utworzone! Wiadomość ostrzegawcza:"

incltxt <- "
int fibonacci(const int x) {
if (x == 0) return(0);
if (x == 1) return(1);
return fibonacci(x - 1) + fibonacci(x - 2);
}"


fibRcpp <- cxxfunction(signature(xs="int"),
plugin="Rcpp",
incl=incltxt,
body="
int x = Rcpp::as<int>(xs);
return Rcpp::wrap( fibonacci(x) );
")

Odpowiedzi:

2 dla odpowiedzi № 1

Rozważmy prostsze i nowsze cppFunction():

R> library(Rcpp)
R> cppFunction("int f(int n) { if (n < 2) return n; return f(n-1) + f(n-2);}")
R> f(10)
[1] 55
R>

Edytować: A tutaj jest twój naprawiony kod. Musisz również załadować Rcpp, aby jego wtyczka została zarejestrowana:

R> library(Rcpp)
R> library(inline)
R> incltxt <- "
+ int fibonacci(const int x) {
+ if (x == 0) return(0);
+ if (x == 1) return(1);
+ return fibonacci(x - 1) + fibonacci(x - 2);
+ }"
R> bodytxt <- "
+ int x = Rcpp::as<int>(xs);
+ return Rcpp::wrap( fibonacci(x) );
+ "
R> fibRcpp <- inline::cxxfunction(signature(xs="int"), incl=incltxt, body=bodytxt, plugin="Rcpp")
R> fibRcpp(10)
R> 55