Skip to content

Instantly share code, notes, and snippets.

@ramhiser
Created July 15, 2012 21:35
Show Gist options
  • Save ramhiser/3118732 to your computer and use it in GitHub Desktop.
Save ramhiser/3118732 to your computer and use it in GitHub Desktop.
knitr Issue for Rmd and Rcpp

Rmd file to show the issue...

library(Rcpp)
library(inline)
## Loading required package: methods
# C++ code to calculate the length of a vector.
src <- "\nRcpp::NumericVector vec_x(x);\nint n = vec_x.size();\ninvisible(\".BeGiN_TiDy_IdEnTiFiEr_HaHaHa.HaHaHa_EnD_TiDy_IdEnTiFiEr\")\nreturn n;\n"

# R function to call the C++ code.
length_Rcpp <- cxxfunction(signature(x = "numeric"), src, plugin = "Rcpp")
## Warning: running command '/Library/Frameworks/R.framework/Resources/bin/R
## CMD SHLIB file16c24781aff2c.cpp 2> file16c24781aff2c.cpp.err.txt' had
## status 1
## file16c24781aff2c.cpp: In function ‘SEXPREC* file16c24781aff2c(SEXPREC*)’:
## file16c24781aff2c.cpp:33: error: ‘invisible’ was not declared in this scope
## file16c24781aff2c.cpp:34: error: expected `;' before ‘return’
## make: *** [file16c24781aff2c.o] Error 1
## 
## ERROR(s) during compilation: source code errors or compiler configuration errors!
## 
## Program source:
##   1: 
##   2: // includes from the plugin
##   3: 
##   4: #include <Rcpp.h>
##   5: 
##   6: 
##   7: #ifndef BEGIN_RCPP
##   8: #define BEGIN_RCPP
##   9: #endif
##  10: 
##  11: #ifndef END_RCPP
##  12: #define END_RCPP
##  13: #endif
##  14: 
##  15: using namespace Rcpp;
##  16: 
##  17: 
##  18: // user includes
##  19: 
##  20: 
##  21: // declarations
##  22: extern "C" {
##  23: SEXP file16c24781aff2c( SEXP x) ;
##  24: }
##  25: 
##  26: // definition
##  27: 
##  28: SEXP file16c24781aff2c( SEXP x ){
##  29: BEGIN_RCPP
##  30: 
##  31: Rcpp::NumericVector vec_x(x);
##  32: int n = vec_x.size();
##  33: invisible(".BeGiN_TiDy_IdEnTiFiEr_HaHaHa.HaHaHa_EnD_TiDy_IdEnTiFiEr")
##  34: return n;
##  35: 
##  36: END_RCPP
##  37: }
##  38: 
##  39: 
## Error: Compilation ERROR, function(s)/method(s) not created!
## file16c24781aff2c.cpp: In function ‘SEXPREC* file16c24781aff2c(SEXPREC*)’:
## file16c24781aff2c.cpp:33: error: ‘invisible’ was not declared in this
## scope file16c24781aff2c.cpp:34: error: expected `;' before ‘return’ make:
## *** [file16c24781aff2c.o] Error 1
x <- seq_len(10)

# Calculate the length of the generated vector.
length_Rcpp(x)
## Error: could not find function "length_Rcpp"
Rmd file to show the issue...
```{r rcpp}
library(Rcpp)
library(inline)
# C++ code to calculate the length of a vector.
src <- '
Rcpp::NumericVector vec_x(x);
int n = vec_x.size();
return n;
'
# R function to call the C++ code.
length_Rcpp <- cxxfunction(signature(x = "numeric"), src, plugin="Rcpp")
```
```{r run}
x <- seq_len(10)
# Calculate the length of the generated vector.
length_Rcpp(x)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment