Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a VBS script from R, while passing arguments from R to VBS

Tags:

r

vbscript

Let's say I want to run a VBS script from R, and I want to pass a value from R to that script.

For example, in a simple file called 'Msg_Script.vbs', I have the code:

Dim Msg_Text

Msg_Text = "[Insert Text Here]"

MsgBox("Hello " & Msg_Text)

How do I run this script using R, while editing the parameters and/or variables in R? In the above script for instance, how would I edit the value of the Msg_Text variable?

like image 463
bschneidr Avatar asked Oct 20 '25 15:10

bschneidr


1 Answers

Another way would be to pass the value as an argument to the VBScript

You'd write the VBS as follows:

Dim Msg_Text
Msg_Text = WScript.Arguments(0)
MsgBox("Hello " & Msg_Text)

And then you'd create a system command in R like this:

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

This approach matches the arguments by position. If you wanted, you could use named arguments instead. This way, your VBS would look like this:

Dim Msg_Text
Msg_Text = WScript.Arguments.Named.Item("Msg_Text")
MsgBox("Hello " & Msg_Text)

And then you'd create a system command in R like this:

system_command <- paste("WScript",
                        '"Msg_Script.vbs"',
                        '/Msg_Text:"World"',
                        sep = " ")
system(command = system_command,
       wait = TRUE)
like image 61
oracle certified professional Avatar answered Oct 23 '25 04:10

oracle certified professional



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!