r - Error in using Rcpp in Rmarkdown -
i want compare speed of ordinary r-loop , loop rcpp. need comparison small documentation , that's why have in use in rmarkdown. code is:
for_r <- function(n){ <- 0 for(i in 1:n){ = + } return(a) } rcpp::cppfunction(' int for_c(int n) { int = 0; for(int = 1; < (n+1); i++) { += i; } return a; }', showoutput = true) microbenchmark::microbenchmark('r-schleife' = for_r(10000), 'c-schleife' = for_c(10000))
the output cppfunction is:
## c:/progra~1/r/r-32~1.1/bin/x64/r cmd shlib -o "sourcecpp_71184.dll" "" "file30c7c6b798b.cpp"
i'm little bit confused following error appears when knitr file:
## error in sourcecpp(code = code, env = env, rebuild = rebuild, showoutput = showoutput, : error 1 occurred building shared library.
if run code in console works fine , without error. try knitr rmd file different directions, failed.
some information software:
- r version 2.3.1
- rtools installed in c:
- i use in combination rstudio version 0.99.465
i searched in other posts, everytime found error wasn't in rmarkdown. installed new operating system (windows 8.1) , after finishing , install programms error occurs. happy if help.
your code builds me (under linux). i'd use sourcecpp()
, put in .cpp file:
#include <rcpp.h> // [[rcpp::export]] int for_c(int n) { int = 0; for(int = 1; < (n+1); i++) { += i; } return a; } /*** r for_r <- function(n) { <- 0 for(i in 1:n){ = + } return(a) } microbenchmark::microbenchmark('r-schleife' = for_r(10000), 'c-schleife' = for_c(10000)) */
which, when sourcecpp()
-ed, produces result:
r> sourcecpp("/tmp/daniel.cpp") r> for_r <- function(n) { + <- 0 + for(i in 1:n){ + = + + } + return(a) + } r> microbenchmark::microbenchmark('r-schleife' = for_r(10000), + 'c-schleife' = for_c(10000)) unit: microseconds expr min lq mean median uq max neval r-schleife 4351.736 4680.954 6300.95103 4774.863 5166.111 70614.405 100 c-schleife 5.457 6.618 9.80134 9.746 11.471 50.236 100 r>
Comments
Post a Comment