R,在 Windows 中获取文件名中的完整文件路径字符串(空格等)

标签 r windows file

当然是一个老问题,但我找不到解决方案(也许没有)。在 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/

相关文章:

r - data.table 按组外连接

c# - 是否有更多的快捷方式,如 %appdata%?

c# - Java 等效于 C# 'using' 语句

windows - 在 Windows Server 2016 中每分钟安排一个任务

windows - 如何在Windows 10中的perl> = 5.18中强制为输出文件设置代码集cp1252?

c# - 如何使用 .NET 创建具有特定扩展名的临时文件?

c++ - 如何期望来自重定向和用户输入的输入

html - 以本地语言显示内容 : R

根据日期字段和另一个字段删除 R 数据框中的重复行

r - 默认情况下让 RStudio 在 RRO 上运行