2

I want to run R code in RStudio by just clicking a button in excel. Currently, I could only open the R code in RStudio. How do I run this R code which is open in RStudio from VBA?

Sub RunRScript()

Dim WshShell, strCurDir
Set WshShell = VBA.CreateObject("WScript.Shell")
Dim waitTillComplete As Boolean: waitTillComplete = True
Dim style As Integer: style = 1
Dim errorCode As Integer
Dim path As String
DirFile = C:\Program Files\RStudio\bin\rstudio.exe
RCodePath = D:\rCode.R

path = Chr(34) & DirFile & Chr(34) & " " & Chr(34) & RCodePath & Chr(34)
errorCode = WshShell.Run(path, style, waitTillComplete)

End Sub

I tried running the rcode using rscript.exe. But some of the r modules are not working when I use rscript.exe. The rcode works fine when I run on RStudio IDE. So I'm trying to open rstudio and run the rcode.

7
  • stackoverflow.com/questions/3506007/… you need to call Rscript.exe with parameters Commented Aug 18, 2021 at 7:53
  • When I tried to use Rscript.exe, some modules in R are not getting executed. But if I run from RStudio, it works. So I'm trying to open RStudio and execute the rcode. Do you have any thoughts on this? Commented Aug 18, 2021 at 8:01
  • Do you know if you have many R interpreters installed? Which parts of the source are not executed? Commented Aug 18, 2021 at 8:06
  • I don't think so. read_xlsx read_excel, read.csv - none of this works. I tried running with admin previleges as well. Still the read module is not working Commented Aug 18, 2021 at 10:01
  • If you can execute correctly Rscript, that would be a different question here on stackoverflow. You could also look some references like community.rstudio.com/t/… Commented Aug 18, 2021 at 17:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.