9

I tried to Knit HTML the following Rmd file:

---
title: "Untitled"
author: "Florian Privé"
date: "12 septembre 2016"
output: html_document
---

```{r fibCpp, engine='Rcpp'}
#include <Rcpp.h>

// [[Rcpp::export]]
int fibonacci(const int x) {
    if (x == 0 || x == 1) return(x);
    return (fibonacci(x - 1)) + fibonacci(x - 2);
}
```

I got the following error:

Building shared library for Rcpp code chunk...
Warning message:
l'exécution de la commande 'make -f "C:/PROGRA~1/R/R-33~1.1/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-33~1.1/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="sourceCpp_2.dll" WIN=64 TCLBIN=64 OBJECTS="file110c1d4643e9.o"' renvoie un statut 127 


Quitting from lines 11-18 (test.Rmd) 
Error in (function (file = "", code = NULL, env = globalenv(), embeddedR = TRUE,  : 
  Error 1 occurred building shared library.
Calls: <Anonymous> ... block_exec -> in_dir -> engine -> do.call -> <Anonymous>
Exécution arrêtée

Am I doing something obviously wrong? Is it a problem related to Windows?

Environment Information from sessionInfo()

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252   
[3] LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] magrittr_1.5    rsconnect_0.4.3 htmltools_0.3.5 tools_3.3.1     yaml_2.1.13    
 [6] Rcpp_0.12.7     stringi_1.1.1   rmarkdown_1.0   stringr_1.1.0   digest_0.6.10  
[11] evaluate_0.9   

Rtools install check via devtools::find_rtools()

[1] TRUE

Results from Sys.getenv()['PATH']

## PATH                  C:\Program
##                       Files\R\R-3.3.1\bin\x64;C:\ProgramData\Oracle\Java\javapath;C:\Program
##                       Files\NVIDIA GPU Computing
##                       Toolkit\CUDA\v7.5\bin;C:\Program
##                       Files\NVIDIA GPU Computing
##                       Toolkit\CUDA\v7.5\libnvvp;;C:\Program Files
##                       (x86)\Intel\iCLS Client\;C:\Program
##                       Files\Intel\iCLS
##                       Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program
##                       Files (x86)\Windows Live\Shared;C:\Program
##                       Files\Intel\Intel(R) Management Engine
##                       Components\DAL;C:\Program
##                       Files\Intel\Intel(R) Management Engine
##                       Components\IPT;C:\Program Files
##                       (x86)\Intel\Intel(R) Management Engine
##                       Components\DAL;C:\Program Files
##                       (x86)\Intel\Intel(R) Management Engine
##                       Components\IPT;C:\Program Files
##                       (x86)\Skype\Phone\;C:\Users\Florian\.dnx\bin;C:\Program
##                       Files\Microsoft DNX\Dnvm\;C:\Program Files
##                       (x86)\NVIDIA
##                       Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Users\Florian\Anaconda3;C:\Users\Florian\Anaconda3\Scripts;C:\Users\Florian\Anaconda3\Library\bin;C:\Program
##                       Files
##                       (x86)\Java\jre1.8.0_101\bin\client;C:\texlive\2015\bin\win32
coatless
  • 20,011
  • 13
  • 69
  • 84
F. Privé
  • 11,423
  • 2
  • 27
  • 78
  • 1
    Only have time to note that your Rcpp example works fine on macOS, latest RStudio Preview, super-current Rcpp and R 3.3.1 – hrbrmstr Sep 12 '16 at 18:47
  • Sorry, I can't edit my post due to too much code in it apparently. You can get the results of `Sys.getenv()['PATH']` [there](https://privefl.github.io/R-presentation/system-env.html). Moreover, I opened an other project and `Rcpp::evalCpp("2 + 2")` is working fine. – F. Privé Sep 13 '16 at 06:51

5 Answers5

7

With the requested information of the Sys.getenv['PATH'] not containing a path with Rtools in it and the knowledge that the knitr error is being triggered by an invalid engine path, I think you are falling victim to devtools::find_rtools() throwing a false positive on setup.

This is typically the case since if it is unable to find Rtools on the system path, it scans for Rtools within the registry and then sets an environment flag. The environment flag does not typically persist while running rmarkdown or during the package build stage. Also see: Why do I need to run find_rtools() before has_devel() = TRUE?

E.g. If you close all open session R sessions, then open a new R session and only type Rcpp::evalCpp("2 + 2") you will likely trigger a compile error.

The fix for this is simple: Add the Rtools install location to the PATH system variable. I maintain an installation guide that literally takes you step-by-step through this process here: http://thecoatlessprofessor.com/programming/rcpp/install-rtools-for-rcpp/

As of Rtools 3.4, the two locations that must be added to the PATH are:

c:\Rtools\bin;
c:\Rtools\mingw_32\bin;

To modify your PATH variable on windows see either:

coatless
  • 20,011
  • 13
  • 69
  • 84
2

I also just experienced this issue. I see that it's been a couple years since this was last addressed, so I wanted to update with the solution that I've found in 2020.

I received that exact error that was mentioned at the top, and had into do a fresh install of Rtools. I originally had RBuildTools, which works for other packages I've built, but still had the issue when compiling the cpp code for this project. I installed Rtools40 and kept all of the defaults. Then changed the system path variable on my windows 10 machine to say C:\rtools40\usr\bin. After restarting the machine, knitr rendered the document without any further errors.

Rtools40 installation instructions can be found at this link

I hope that this helps!

aromatic6tet
  • 91
  • 1
  • 8
1

I experienced this error today on windows 10, and the outputs you included are as good as identical to those of mine.

Neither J_F's workaround or adding "c:\Rtools\bin" to paths through advanced system settings solved it.

What solved it for me was to uninstall Rtools and reinstall it, checking the option to change the paths during installation. I put the paths: "c:\Rtools\bin", "C:\Rtools\mingw_32\bin", "C:\Program Files\R\R-3.3.1\bin\i386" and "C:\Program Files\R\R-3.3.1\bin\x64" in there.

I wonder why adding "c:\Rtools\bin" to paths through advanced system settings didnt change the output of Sys.getenv()['PATH']

Coolwater
  • 703
  • 8
  • 15
1

I had exactly this issue when running an Rmarkdown {Rcpp} block on Windows 10. Rcpp had worked great in every other context.

  1. Install Rtools40 (as @aromatic6tet) said, so that it appears in C:\rtools40
  2. Restart machine
  3. Open Rstudio
  4. Type the following, note use of semicolon delimiter between path constant and use of double backslash. Sys.setenv(PATH = paste(Sys.getenv("PATH"), "C:\\rtools40\\usr\\bin", sep = ";"))

...and knitr now compiles Rcpp!

zdebruine
  • 3,687
  • 6
  • 31
  • 50
0

A workaround could be this:

---
title: "Untitled"
author: "Florian Privé"
date: "12 septembre 2016"
output: html_document
---

```{r}
Rcpp::cppFunction('
int fibonacci(const int x) {
    if (x == 0 || x == 1) return(x);
    return (fibonacci(x - 1)) + fibonacci(x - 2);
}') 
```

```{r}
fibonacci(10L)
```
# [1] 55
J_F
  • 9,956
  • 2
  • 31
  • 55
  • How exactly is that a workaround if, as we suspect, _he hasn't got a working Rcpp setup_ ? – Dirk Eddelbuettel Sep 13 '16 at 00:20
  • I have a running Rcpp setup on my machine and was not able to run this example https://github.com/yihui/knitr-examples/blob/master/029-engine-Rcpp.Rmd. So I presented the workaround above, because this works for me! Don´t no why, but that´s a fact. Perhaps the original poster has the same problems ... – J_F Sep 13 '16 at 06:27
  • Same error here. Another info that may be useful: I use Rcpp for [this in-progress package](https://github.com/privefl/bigsnpr). The `Check` is "stopping" with an endless checking of examples. And `Build & Reload` is giving me an error. But if I use `Check`, stop it before the examples and then use `Build & Reload`, it works.. This package builds on Travis-CI and win-builder. I don't know if this issue is related, otherwise just forget it. – F. Privé Sep 13 '16 at 07:02
  • My error was similar to this. It was occuring because I had `cache = TRUE` in knittr options. Apparently Rccp functions don't play nice with cache. So to fix it I put `cache = FALSE` in the code block defining the Rcpp function. – Gordon McDonald Mar 27 '18 at 07:06