如何从 R 运行 VBS 脚本,同时将参数从 R 传递到 VBS

bsc*_*idr 4 vbscript r

假设我想从 R 运行一个 VBS 脚本,并且想将一个值从 R 传递到该脚本。

例如,在一个名为“Msg_Script.vbs”的简单文件中,我有以下代码:

Dim Msg_Text

Msg_Text = "[Insert Text Here]"

MsgBox("Hello " & Msg_Text)
Run Code Online (Sandbox Code Playgroud)

如何使用 R 运行此脚本,同时在 R 中编辑参数和/或变量?例如,在上面的脚本中,我将如何编辑Msg_Text变量的值?

ora*_*nal 5

另一种方法是将值作为参数传递给 VBScript

您可以按如下方式编写 VBS:

Dim Msg_Text
Msg_Text = WScript.Arguments(0)
MsgBox("Hello " & Msg_Text)
Run Code Online (Sandbox Code Playgroud)

然后你可以在 R 中创建一个系统命令,如下所示:

system_command <- paste("WScript",
                        '"Msg_Script.vbs"',
                        '"World"',
                        sep = " ")
system(command = system_command,
       wait = TRUE)
Run Code Online (Sandbox Code Playgroud)

这种方法按位置匹配参数。如果您愿意,可以改用命名参数。这样,您的 VBS 将如下所示:

Dim Msg_Text
Msg_Text = WScript.Arguments(0)
MsgBox("Hello " & Msg_Text)
Run Code Online (Sandbox Code Playgroud)

然后你可以在 R 中创建一个系统命令,如下所示:

system_command <- paste("WScript",
                        '"Msg_Script.vbs"',
                        '/Msg_Text:"World"',
                        sep = " ")
system(command = system_command,
       wait = TRUE)
Run Code Online (Sandbox Code Playgroud)