Markdown中使用Rcpp引擎

Markdown中使用Rcpp引擎

本文介绍了无法在R Markdown中使用Rcpp引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试Knit HTML以下Rmd文件:

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

我遇到以下错误:

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有关吗?

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

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

通过devtools::find_rtools()

进行Rtools安装检查

Rtools install check via devtools::find_rtools()

[1] TRUE

Sys.getenv()['PATH']

的结果

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

推荐答案

包含 Sys.getenv['PATH'] 中不包含带有Rtools的路径,并且知道knitr错误是由无效的 engine 路径触发的,我认为您成为 devtools :: find_rtools()的受害者a>在安装时抛出误报.

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.

通常是这样,因为如果找不到 Rtools 在系统路径上,它会扫描注册表中的 Rtools ,然后设置环境标志.在运行rmarkdown或程序包构建阶段,环境标志通常不会持久存在.另请参阅:为什么需要在has_devel()= TRUE之前运行find_rtools()吗?

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?

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

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.

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

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/

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

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

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

要在Windows上修改PATH变量,请参见:

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?

这篇关于无法在R Markdown中使用Rcpp引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 14:43