我尝试Knit HTML以下Rmd文件:

---
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);
}
```

我收到以下错误:
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

我做错了什么吗?与Windows有关吗?

来自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通过devtools::find_rtools()安装检查
[1] TRUE
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

最佳答案

所请求的 Sys.getenv['PATH'] 信息中不包含带有Rtools的路径,并且知道knitr错误是由无效的引擎路径触发的,我认为您已成为devtools::find_rtools()的受害者,在安装时抛出了误报。

通常是这种情况,因为如果无法在系统路径上找到Rtools,它将在注册表中扫描Rtools,然后设置一个环境标志。在运行rmarkdown或程序包构建阶段,环境标志通常不会持久存在。另请参阅:Why do I need to run find_rtools() before has_devel() = TRUE?

例如。如果关闭所有打开的 session R session ,然后打开一个新的 R session ,仅键入Rcpp::evalCpp("2 + 2"),您可能会触发编译错误。

解决方法很简单:Rtools安装位置添加到PATH系统变量中。 我维护着一个安装指南,该指南从字面上指导您逐步完成此过程:http://thecoatlessprofessor.com/programming/rcpp/install-rtools-for-rcpp/

从Rtools 3.4开始,必须添加到PATH中的两个位置是:

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

要在Windows上修改PATH变量,请参见:
  • 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?
  • 关于r - 无法在R Markdown中使用Rcpp引擎,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39456577/

    10-12 15:34