1

I have an R-script, which runs a VBS.

VB-Script should pass arguments back to R.

I created the codes, but passed argument is still NA.

R:

path <- "C:\\Users\\PD\\Desktop\\Dashboard Citi R\\test\\scripcik.vbs"

shell(shQuote(normalizePath(path)), "cscript", flag = "//nologo")

args<-commandArgs(TRUE)
myvar<-args[1]
print(myvar)

VBS:

dim myArr
Dim shell
Set shell = CreateObject("WScript.Shell")
chartpath6 = "C:\Users\PD\Desktop\Dashboard Citi R\test\bazy\" & myDate(now) & ".accdb"
chartpath5 = "C:\Users\PD\Desktop\Dashboard Citi R\test\bazy\" & myDate(now)-1 & ".accdb"

myArr = Array(chartpath6,chartpath5) 

for i = 0 to 1
    ReportFileStatus(myArr(i))
next

sub ReportFileStatus(filespec)
   Dim fso, msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.FileExists(filespec)) Then
      msg = filespec & " exists."
   Else
      msg = filespec & " doesn't exist."
   End If
   msgbox msg
End sub

Function myDate(dt)
    dim m,y
    m = right("0" & datePart("m",dt),2)
    y = datePart("yyyy",dt)
    myDate=  y & m
End Function

Dim path

path = "Rscript C:\Users\PD\Desktop\Dashboard Citi R\test\runR.R " & msg

shell.Run(path)
M--
  • 25,431
  • 8
  • 61
  • 93
Geron
  • 85
  • 1
  • 7
  • 1
    What value are you trying to pass to the Rscript? What's the desired behavior here? As far as I can tell you never initialize `msg` in your vbs script. Which script are you running first? Looks like you're setting up an infinite loop of one calling the other It would help if you provided a more [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jun 19 '17 at 21:05
  • I want receieve msg string as output. First i run R script, which open VBS. VBS pass msg string to R console. – Geron Jun 19 '17 at 21:19
  • There is not infinite loop. There is create Array(0 to 1) and loop through two elements. – Geron Jun 19 '17 at 21:20
  • After `shell(shQuote(normalizePath(path)), "cscript", flag = "//nologo")` you need to wait till VBS returns the argument and then Run remaining of the R code. Right? – M-- Jun 19 '17 at 21:27
  • Perhaps, but adding Sys.sleep(10) didn't help. – Geron Jun 19 '17 at 21:31
  • 1
    I've run other scripts with Excel. R awaits till macro/script will be entirely executed. – Geron Jun 19 '17 at 21:34
  • How do you want to pass the value of msg? values for both chartpath6 and chartpath5 combined, or individually? – Flakes Jun 20 '17 at 13:04

1 Answers1

0

If the variable you try to send is a text message, you can write it to a txt file and loop from R until value of the text file not null or empty (I nerver tried to code with R) and then use the value of the text file.

(I also tried wscript.echo "MYTEXT" and run it from command line but it poped a msgbox).

Tommy
  • 74
  • 1
  • 1
  • 9
  • Yes, I thought about that way, but first I would like to check there is other way to pass variable directly to R from VBS. – Geron Jun 21 '17 at 17:36
  • Maybe that could help: https://stackoverflow.com/questions/187040/how-do-i-return-an-exit-code-from-a-vbscript-console-application (with this line: WScript.Quit(returnValue)) – Tommy Jun 21 '17 at 18:45