1

Since I have an umlaut (special character) in the path, sourcing a cpp file using Rcpp throws the error that the file is not found.

I am working in R-Studio under Windows. Sourcing R files works well when I use the function source, since it allows to set the encoding.

source_dir <- "D:/path/with/umläut/inside"
source(paste0(source_dir, "/some_r_file.R"), encoding = "UTF-8")

However, when I source a cpp file:

Rcpp::sourceCpp(paste0(source_dir, "/portfolio.cpp"))

I get the following error:

Error in Rcpp::sourceCpp(paste0(source_dir, "/portfolio.cpp"))
  file not found: 'D:/path/with/umläut/inside/portfolio.cpp'

This is because under the current encoding ä in the variable source_dir is interpreted as ä. I had the same issue with the function source and the encoding = "UTF-8" option solved it. However, it seems that the function Rcpp::sourceCpp does not allow the encoding and I don't know how to set it.

Any idea on how can I get around this?

Phil
  • 7,287
  • 3
  • 36
  • 66
  • That may be a Windows limitation, see e.g. [this answer](https://stackoverflow.com/questions/30829364/open-utf8-encoded-filename-in-c-windows). Rcpp uses `ifstream` and `ofstream`, you could try to patch it but maybe that is just not worth it (as we'd have to carry the patch). I have an Umlaut in my name too and even my machine-readable ID always transposed it to 'ascii' just as I generally use it now... – Dirk Eddelbuettel Oct 26 '21 at 12:14
  • To add, R has recommended (in the R-for-Windows FAQ if memory serves) to 'not install in path with spaces' (yet the installer oddly defaults / defaulted to it). UTF-8 chars are not explicitly listed in that rule, but we should as much as possible follow that rule. – Dirk Eddelbuettel Oct 26 '21 at 12:58
  • Thank you Dirk. I understand that you are trying to point me in the right direction. The answer in your link seems to not apply to my case because the file path is defined in the R code and not in the c++ code. Also, I totally agree with your recommendation to not use special characters or spaces in a path, but I have to adapt to the path defined by somebody else. – Domenico Spidy Tamburro Oct 27 '21 at 18:39
  • All the hard work is done by C++ code in the file `src/attributes.cpp` (in the `Rcpp` sources). I think your easiest way around it is a local helper function (in R) which copies you files to, say, `C:/TEMP` or then uses `Rcpp` to read and process it from a non-utf8 path via `sourceCpp()` and friends. – Dirk Eddelbuettel Oct 27 '21 at 19:08

0 Answers0