title: Use VBA to call R code
body: <p>I am working on a project that I need to use VBA as front end to call R as back end. Apparently, there is no bug warning but I do not get my output.</p>
  
  <p>Based on VBA, I ask R to read an external csv file and then write a table csv as output. But it does not do the job.</p>
  
  <p>Here is my code VBA and R.</p>
  
  <p>VBA####</p>
  
  <pre class="lang-vb prettyprint-override"><code>Sub RunRscript()
      'runs an external R code through Shell
      'The location of the RScript is 'C:\R_code'
      'The script name is 'hello.R'
  
      Dim shell As Object
      Set shell = VBA.CreateObject("WScript.Shell")
      Dim waitTillComplete As Boolean: waitTillComplete = True
      Dim style As Integer: style = 1
      Dim errorCode As Integer
      Dim path As String
      path = "RScript ""I:\des\Perso\Xiaofei\try.R"""
      errorCode = shell.Run(path, style, waitTillComplete)
  
  End Sub
  </code></pre>
  
  <p>R#####</p>
  
  <pre><code>a &lt;- read.table("German_ZCY_frombloomberg.csv",sep = ",",header = TRUE)
  a &lt;- data.frame(a)
  
  write.table(a,"I:/des/Perso/Xiaofei/hello.csv", quote = FALSE, row.names  =FALSE, col.names = TRUE, sep = ";")
  </code></pre>
  
  <p>I do not know where is the problem since no warning happend. But I can not see the csv file "hello.csv" created.</p>
  
  <p>On the other hand, if I use another R code to simply write a phrase into a text file with the following code, it works very well(VBA is able to let R do the job)</p>
  
  <p>R##</p>
  
  <pre><code> sink('I:/des/Perso/Xiaofei/hello.txt',append=F,type="output")
   cat('Hello World')
   var1&lt;-5^3
   var2&lt;-7^3
   cat('\nThe result of adding',var1,'to',var2,'is',var1+var2)
   sink(NULL)
  </code></pre>
  
  <p>Could you please help me with this problem? Thank you very much,</p>
  
  <p>Best regards,</p>
  
  <p>Xiaofei,</p>