【问题标题】:Rscript file path with space带空格的 Rscript 文件路径
【发布时间】:2018-07-10 16:36:07
【问题描述】:

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

Rscript C:/Documents/Folder name containing space/myscript.txt

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

Fatal error: cannot open file 'C:/Documents/Folder': No such file or directory

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

Rscript "C:/Documents/Folder name containing space/myscript.txt"
The filename, directory name, or volume label syntax is incorrect.

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

任何帮助将不胜感激。

进一步说明:

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

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

Rscript [options] [-e expr [-e expr2 ...] | file] [args]

还需要注意的是:

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

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

The filename, directory name, or volume label syntax is incorrect.

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

【问题讨论】:

  • 这样可以Rscript C:/Documents/Folder\\ name\\ containing\\ space/myscript.txt吗?
  • 打开一个命令提示符窗口并运行cmd /?。最后一个帮助页面输出到控制台窗口(不是shell窗口)的最后一段解释说,文件名参数必须用双引号括起来,以包含空格或这些字符之一&()[]{}^=;!'+,`~。 Windows 上的目录分隔符是反斜杠字符\。不知道 Windows 上的 Rscript 是否期望文件名与 Unix/Linux/Mac 上的 / 相同。 Rscript.exe "C:\Documents\Folder name containing space\myscript.txt" 可能工作。 (我无法测试它,因为没有安装 Rscript。)
  • 感谢您的关注,但这并不能解决问题。我遇到的问题不在于 R 本身,而是通过 shell 传递文件路径。使用"file path" 不起作用,返回The filename, directory name, or volume label syntax is incorrect.。我正在寻找一种让 shell 传递包含空格的文件路径的方法。在文档中指定了 在表达式和文件中允许使用空格(但需要保护使用中的 shell,如果有的话,例如通过将参数括在引号中)。似乎没用。
  • @rashid 和 @lit 使用 \\ 并不能解决问题,而是返回 C:/Documents/Folder\\' is not recognized as an internal or external command, operable program or batch file
  • 您是否尝试将文件路径用引号括起来?所以Rscript "C:/Documents/Folder name containing space/myscript.txt"

标签: r cmd whitespace rscript


【解决方案1】:

这是 R 版本 3.5.0 for Windows 中的一个 BUG。
除了降级之外,一种解决方法是创建一个路径中没有空格的 R 脚本,并使用 source() 运行带空格的脚本:

## C:\Documents\Folder-name-no-space\myscript.txt
source("C:/Documents/Folder name containing space/myscript.txt")

然后你运行它:

Rscript C:\Documents\Folder-name-no-space\myscript.txt

也可以:

Rscript C:/Documents/Folder-name-no-space/myscript.txt

您也可以尝试 8.3 文件名。您可以通过以下方式获得它:

for %I in ("C:/Documents/Folder name containing space/myscript.txt") do @echo %~sI

更新

自 3.5.1 以来,该问题已得到修复。

【讨论】:

    【解决方案2】:

    正斜杠在 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>
    

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

    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
    

    您可以在 R 中验证它,例如

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

    【讨论】:

      【解决方案3】:

      简单的解决方案:安装更新版本的 R。

      来自version 3.5.1 release notes,此处描述了相关的错误修复:

      允许 Rscript 的文件参数包含空格,即使它是第一个在命令行上。

      【讨论】:

        【解决方案4】:

        另一个方便的解决方法是为包含空格的路径部分创建别名,即:

        SUBST k: "c:\Folder with Spaces"
        rscript k:\scripts\program.R
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-07
          • 1970-01-01
          • 1970-01-01
          • 2015-06-13
          • 2021-08-16
          • 2011-01-01
          • 2014-09-29
          • 1970-01-01
          相关资源
          最近更新 更多