Jeff Newmiller
2023-Aug-30 21:28 UTC
[R] Problems with installing R packages from source and running C++ in R, even on fresh R installation
TL:DR there are at least three maybe four ways to address this depending on what you plan to do. I usually adjust PATH to add Rtools using .Rprofile. But if you do that then if you want to use the command line to invoke R then you need to set the PATH separately when you start the shell. For this reason some people like to set it in the User Environment Variables control panel... but some Rtools programs conflict with some Cygwin tools so I don't like to set it globally that way since I use Cygwin a lot. Finally, I _think_ RStudio will try to look up your Rtools and adjust settings automagically from within RStudio, but only if you have never previously configured it in your environment variables. But that is only inferred from reports I have read that imply they never manually set anything and it "just works"... I have always set it up manually. If this sounds attractive then you might try making sure Rtools is NOT in your PATH before RStudio starts up _and_ that it isn't configured in your .Rprofile file. On August 30, 2023 12:10:28 PM PDT, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:>On 30/08/2023 2:59 p.m., Ivan Krylov wrote: >> On Wed, 30 Aug 2023 16:31:20 +0000 >> Christophe Bousquet <chr_bousquet at protonmail.com> wrote: >> >>> So, yes, it seems possible for R to localize paths related to >>> Rtools... But then, I really do not get where things go wrong... >> >> When installing packages containing code to compile, R eventually calls >> R CMD SHLIB. Same thing happens with inline C++: it gets stored in a >> temporary file, compiled into a *.dll using R CMD SHLIB and then loaded >> using dyn.load(). >> >> Write the following into a file named hello.c: >> >> #include <R.h> >> #include <Rinternals.h> >> SEXP hello(void) { >> SEXP ret = PROTECT(allocVector(STRSXP, 1)); >> SET_STRING_ELT(ret, 0, mkChar("hello")); >> UNPROTECT(1); >> return ret; >> } >> >> From within R, setwd() to the directory containing hello.c and run: >> >> tools::Rcmd('SHLIB -n hello.c') >> tools::Rcmd('SHLIB hello.c') >> >> What do the commands print? Does the second command fail? >> >> (Let's Cc: R-help at r-project.org in case people with more experience >> debugging Windows problems have a better idea what's going on.) >> > >It sounds exactly as though Rtools files aren't on the path that's in effect when R starts. That's probably the one stored in the registry. In the old days it was set in the System app in the Windows Control Panel under "Edit the system environment variables". I don't know if that's still true. > >Duncan Murdoch > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
Christophe Bousquet
2023-Aug-31 11:57 UTC
[R] Problems with installing R packages from source and running C++ in R, even on fresh R installation
> When installing packages containing code to compile, R eventually calls > R CMD SHLIB. Same thing happens with inline C++: it gets stored in a > temporary file, compiled into a *.dll using R CMD SHLIB and then loaded > using dyn.load(). > > Write the following into a file named hello.c: > > #include <R.h> > #include <Rinternals.h> > SEXP hello(void) { > SEXP ret = PROTECT(allocVector(STRSXP, 1)); > SET_STRING_ELT(ret, 0, mkChar("hello")); > UNPROTECT(1); > return ret; > } > > From within R, setwd() to the directory containing hello.c and run: > > tools::Rcmd('SHLIB -n hello.c') > tools::Rcmd('SHLIB hello.c') > > What do the commands print? Does the second command fail?I basically get no output from the two commands, apart from a new blank R prompt. The same is true when I run the commands from within R with reference to the other hello.c you asked me to create previously: #include <stdio.h> int main(void) { printf("Hello world!\n"); } However, I can still make this old hello.c print its message using Rtools43 bash with your previous code: export PATH=/x86_64-w64-mingw32.static.posix/bin:$PATH gcc -o hello.exe hello.c ./hello.exe But, if I try to apply this code to the new hello.c file, I get the following error:hello.c:1:10: fatal error: R.h: No such file or directory 1 | #include <R.h> | ^~~~~ compilation terminated. I also continued some potential diagnostics with the pkgbuild package. Here is the output: ```> library(pkgbuild) > rtools_path()[1] "C:\\rtools43/usr/bin"> has_rtools(debug = TRUE)Found in Rtools 4.3 installation folder [1] TRUE> check_rtools(debug = TRUE)Found in Rtools 4.3 installation folder [1] TRUE> has_compiler(debug = TRUE)Trying to compile a simple C file Running "C:/PROGRA~1/R/R-43~1.1/bin/x64/Rcmd.exe" SHLIB foo.c [1] FALSE> check_compiler(debug = TRUE)Trying to compile a simple C file Running "C:/PROGRA~1/R/R-43~1.1/bin/x64/Rcmd.exe" SHLIB foo.c Error: Failed to compile C code> rcmd_build_tools()Error in paste0("R CMD ", ..1) : the ... list contains fewer than 1 element In addition: Warning messages: 1: In is_string(left) : restarting interrupted promise evaluation 2: In is_string(left) : restarting interrupted promise evaluation ``` However, I am not sure how this is relevant for the issue at hand... Concerning the other answers, I am not sure how this could be related to RStudio: I test everything only using R without RStudio to avoid this extra layer of potential dependencies (but it also does not work if I try within RStudio anyway). I am also not sure how this could be related to the paths: on my personal laptop (on which everything works fine), the only paths related to R are RTOOLS43_HOME (C:/rtools43) and RTOOLS42_HOME (C:/rtools42). The same paths that are currently set on my work laptop. It was not working neither when I tried to add other paths related to R on my work laptop.
Maybe Matching Threads
- Problems with installing R packages from source and running C++ in R, even on fresh R installation
- Problems with installing R packages from source and running C++ in R, even on fresh R installation
- Problems with installing R packages from source and running C++ in R, even on fresh R installation
- Problems with installing R packages from source and running C++ in R, even on fresh R installation
- Problems with installing R packages from source and running C++ in R, even on fresh R installation