Running RScript from excel Permission denied Error Code 70 - r

I am trying to run Rscript from Excel VBA and I receive the following error. I tried different things but just cannot make it work. Could someone please help?
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

Related

Rscript opens but do not execute from VBA

I run a Rscript from VBA, and currently my VBA code opens a shell of rscript.exe and the window do execute the script. It will run some code, but others it will close and send VBA message it has completed. Some of the code it does not like to run such as:
Excelticker <- (read_excel("ticker.xlsx",sheet="Tickers", range ="A1:A400"))
Excelticker <- Excelticker$Ticker
Excelticker <- na.omit(Excelticker)
financials <- getQuote(FirmsEG, what = yahooQF(c("P/E Ratio","Market Capitalization","Shares Outstanding","Currency","Earnings/Share")))
This is my VBA code
Public Sub Update()
Dim iEerrorcode As Integer
Dim MyRscript As String
Dim MyUpdate As String
MyRscript = "C:\Users\elowe\xxxx\M&A\models\test.R"
iEerrorcode = Run_R_Script(MyRscript)
MsgBox "Run"
End Sub
Function Run_R_Script(MyRscript As String, Optional RhomeDir As String = "C:\Program Files\R\R-4.2.2\bin\x64\Rscript.exe", 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 ""
sPath = """" & RhomeDir & """ """ & MyRscript & """"
'Run r using shell
Run_R_Script = shell.Run(sPath, iStyle, bWaitTillComplete)
End Function

Run R script from VBA

I have tried several examples found on forums to run my R script from VBA, but it doesn't work. The R script works well alone. Here is the code I ran:
Sub RunRscript()
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 C:\Users\Documents\Code.R"
errorCode = shell.Run(path, style, waitTillComplete)
End Sub
I'm getting an error message saying that running object "IWshShell3" failed. Is there anything special to write on the R code prior to running this macro? Shall I load a package, or load the files in a specific folder?
Your script is not able to find the executable (RScript).
Provide the absolute path, then it should work well.
See here on where to find it: http://datacornering.com/how-to-run-r-scripts-from-the-windows-command-line-cmd/
Edit:
I saw right now, that you could stumble into further problems regarding missing environments.
See here: Setting .libPaths() For Running R Scripts From Command Line Using Rscript.exe
This is how I would do it.
Sub RunRscript1()
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 to R executable: C:\your_path\Documents\R\R-3.2.5\bin\x64\R.exe
' path to R script: C:\your_path\Documents\R\Download.r
' see more setup details here
' http://shashiasrblog.blogspot.com/2013/10/vba-front-end-for-r.html
path = "C:\your_path\Documents\R\R-3.2.5\bin\x64\R.exe CMD BATCH --vanilla --slave C:\your_path\Documents\R\Download.r"
'path = """C:\your_path\Documents\R\R-3.2.5\bin\i386"" C:\Users\rshuell001\Documents\R\Download.R"
errorCode = shell.Run(path, style, waitTillComplete)
End Sub
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 a .R file through VBA [duplicate]

How can I run a R script from VBA? Say I have a R script stored as C:\XXX\testR.R
I tried using Shell, but not quite successful.
Public Sub RunRTest()
Shell ("Rscript test.r")
End Sub
Note be careful with your file locations and may need more explicit Shell dim statements....e.g. replace with these lines in your VB
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 = """" & Cells.Range("RhomeDir") & """ """ & Cells.Range("MyRscript") & """"
errorCode = shell.Run(path, style, waitTillComplete)
where, in Excel a cell with a named reference RhomeDir contains text
C:\Program Files\R\R-3.2.3\bin\x64\rscript and
MyRscript contains text C:/Documents/Rworkings/Rscripttest.s
noting the unix R backslash and .s or .r postfix and VB replaces "" with " to give double brackets in path expression (plus further outside brackets to denote string). Also not a good idea to have spaces in your file name.
The full dim syntax of the shell command above was found by searching for VBA shell.
I put everything in a function that can be called easily. The output is the shell.run output, which is an integer:
Function to Run an R Script:
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
Examples how to call:
Sub Demo()
Dim iEerrorCode As Integer
iEerrorCode = Run_R_Script("C:\Program Files\R\R-3.4.4\bin\x64\rscript","C:\Ibos\R\WF_Metrics\Abe.R")
End Sub
OR
Sub Demo()
Dim iEerrorCode As Integer
Dim WS as WorkSheet
Set WS=ThisWorkBook.Worksheets("Sheet1")
iEerrorCode = Run_R_Script(WS.Range("A1"),WS.Range("A2")) 'cell A1=adderess of R application and cell A2 is the address of your R file, one can use a named range too
End Sub

Running a perl script, need to output results to VB.net

I am running multiple perl scripts (about 2-5) within a ASP.net page using VB.net. The scripts are executing just fine. The script is returning the DNS name and other information of the UNIX server to ensure we have it configured correctly.
I need to output the Perl results to VB.net so I can show the results in the main page (and color code depending on success/failure).
Any suggestions?
EDIT:
Showing my code
If rdoUnix.Checked Then
runUnixScript("testScript.pl", UNIXUSERNAME, UNIXPASSWORD)
End If
End If
End Sub
Public Sub runUnixScript(ByVal SCRIPT As String, ByVal UNIXUSERNAME As String, ByVal UNIXPASSWORD As String)
Dim COMPUTERNAME As String = FQDN.Text
Dim virtualFolder As String = "~/Scripts"
Dim physicalFolder As String = Server.MapPath(virtualFolder)
Dim processCmdFileTransfer As String = "/K C:\pscp.exe -pw " & UNIXPASSWORD & " " & physicalFolder & "\" & SCRIPT & " " & UNIXUSERNAME & "#" & COMPUTERNAME & ":" & SCRIPT
Dim processCmdFileActions As String = "-ssh -pw " & UNIXPASSWORD & " " & UNIXUSERNAME & "#" & COMPUTERNAME & "XX" & SCRIPT
' Transfers Script, Makes it executable, Runs Script and then deletes script
RunProcess("C:\Windows\System32\cmd.exe", processCmdFileTransfer, SCRIPT)
RunProcess("C:\plink.exe", processCmdFileActions, SCRIPT, " chmod u+x ./")
RunProcess("C:\plink.exe", processCmdFileActions, SCRIPT, " ./")
RunProcess("C:\plink.exe", processCmdFileActions, SCRIPT, " rm ./")
End Sub
Public Sub RunProcess(ByVal processPath As String, ByVal startInfo As String, ByVal script As String, Optional ByVal command As String = "")
Dim Proc As New System.Diagnostics.Process
Proc.StartInfo = New ProcessStartInfo(processPath)
If (InStr(startInfo, "XX") > 0) And (command <> "") Then
startInfo = startInfo.Replace("XX", command)
End If
Proc.StartInfo.Arguments = startInfo
Proc.StartInfo.RedirectStandardInput = True
Proc.StartInfo.RedirectStandardOutput = False
Proc.StartInfo.UseShellExecute = False
Proc.StartInfo.CreateNoWindow = True
Proc.Start()
Proc.WaitForExit()
End Sub
Try redirecting the output of the executed code using output redirection.
Just pass the perl executable the path of the perlscript in the following line
Dim psI As New ProcessStartInfo("PERL_INSTALLED\\perl.exe SCRIPT_DIR\\MyScript.pl");

Running R scripts from VBA

How can I run a R script from VBA? Say I have a R script stored as C:\XXX\testR.R
I tried using Shell, but not quite successful.
Public Sub RunRTest()
Shell ("Rscript test.r")
End Sub
Note be careful with your file locations and may need more explicit Shell dim statements....e.g. replace with these lines in your VB
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 = """" & Cells.Range("RhomeDir") & """ """ & Cells.Range("MyRscript") & """"
errorCode = shell.Run(path, style, waitTillComplete)
where, in Excel a cell with a named reference RhomeDir contains text
C:\Program Files\R\R-3.2.3\bin\x64\rscript and
MyRscript contains text C:/Documents/Rworkings/Rscripttest.s
noting the unix R backslash and .s or .r postfix and VB replaces "" with " to give double brackets in path expression (plus further outside brackets to denote string). Also not a good idea to have spaces in your file name.
The full dim syntax of the shell command above was found by searching for VBA shell.
I put everything in a function that can be called easily. The output is the shell.run output, which is an integer:
Function to Run an R Script:
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
Examples how to call:
Sub Demo()
Dim iEerrorCode As Integer
iEerrorCode = Run_R_Script("C:\Program Files\R\R-3.4.4\bin\x64\rscript","C:\Ibos\R\WF_Metrics\Abe.R")
End Sub
OR
Sub Demo()
Dim iEerrorCode As Integer
Dim WS as WorkSheet
Set WS=ThisWorkBook.Worksheets("Sheet1")
iEerrorCode = Run_R_Script(WS.Range("A1"),WS.Range("A2")) 'cell A1=adderess of R application and cell A2 is the address of your R file, one can use a named range too
End Sub

Resources