【问题标题】:Running a Powershell script from R using system2() rather than system()使用 system2() 而不是 system() 从 R 运行 Powershell 脚本
【发布时间】:2019-04-28 15:29:05
【问题描述】:

我有一个 PowerShell 脚本(例如在 C:\directoryName\coolScript.ps1)。如果我想从 R 调用它,我可以运行

system('powershell -file "C:\\directoryName\\coolScript.ps1"')

如果我尝试对system2() 执行相同操作,它不会返回错误,但不会执行脚本。由于system() 命令的文档说system2()“推荐用于新代码”,我想使用system2()。有没有办法做到这一点?

【问题讨论】:

    标签: r powershell


    【解决方案1】:

    system() 不同,system2()command 参数始终由shQuote 引用,因此它必须是没有参数的单个命令,而参数通过args 参数传递给command .

    它们都有效:

    system("sed -i 's/oldword\\s/oldword/g' d:/junk/x/test.tex")
    system2("sed", args=c("-i", "s/oldword\\s/newword/g", "d:/junk/x/test.tex"))
    

    我会尝试:

    system2("powershell", args=c("-file", "C:\\directoryName\\coolScript.ps1"))
    

    您应该注意的另一件事是 R-3.2.1\bin\i386(32 位)和 R-3.2.1\bin 中有两个版本的 R 可执行文件\x64(64 位)。默认情况下,只有第一个安装在 32 位版本的 Windows 上,但都安装在 64 位操作系统上。 R 的 32 位版本将调用 PowerShell 的 32 位版本,64 位版本也是如此,因此请小心使用 Set-ExecutionPolicy 正确的。

    【讨论】:

    • 大概如果有人接到system('powershell ...') 的电话来工作,那么他们已经设置了相关的执行策略,但这一点对于让system2() 对来的人产生预期的效果至关重要这里没有有效的system() 电话。
    猜你喜欢
    • 2019-03-30
    • 2011-02-04
    • 1970-01-01
    • 2018-03-04
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    相关资源
    最近更新 更多