当然是一个老问题,但我找不到解决方案(也许没有)。在 Unix 上,直接使用 R 函数 file.path
获取某个文件的路径。当路径中的空格以 ~
返回时,如何在 Windows 下完成同样的事情。
如果我需要写,将 Rscript.exe 的路径说成一个文件,这将在 unix 上工作:
x <- list.files(R.home("bin"), full.names = T, pattern = "Rscript")
writeLines(x, con = "path_to_rscript.txt")
在 Windows 上,结果是:
C:/PROGRA~1/R/R-35~1.1/bin/x64/Rscript.exe
我会想要这样的东西:
C:/Program Files/R-3.5.1/bin/x64/Rscript.exe
有没有办法规避这种行为(大写的 PROGRA 是什么?)。
最佳答案
确实,结账 normalizePath :
normalizePath(path, winslash = "\\", mustWork = NA)
明确指出:
On Windows it converts relative paths to absolute paths, converts short names for path elements to long names and ensures the separator is that specified by winslash. It will match paths case-insensitively and return the canonical case. UTF-8-encoded paths not valid in the current locale can be used.
关于R,在 Windows 中获取文件名中的完整文件路径字符串(空格等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51927755/