3

I am not managing to compile Rcpp or rstan code on Windows.

When I try to compile an rstan program (below) I receive the error

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

If I try to run the following within Rstudio it redownloads Rtools but it has been already downloaded

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

And running

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

Additionally, when I run Sys.which("make") in the command window it just has "", and not the "C:\rtools40\usr\bin\make.exe". When I run library(Rcpp); evalCpp("2+2") it produces the original error of this tread


The original question was related to receiving an error when trying to compile an rstan program.

I'm using the code below

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) 

I keep getting this error message:

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

When I go to this directory I don't have Rcpp.h:27 but I do have Rcpp.h. Its like this on all the files. Please help. Thanks


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
user20650
  • 24,654
  • 5
  • 56
  • 91
  • 2
    Did you install rtools? – user20650 Sep 05 '20 at 00:58
  • 27 refers to the line number in `Rcpp.h` referenced in the error printed. @user20650 's point is probably where you need to focus, though (rtools) – duckmayr Sep 05 '20 at 01:25
  • Welcome to Stack Overflow. If you are using R 4.x install RTools from here: https://cran.r-project.org/bin/windows/Rtools/. If you are still using R 3.x you will want probably want Rtools35.exe which you can get from https://cran.r-project.org/bin/windows/Rtools/history.html – itsMeInMiami Sep 05 '20 at 01:40
  • Rtools 4.0 is added and I'm using Rstudio not R and I'm still getting the same error message. – Jon Burks Jr Sep 05 '20 at 11:35
  • @JonBurksJr; can you confirm that you set the path to Rtools. Can you confirm that `library(Rcpp); evalCpp("2+2")` evaluates please. – user20650 Sep 05 '20 at 11:42
  • ... (ps you are using R , rstudio is an IDE / wrapper to make using R a bit more user friendly) – user20650 Sep 05 '20 at 11:49
  • All this does is library(Rcpp); evalCpp("2+2") redownloads Rtools when it is already downloaded – Jon Burks Jr Sep 06 '20 at 11:13
  • @JonBurksJr; okay thanks. If that example cannot compile then it may suggest that rtools is not on your path. What does `Sys.which("make")` return? I have seen examples on here with there being a few false negatives with rtools and rstudio -- does the small Rcpp example work using r from the terminal on the windows gui? – user20650 Sep 06 '20 at 16:48
  • It says Sys.which("make") "C:\\rtools40\\usr\\bin\\make.exe" – Jon Burks Jr Sep 06 '20 at 22:41
  • okay, good then it seems that rtools is on your path. Did you try running the small Rcpp example in the terminal or in the standard windows gui. Just trying to troubleshoot – user20650 Sep 06 '20 at 23:01
  • in the standard windows I think – Jon Burks Jr Sep 06 '20 at 23:28
  • @JonBurksJrl can you be clear please. Did you run `library(Rcpp); evalCpp("2+2")` in the Windows gui and/or the command line i.e. not within rstudio --> asking as it may be an issue with rstudio not seeing rtools for some reason rather than an r/rtools issue – user20650 Sep 06 '20 at 23:58
  • I ran it in Rstudio – Jon Burks Jr Sep 07 '20 at 00:18
  • Can you run it in the standard window gui or in the terminal please. This way you can isolate if it is an issue with rstudio rather than r. – user20650 Sep 07 '20 at 11:07
  • when I run Sys.which("make") make in the command window it just has "", not the "C:\\rtools40\\usr\\bin\\make.exe" when I run library(Rcpp); evalCpp("2+2") ....it produces the original error of this tread – Jon Burks Jr Sep 07 '20 at 17:03

1 Answers1

0

As it wasn't mentionned, it could simply be a path problem.

Putting Rtools on the PATH

After installation is complete, you need to perform one more step to be able to compile R packages: you need to put the location of the Rtools make utilities (bash, make, etc) on the PATH.

Try to execute once in R console following command:

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

This last step is necessary after Rtools installation.

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • Thanks for showing this. The op did confirm that `Sys.which("make")` directed to the rtools `make` executable which would suggest it has indeed been added to the path [as also suggested by the rtools page](https://cran.r-project.org/bin/windows/Rtools/) – user20650 Sep 09 '20 at 12:43
  • @user20650, `Sys.which("make")` is "" according to original post, which makes my answer quite plausible. – Waldi Sep 09 '20 at 18:48
  • @Jon, did you try to check if this helps? Interested to know ;) – Waldi Sep 11 '20 at 21:13