带空格的 Rscript 文件路径

Gab*_*sky 6 whitespace cmd r rscript

我正在尝试在 Windows shell 中运行以下 R 脚本:

Rscript C:/Documents/Folder name containing space/myscript.txt
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我收到错误:

Fatal error: cannot open file 'C:/Documents/Folder': No such file or directory
Run Code Online (Sandbox Code Playgroud)

但是,当我使用引号时(如其他帖子中建议的那样尝试单双和三重),我收到以下错误:

Rscript "C:/Documents/Folder name containing space/myscript.txt"
The filename, directory name, or volume label syntax is incorrect.
Run Code Online (Sandbox Code Playgroud)

我找不到解决空间问题和更改文件位置的方法,因此没有空格对我来说不是一个选择。

任何帮助将不胜感激。

进一步说明:

我遇到的问题与 R 没有直接关系,而是与将包含空格的文件路径传递给 Rscript 相关。

在文档中,Rsript 应按以下方式使用:

Rscript [options] [-e expr [-e expr2 ...] | file] [args]
Run Code Online (Sandbox Code Playgroud)

还指出:

表达式和文件中允许有空格(但需要保护它不受正在使用的 shell 的影响,如果有的话,例如通过将参数括在引号中)。

但是,尝试将文件路径括在引号中会导致错误

The filename, directory name, or volume label syntax is incorrect.
Run Code Online (Sandbox Code Playgroud)

为避免混淆Rscript C:/Documents/Folder_name/myscript.txt当路径不包含任何空格时,运行可以正常工作Rscript "C:/Documents/Folder_name/myscript.txt"

Hen*_*ikB 1

正斜杠在 R 中工作得很好,所以不用担心反斜杠。我刚刚验证了以下内容在 Windows 8.1 的 CMD.exe 终端上有效:

C:\Windows\System32> Rscript "C:/Users/hb/folder with spaces/script.R"
[1] "1+2+3"

C:\Windows\System32>
Run Code Online (Sandbox Code Playgroud)

我最好的猜测是您的路径名不正确。如果它是一个不存在的路径名,您会得到:

C:\Windows\System32> Rscript --vanilla "C:/Users/hb/folder with spaces/non-existing.R"
Fatal error: cannot open file 'C:/Users/hb/folder with spaces/non-existing.R': No such file or directory
Run Code Online (Sandbox Code Playgroud)

您可以从 R 内部验证它,例如

> file.exists("C:/Users/hb/folder with spaces/script.R")
[1] TRUE
Run Code Online (Sandbox Code Playgroud)