Can't use Rcpp engine in R Markdown

微笑、不失礼 提交于 2019-12-19 17:46:20

问题


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

回答1:


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:

  • How do I set system environment variables in Windows 10?
  • What are PATH and other environment variables, and how can I set or use them?



回答2:


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']




回答3:


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


来源:https://stackoverflow.com/questions/39456577/cant-use-rcpp-engine-in-r-markdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!