使用VBA调用R代码

我正在做一个项目,我需要使用VBA作为前端来调用R作为后端。 显然,没有错误警告,但我没有得到我的输出。

基于VBA,我要求R读取一个外部的csv文件,然后写一个表csv作为输出。 但是这并不能完成这项工作。

这里是我的代码VBA和R.

VBA ####

Sub RunRscript() 'runs an external R code through Shell 'The location of the RScript is 'C:\R_code' 'The script name is 'hello.R' 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 ""I:\des\Perso\Xiaofei\try.R""" errorCode = shell.Run(path, style, waitTillComplete) End Sub 

[R #####

 a <- read.table("German_ZCY_frombloomberg.csv",sep = ",",header = TRUE) a <- data.frame(a) write.table(a,"I:/des/Perso/Xiaofei/hello.csv", quote = FALSE, row.names =FALSE, col.names = TRUE, sep = ";") 

由于没有发生警告,我不知道问题在哪里。 但是我看不到创build的csv文件“hello.csv”。

另一方面,如果我使用另一个R代码来简单地用下面的代码将一个短语写入文本文件,那么它工作得很好(VBA可以让R做这个工作)

[R ##

  sink('I:/des/Perso/Xiaofei/hello.txt',append=F,type="output") cat('Hello World') var1<-5^3 var2<-7^3 cat('\nThe result of adding',var1,'to',var2,'is',var1+var2) sink(NULL) 

你能帮我解决这个问题吗? 非常感谢你,

最好的祝福,

晓飞,