使用Shell在VBA中运行R脚本

时间:2018-03-30 10:54:32

标签: r vba

VBA代码正在运行,但它没有从R输出(Write.CSV文件)保存文件

Sub R_Click()
   runs an external R code through Shell
   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 = """" & "C:\Users\a.af.jain\Documents\R\R-3.4.1\bin\x64\RScript" & """ """ & "C:\Users\a.af.jain\Desktop\Missing_data.R" & """"
   errorCode = shell.Run(path, style, waitTillComplete)
End Sub

R代码

setwd("C:/Users/a.af.jain/Desktop/R")
input_missing = read.csv("input_missing.csv") 
Input_LGD = read.csv("Insert_LGD.csv") 
Result_data_missing = merge(input_missing,Input_LGD,all.x = TRUE) 
write.csv(Result_data_missing,"Result_data_missing.csv")

VBA能够从R读取input_missing文件,但我不知道它是否正在进行合并,而且VBA代码无法保存来自R的文件(write.csv文件)

1 个答案:

答案 0 :(得分:0)

您的报价已关闭,您需要用双引号将Rscript的地址换行,并在Rscript地址和应执行的R文件的地址之间保留一个空格字符:

path = """C:\Users\a.af.jain\Documents\R\R-3.4.1\bin\x64\RScript"" C:\Users\a.af.jain\Desktop\Missing_data.R"

path = """C:\Users\a.af.jain\Documents\R\R-3.4.1\bin\x64\RScript""" & " " &  "C:\Users\a.af.jain\Desktop\Missing_data.R"