2017-08-10 21 views
0

RからVBSスクリプトを実行し、Rからそのスクリプトに値を渡したいとします。RからVBSスクリプトを実行してRからVBSに引数を渡す方法

たとえば、「Msg_Script.vbs」と呼ばれる単純なファイルで、私は、コードを持っている:Rでパラメータおよび/または変数の編集中に

Dim Msg_Text 

Msg_Text = "[Insert Text Here]" 

MsgBox("Hello " & Msg_Text) 

にはどうすれば、Rを使用して、このスクリプトを実行するのですか?たとえば、上記のスクリプトでは、Msg_Text変数の値をどのように編集しますか?

答えて

1

もう一つの方法は、argument to the VBScript

として値を渡すことであろう

次のようにVBSを書いてください。

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

そして、あなたは、このようにRでシステムコマンドを作成したい:

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

このアプローチは、位置によって、引数と一致します。 必要に応じて、代わりに名前付き引数を使用できます。この方法で、あなたのVBSは次のようになります。

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

そしてあなたは、このようなRでシステムコマンドを作成したい:

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

ここ幾分 - ハック解決策は次のとおり

は(readLines()を使用)RにVBSスクリプトから行を読む

vbs_lines <- readLines(con = "Msg_Script.vbs") 

編集特定のテキストを検索と置換することにより、Rのライン:

updated_vbs_lines <- gsub(x = vbs_lines, 
          pattern = "[Insert Text Here]", 
          replacement = "World", 
          fixed = TRUE) 

更新回線を使用して新しいVBSスクリプトを作成します。

をあなたはそれを実行した後

full_temp_script_path <- normalizePath("Temporary VBS Script.vbs") 
system_command <- paste0("WScript ", '"', full_temp_script_path, '"') 

system(command = system_command, 
     wait = TRUE) 

は、新しいスクリプトを削除します:

writeLines(text = updated_vbs_lines, 
      con = "Temporary VBS Script.vbs") 
は、システムコマンドを使用してスクリプトを実行します

file.remove("Temporary VBS Script.vbs") 
関連する問題