Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sourceCpp() failed to compile if there is some space in R path #1234

Closed
kirimaru-jp opened this issue Oct 12, 2022 · 5 comments
Closed

sourceCpp() failed to compile if there is some space in R path #1234

kirimaru-jp opened this issue Oct 12, 2022 · 5 comments

Comments

@kirimaru-jp
Copy link

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
@eddelbuettel
Copy link
Member

eddelbuettel commented Oct 12, 2022

Yes that line

r <- paste(R.home("bin"), "R", sep = .Platform$file.sep)

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.

@kirimaru-jp
Copy link
Author

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"))
}

@eddelbuettel
Copy link
Member

eddelbuettel commented Oct 12, 2022

Yes either that or maybe simpler to just add mustWork=FALSE to suppress the warning? (See ?normalizePath on that.)

@kirimaru-jp
Copy link
Author

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

@eddelbuettel
Copy link
Member

Splendid. I will get this taken care of. Thanks for the heads-up and the tests, much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants