1
votes

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?

4
You missed a few thousand live examples out there, including (of course) at the Rcpp Gallery which runs off Rcpp code examples written in Rmarkdown. - Dirk Eddelbuettel
You need to install Rtools 3.5 in the default location C:/Rtools, c.f. thecoatlessprofessor.com/programming/… - coatless
I already have it installed there. - jakes
Make sure that the values the Rtools tries to insert on your path are at the beginning then. - coatless

4 Answers

4
votes

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;
}
```
4
votes

You have to #include <Rcpp> in your code chunk, and most likely, add the // [[Rcpp:export]] directive to your function as well.

Like this:

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

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

You just omitted the inclusion of the Rcpp.h and the [[Rcpp::export]] attribute to create and link the function into R.

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

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

In another chunk, you can call the Rcpp function with the engine R.

```{r callFirstChunkInR}
doubleMe(c(2, 2))
```

Additional information on using different engines in R Markdown documents can be found at:

R Markdown: The Definitive Guide - Chapter 2, Section 2.7 Other language engines

Examples with Rcpp in R Markdown

You can find some of my previous so answer RMarkdown files that use Rcpp here:

https://github.com/coatless/so

Render of RMarkdown Document

enter image description here

1
votes

I have a related problem, which seems to be caused by the default chunk header inserted by RStudio Ver 1.1.442 for Rcpp chunks in an RMarkdown file.

Borrowing from the OPs question above, this doesn't work:

```{rcpp firstChunk}
#include <Rcpp.h>
using namespace Rcpp;
// Function declaration with export tag
// [[Rcpp::export]]
Rcpp::IntegerVector doubleMe(Rcpp::IntegerVector x) {
  return x + x;
}
```  

RStudio produces the following error message when I attempt to run the chunk:

'rcpp' is not recognized as an internal or external command, operable program or batch file.

However, this works fine:

```{Rcpp firstChunk}
#include <Rcpp.h>
using namespace Rcpp;
// Function declaration with export tag
// [[Rcpp::export]]
Rcpp::IntegerVector doubleMe(Rcpp::IntegerVector x) {
  return x + x;
}
```  

The only difference between the two pieces of code are "Rcpp" and "rcpp" in the chunk headers.