Run VBA script from R
Write a VBscript wrapper that calls your VBA. See Way to run Excel macros from command line or batch file?
Run your VBscript via R's
system
orshell
functions.
Launch VBA macro from R
I found the solution. If some of you are trying to call personnal library from a vbscript, you need to add :
Dim objWorkbook
Set objWorkbook=xlApp.Workbooks.Open("D:\users\...\PERSONAL.XLSB")
before launching the macro.
Running R scripts from VBA
Public Sub RunRTest()
Shell ("Rscript test.r")
End Sub
Run VBA code for multiple Excel files in R
R
code that executes this particular task is as follows:
shell(shQuote(normalizePath("C:/Documents/VBS.vbs")), "cscript", flag = "//nologo")
And VB script can be found in this topic.
Run R script from VBA
Finally, here is a solution working well:
Function Run_R_Script(sRApplicationPath As String, _
sRFilePath As String, _
Optional iStyle As Integer = 1, _
Optional bWaitTillComplete As Boolean = True) As Integer
Dim sPath As String
Dim shell As Object
'Define shell object
Set shell = VBA.CreateObject("WScript.Shell")
'Wrap the R path with double quotations
sPath = """" & sRApplicationPath & """"
sPath = sPath & " "
sPath = sPath & sRFilePath
Run_R_Script = shell.Run(sPath, iStyle, bWaitTillComplete)
End Function
Sub Demo()
Dim iEerrorCode As Integer
iEerrorCode = Run_R_Script("C:\Program Files\R\R-3.6.1\bin\x64\Rscript",
"C:\Users\myname\Desktop\Code.R")
End Sub
Run R script in Excel
It seems quite odd that it is opening in RStudio. I would suggest running it straight through R.exe. It looks like the PATH is setup all correctly from what you have told us. So you can call R.exe like this if don't need the output:
Sub RunRscript()
Shell ("R CMD BATCH C:\R_code\hello.R")
End Sub
If you need the output then you'll need to make a WshShell object like this:
Sub RunRscript()
Dim output As String
output = CreateObject("WScript.Shell").Exec("R CMD BATCH C:\R_code\hello.R").StdOut.ReadAll
End Sub
This is the older way to run R scripts but should work fine for the time being. You may want to look into your installation of R a bit more to see if there are any other problems.
How to pass an argument from Excel/VBA to a script in Rstudio
EDIT:
The problem is RStudio and not your code. RStudio doesn't accept command line arguments:
https://support.rstudio.com/hc/en-us/community/posts/200659066-Accessing-command-line-options-in-RStudio
OLD ANSWER:
The problem I am seeing is that you are putting the text "var1" in to the path instead of the contents of the variable called var1. I replaced your Chr(34) with stacked quotes because it's easier for me to keep track of. I apologize if this looks less readable to you. I tested the string and it does feed the contents of Var1 as a command line argument.
Try this:
Option Explicit
Public Sub RunRscript()
ActiveWorkbook.Save
Dim shell As Object
Set shell = VBA.CreateObject("WScript.Shell")
Dim waitTillComplete As Boolean: waitTillComplete = True
Dim style As Long: style = 1
Dim errorcode As Long
Dim path As String
Dim var1 As String
var1 = ActiveSheet.Range("F3").Value
path = """C:\Program Files\RStudio\bin\rstudio.exe"" ""C:\Users\LI\Downloads\starting_code_v3.R"" """ & var1 & """"
errorcode = shell.Run(path, style, waitTillComplete)
End Sub
Related Topics
Count the Number of All Words in a String
R: Gsub, Pattern = Vector and Replacement = Vector
Find Which Season a Particular Date Belongs To
Dplyr: "Error in N(): Function Should Not Be Called Directly"
Assign Multiple Objects to .Globalenv from Within a Function
How to Order Data by Value Within Ggplot Facets
How to Delete Rows from a Dataframe That Contain N*Na
Call Apply-Like Function on Each Row of Dataframe With Multiple Arguments from Each Row
Gradient of N Colors Ranging from Color 1 and Color 2
How to Install Packages in Latest Version of Rstudio and R Version.3.1.1
Convert Unix Epoch to Date Object
R - Concatenate Two Dataframes
How to Get Week Numbers from Dates
Find How Many Times Duplicated Rows Repeat in R Data Frame
Why Do R Objects Not Print in a Function or a "For" Loop