I got problem while I am converting a Ruby array into a vector in R.

def self.risk_return_plot(stock_ticker = ["VZ", "CHU", "T", "VOD", "DTEGY"])
   RSERVE.eval <<EOF
   myenv <- new.env()
   getSymbols("#{@stock_ticker}", env = myenv)
   monthly.Return <- do.call(merge, c(eapply(myenv, monthlyReturn), all=FALSE))
   names(monthly.Return)[1:ncol(monthly.Return)] <- paste("#{@stock_ticker}", 1:ncol(monthly.Return), sep="")
   EOF
end

When I send the command to the R server, it shows an error saying that Eval error: getSymbols(["VZ", "CHU", "T", "VOD", "DTEGY"]), and the correct type would be getSymbols(c("VZ", "CHU", "T", "VOD", "DTEGY")) in R.

有帮助吗?

解决方案

If I understood your question, you want to create R a snippet of code, on the fly, and execute it on a R server.

You need to some fixes on your code:

def self.risk_return_plot(stock_ticker = ["VZ", "CHU", "T", "VOD", "DTEGY"])
   # creates a R vector version from stock_ticker array
   # enclose each item with " char, join with ',' and enclose all with c()
   r_vector = "c(#{stock_ticker.map() {|item| "\"#{item}\""}.join(',')})"
   # send R command string to the server
   RSERVE.eval <<-EOF
   myenv <- new.env()
   getSymbols(#{r_vector}, env = myenv)
   monthly.Return <- do.call(merge, c(eapply(myenv, monthlyReturn), all=FALSE))
   names(monthly.Return)[1:ncol(monthly.Return)] <- paste(#{r_vector}, 1:ncol(monthly.Return), sep="")
   EOF
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top