【问题标题】:Cannot compile rstan or Rcpp code in Rstudio with windows 10无法在 Windows 10 的 Rstudio 中编译 rstan 或 Rcpp 代码
【发布时间】:2020-09-04 23:53:38
【问题描述】:

我无法在 Windows 上编译 Rcpprstan 代码。

当我尝试编译 rstan 程序(如下)时,我收到错误

编译代码中的错误(f,代码,语言 = 语言,详细 = 详细) : 在 C:/Users/thean/AppData/Local/Temp/Rtmp0kEsrn/downloaded_pa​​ckages/rJava_0.9-13.zip/Rcpp/include/RcppCommon.h:29 包含的文件中, 从 C:/Users/thean/AppData/Local/Temp/Rtmp0kEsrn/downloaded_pa​​ckages/rJava_0.9-13.zip/Rcpp/include/Rcpp.h:27,

来自文件18045ecf22e4.cpp:7:C:/Users/thean/AppData/Local/Temp/Rtmp0kEsrn/downloaded_pa​​ckages/rJava_0.9-13.zip/Rcpp/include/Rcpp/r/headers.h:71:10 : 致命错误:R.h:没有这样的文件或目录 #include
^~~~~编译终止。make: *** [C:/PROGRA~1/R/R-40~1.1/etc/x64/Makeconf:229:file18045ecf22e4.o] 错误 1

如果我尝试在Rstudio 中运行以下内容,它会重新下载 Rtools,但它已经被下载了

library(Rcpp); evalCpp("2+2") 

正在运行

Sys.which("make") 
"C:\\rtools40\\usr\\bin\\make.exe" 

此外,当我在命令窗口中运行Sys.which("make") 时,它只有"",而不是“C:\rtools40\usr\bin\make.exe”。当我运行library(Rcpp); evalCpp("2+2") 时,它会产生此胎面的原始错误


最初的问题与尝试编译 rstan 程序时收到错误有关。

我正在使用下面的代码

library(StanHeaders)

library(rstan) 
values = list(y = rnorm(1000,5,3)) 

model =" 

data { 
real y[1000]; 
} 

parameters { 
real mu; 
real sigma; 
} 

model { 
mu    ~ normal(0,10);   
sigma ~ normal(0,10);  
y     ~ normal(mu,sigma); 
} 
" 

fit <- stan(model_code = model, data = values, warmup = 500, iter = 1000, 
chains = 4, cores = 2, thin = 1) 
posterior = extract(fit) 

我不断收到此错误消息:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  In file included from C:/Users/thean/AppData/Local/Temp/Rtmp0kEsrn/downloaded_packages/rJava_0.9-13.zip/Rcpp/include/RcppCommon.h:29,                 from C:/Users/thean/AppData/Local/Temp/Rtmp0kEsrn/downloaded_packages/rJava_0.9-13.zip/Rcpp/include/Rcpp.h:27,                 
    from file18045ecf22e4.cpp:7:C:/Users/thean/AppData/Local/Temp/Rtmp0kEsrn/downloaded_packages/rJava_0.9-13.zip/Rcpp/include/Rcpp/r/headers.h:71:10: fatal error: R.h: No such file or directory #include <R.h>          ^~~~~compilation terminated.make: *** [C:/PROGRA~1/R/R-40~1.1/etc/x64/Makeconf:229: file18045ecf22e4.o] Error 1

当我转到此目录时,我没有 Rcpp.h:27,但我有 Rcpp.h。在所有文件上都是这样。 请帮忙。 谢谢


sessionInfo()

> R version 4.0.1 (2020-06-06) Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 18362)
> 
> Matrix products: default
> 
> locale: [1] LC_COLLATE=English_United States.1252 
> LC_CTYPE=English_United States.1252    LC_MONETARY=English_United
> States.1252 [4] LC_NUMERIC=C                          
> LC_TIME=English_United States.1252    
> 
> attached base packages: [1] stats     graphics  grDevices utils    
> datasets  methods   base     
> 
> other attached packages: [1] rstan_2.21.2         ggplot2_3.3.2       
> StanHeaders_2.21.0-6 Rcpp_1.0.5          
> 
> loaded via a namespace (and not attached):  [1] compiler_4.0.1    
> pillar_1.4.6       prettyunits_1.1.1  remotes_2.2.0      tools_4.0.1  
> pkgbuild_1.1.0      [7] jsonlite_1.7.0     lifecycle_0.2.0   
> tibble_3.0.3       gtable_0.3.0       pkgconfig_2.0.3    rlang_0.4.7  
> [13] cli_2.0.2          rstudioapi_0.11    curl_4.3          
> parallel_4.0.1     loo_2.3.1          gridExtra_2.3      [19]
> withr_2.2.0        dplyr_1.0.2        generics_0.0.2     vctrs_0.3.4  
> stats4_4.0.1       grid_4.0.1         [25] tidyselect_1.1.0  
> glue_1.4.2         inline_0.3.15      R6_2.4.1          
> processx_3.4.3     fansi_0.4.1        [31] callr_3.4.3       
> purrr_0.3.4        magrittr_1.5       codetools_0.2-16  
> matrixStats_0.56.0 scales_1.1.1       [37] ps_1.3.4          
> ellipsis_0.3.1     assertthat_0.2.1   colorspace_1.4-1   V8_3.2.0     
> RcppParallel_5.0.2 [43] munsell_0.5.0      crayon_1.3.4

【问题讨论】:

  • 你安装了 rtools 吗?
  • 27 指的是打印错误中引用的Rcpp.h 中的行号。 @user20650 的重点可能是您需要关注的地方,不过(rtools)
  • 欢迎来到 Stack Overflow。如果您使用的是 R 4.x,请从此处安装 RTools:cran.r-project.org/bin/windows/Rtools。如果您仍在使用 R 3.x,您可能需要 Rtools35.exe,您可以从 cran.r-project.org/bin/windows/Rtools/history.html 获得它
  • Rtools 4.0 已添加,我使用的是 Rstudio 而不是 R,但仍然收到相同的错误消息。
  • @JonBurksJr;你能确认你设置了 Rtools 的路径吗?请您确认library(Rcpp); evalCpp("2+2") 进行评估。

标签: r windows compilation rstan


【解决方案1】:

由于没有提及,可能只是路径问题。

将 Rtools 放在 PATH 上

安装完成后,您需要再执行一个步骤才能编译 R 包:您需要将 Rtools make 实用程序(bash、make 等)的位置放在 PATH 上。

尝试在 R 控制台中执行一次以下命令:

writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")

这最后一步is necessary after Rtools installation

【讨论】:

  • 感谢您展示此内容。该操作确实确认 Sys.which("make") 指向 rtools make 可执行文件,这表明它确实已添加到路径 as also suggested by the rtools page
  • @user20650, Sys.which("make") 根据原始帖子是“”,这使我的回答很合理。
  • @Jon,您是否尝试检查这是否有帮助?有兴趣知道;)
猜你喜欢
  • 2016-10-16
  • 2017-10-01
  • 2020-12-31
  • 2021-05-21
  • 1970-01-01
  • 2017-05-15
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多