sourceCpp() failed to compile if there is some space in R path
Closed this issue · 5 comments
When there is some space in R path, sourceCpp failed to compile:
library(Rcpp)
code2 = "#include <Rcpp.h>\n\nusing namespace Rcpp;\n\n// [[Rcpp::export]]\nint fibonacci(const int x) {\n if (x == 0) return(0);\n if (x == 1) return(1);\n return (fibonacci(x - 1)) + fibonacci(x - 2);\n }"
sourceCpp(code = code2)
It showed the error:
Error in system(cmd, intern = !showOutput) : 'd:/Program' not found
> sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Rcpp_1.0.9
loaded via a namespace (and not attached):
[1] compiler_4.2.1 tools_4.2.1
A possible solution is to add the command r <- paste0('"', r, '"')
after the command r <- paste(R.home("bin"), "R", sep = .Platform$file.sep)
.
After done that, it compiles normally.
> fibonacci(10)
[1] 55
Yes that line
Line 132 in 8a44ae1
is a little suspicious. We could use file.path()
instead of paste
and we use normalizePath
in a number of other places already but not here:
edd@rob:~/git/rcpp(master)$ ag normalizePath
R/tools.R
43: path <- normalizePath(path) # #nocov start
R/Attributes.R
39: cacheDir <- normalizePath(cacheDir)
50: rWorkingDir <- normalizePath(dirname(file))
54: file <- normalizePath(file, winslash = "/")
238: cppSourcePath <- normalizePath(cppSourcePath)
239: buildDirectory <- normalizePath(buildDirectory)
245: cacheFiles <- normalizePath(cacheFiles)
416: pkgdir <- normalizePath(pkgdir, winslash = "/")
465: cppFiles <- normalizePath(cppFiles, winslash = "/")
src/attributes.cpp
902: Rcpp::Function normalizePath = baseEnv["normalizePath"];
942: include = normalizePath(include, "/");
984: Rcpp::Function normalizePath = baseEnv["normalizePath"];
985: sourceFile = Rcpp::as<std::string>(normalizePath(sourceFile, "/"));
edd@rob:~/git/rcpp(master)$
Can you also try the alternate form
r <- normalizePath(file.path(R.home("bin"), "R"), winslash = "/")
which should work (but I don't have a Windows system handy to test.
Can you also try the alternate form
r <- normalizePath(file.path(R.home("bin"), "R"), winslash = "/")which should work (but I don't have a Windows system handy to test.
Thank you for the prompt response!
Yes, it worked, but there is a warning
> sourceCpp(code = code2)
Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
path[1]="d:/Program Files/R/R-4.2.1/bin/x64/R": The system cannot find the file specified
> fibonacci(10)
[1] 55
Add ".exe" after "R" helps to get rid of the warning
r <- normalizePath(file.path(R.home("bin"), "R.exe"), winslash = "/")
,
but of course it does not work on Linux or Mac.
Hence, maybe we should check the current OS like this?
if (.Platform$OS.type == "windows") {
r <- normalizePath(file.path(R.home("bin"), "R.exe"))
}
else {
r <- normalizePath(file.path(R.home("bin"), "R"))
}
Yes either that or maybe simpler to just add mustWork=FALSE
to suppress the warning? (See ?normalizePath
on that.)
Yes either than or maybe simpler to just add
mustWork=FALSE
to suppress the warning? (See?normalizePath
on that.)
Yes, it did, much simpler!
> sourceCpp(code = code2)
> fibonacci(10)
[1] 55
Splendid. I will get this taken care of. Thanks for the heads-up and the tests, much appreciated!