Executar macro com Rexcel
Existem alguns especialistas em Rexcel aqui? :)
Estou executando uma macro com o Rexcel no VBA:
Sub create_efficient_frontier()
RInterface.StartRServer
Sheets("Analys").Range("A52:K82").ClearContents
RInterface.PutDataframe "datat", Range("ChosenData!X181:AD352")
RInterface.PutArray "startdate", Range("Analys!K2")
RInterface.PutArray "enddate", Range("Analys!K3")
RInterface.RunRFile "C:/Users/Documents/EffFront.R"
RInterface.GetDataframe "hmz$pweight", Range("Analys!A51:E76")
End Sub
O código R de origem é:
myfront=function(datat,startdate,enddate){
library(fPortfolio)
datat=datat[startdate:enddate,]
datanew=datat[sapply(datat[1,], function(x) !any(is.na(x)))]
datatss=as.timeSeries(datanew,datanew[,1])
Spec = portfolioSpec()
Constraints = "Long-Only"
globminhfrxmed=minvariancePortfolio(
data = datatss,
spec = Spec,
constraints = Constraints)
setNFrontierPoints(Spec) <- 25
globminfrontier <- portfolioFrontier(datatss,Spec, Constraints)
thelisttoret<-vector(mode="list")
pweights<-globminfrontier@portfolio@portfolio$weights
pweights<-data.frame(pweights)
names(pweights)=colnames(globminfrontier@portfolio@portfolio$covRiskBudgets)
thelisttoret[["pweights"]]<-pweights
tret<-globminfrontier@portfolio@portfolio$targetReturn[,1,drop=FALSE]
thelisttoret[["tret"]]<-tret
trisk<-globminfrontier@portfolio@portfolio$targetRisk[,2,drop=FALSE]
thelisttoret[["trisk"]]<-trisk
return(thelisttoret)
}
hmz=myfront(datat,startdate,enddate)
Isso parece funcionar, mas a primeira linha (onde os nomes devem estar) é#RError
. Mas o código funciona corretamente em R.
É estranho, porque quando eu leio online dizRInterface.GetDataframe(varname,range) Puts the value of R variable var (which needs to be a dataframe) into Excel range range, putting variable names in the first row of the range
.
Alguma idéia do que estou fazendo de errado?
Cumprimentos!