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.

7hirumal
  • 21
  • 2
  • https://stackoverflow.com/questions/3506007/running-r-code-from-command-line-windows you need to call Rscript.exe with parameters – crestor Aug 18 '21 at 07: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? – 7hirumal Aug 18 '21 at 08:01
  • Do you know if you have many R interpreters installed? Which parts of the source are not executed? – crestor Aug 18 '21 at 08: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 – 7hirumal Aug 18 '21 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 https://community.rstudio.com/t/access-denied-for-all-r-and-rstudio-activities-but-not-other-apps-in-previously-used-folders/11661 – crestor Aug 18 '21 at 17:27
  • The common way of executing an R script (on Windows) is calling Rscript, not trying to open a full IDE (like RStudio). – crestor Aug 18 '21 at 17:34
  • I have tried calling Rscript. But the script is not getting executed. Thats why I'm trying to open RStudio and execute it. Script works when I run it with RStudio. But not with rscript.exe. – 7hirumal Aug 19 '21 at 04:56

0 Answers0