title: R Script execution from inside VBA using Shell not working
body: <p>I have an R script that is working perfectly when I execute it from R Studio, but when I am trying to run it using a VBA code that calls Shell, the code runs, the command window shows up and closes, but it does not generate the result file. It does not throw any error. Can someone see what the problem is?</p>
  
  <p>This is the address of the folder that has the <code>Rscript.exe</code> file: <code>C:\Program Files\R\R-3.4.4\bin\x64\</code></p>
  
  <p><strong>VBA Code:</strong></p>
  
  <pre class="lang-vb prettyprint-override"><code>Sub RunRScript()
      Dim shell As Object
  
      Dim waitTillComplete As Boolean: waitTillComplete = True
      Dim style As Integer: style = 1
      Dim errorCode As Integer
      Dim path As String
      Set shell = VBA.CreateObject("WScript.Shell")
  
      path = """C:\Program Files\R\R-3.4.4\bin\x64\RScript"" C:\Ibos\R\WF_Metrics\abc.R"
  
      errorCode = shell.Run(path, style, waitTillComplete)
  
  End Sub
  </code></pre>
  
  <p><strong>R Script:</strong></p>
  
  <pre class="lang-r prettyprint-override"><code>library(RODBC)
  library(dplyr)
  #library(data.table)
  library(tidyr)
  library(tictoc)
  library(tidyverse)
  library(lubridate)
  library(xlsx)
  library(sqldf)
  
  #set working directory
  setwd("C:/Ibos/R/WF_Metrics")
  
  
  my_server="servername"
  my_db="dbname"
  my_username="username"
  my_pwd="password"
  
  
  db &lt;- odbcDriverConnect(paste0("DRIVER={SQL Server};
                                 server=",my_server,";
                                 database=",my_db,";
                                 uid=",my_username,";
                                 pwd=",my_pwd))
  
  
  sql="select * from dbo.metricsfinal"
  
  df &lt;- sqlQuery(db,sql)
  
  
  myfile="results"
  write.csv(df, file = paste0(myfile,"_test",".csv") ,row.names=FALSE)
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>After Oliver's answer and some helpful comments by other, I found out that the problem is the <code>xlsx</code> package. Now I need to figure out how to resolve this issue. I prefer to use this package rather than look for other packages/options, any help is appreciated. Here is the error:</p>
  
  <blockquote>
    <p>Error: package or namespace load failed for 'xlsx':  .onLoad failed in
    loadNamespace() for 'rJava', details:   call:
    dirname(this$RuntimeLib)   error: a character vector argument
    expected Execution halted</p>
  </blockquote>