Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rcpp in markdown

Tags:

r

r-markdown

rcpp

I'm trying to use a function created using Rcpp package within Rmarkdown document. But the following results in an error:

```{Rcpp firstChunk}
Rcpp::IntegerVector doubleMe(Rcpp::IntegerVector x) {
  return x + x;
}
```

C:/Rtools/mingw_64/bin/g++ -I"C:/Users/JAKMIC~1/DOCUME~1/R/R-35~1.1/include" -DNDEBUG -I"C:/Users/jakmicha1/Documents/R/R-3.5.1/library/Rcpp/include" -I"C:/Users/jakmicha1/AppData/Local/Temp/RtmpQBQexm/sourceCpp-x86_64-w64-mingw32-0.12.18" -O2 -Wall -mtune=generic -c file17ec52d61f75.cpp -o file17ec52d61f75.o file17ec52d61f75.cpp:1:1: error: 'Rcpp' does not name a type Rcpp::IntegerVector doubleMe(Rcpp::IntegerVector x) { ^ make: *** [C:/Users/JAKMIC~1/DOCUME~1/R/R-35~1.1/etc/x64/Makeconf:215: file17ec52d61f75.o] Error 1 Error in Rcpp::sourceCpp(code = "Rcpp::IntegerVector doubleMe(Rcpp::IntegerVector x) {\n return x + x;\n}") : Error 1 occurred building shared library.

What may be a cause and how can I solve it?

Edit:

Thanks for all the replies. The code seems to work ok while running chunks. There is an error while knitting though.

---
title: "title"
output: pdf_document
---

```{Rcpp firstChunk}
#include <Rcpp.h>

//[[Rcpp::export]]
Rcpp::IntegerVector double2Me(Rcpp::IntegerVector x) {
  return x + x;
}
```

```{r callFirstChunkInR}
double2Me(c(2, 2))
```
# In command 'system(cmd)': 'make' not found

# Quitting from lines 7-13 (title.Rmd) 
# Error in command '(function (file = "", code = NULL, env = globalenv(), embeddedR = TRUE, ':
#  Error 1 occurred building shared library.
# Calls: <Anonymous> ... block_exec -> in_dir -> engine -> do.call -> <Anonymous>

I'm using Rmarkdown 1.10 within RStudio 1.1.456 on Windows 7 and Rcpp_0.12.19. Any ideas?

like image 580
jakes Avatar asked Jan 29 '26 13:01

jakes


1 Answers

The Rcpp chunks in R-markdown are equivalent to Rcpp::sourceCpp, not to Rcpp:cppFunction. You therefore have to specify the necessary includes and tell Rcpp to export the function:

```{Rcpp firstChunk}
#include <Rcpp.h>
//[[Rcpp::export]]
Rcpp::IntegerVector doubleMe(Rcpp::IntegerVector x) {
  return x + x;
}
```
like image 107
Ralf Stubner Avatar answered Jan 31 '26 05:01

Ralf Stubner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!