5

I would like to to convert output from the getSymbols in quantmod package to a data frame. Presently I achieve that with the following code.

Data <- new.env()
getSymbols(Symbols = "EUR/USD", src = "oanda", from = "2005-01-01", 
    to = "2006-01-01", env = Data)
test <- as.data.frame(Data$EURUSD)
head(test)

Ideally, I would like to shorten this code to something on the lines:

test <- as.data.frame(getSymbols(Symbols = "EUR/USD", 
   src = "oanda", from = "2005-01-01", to = "2006-01-01"))

But this doesn't work as it should:

> head(test)
  getSymbols(Symbols = "EUR/USD", src = "oanda", from = "2005-01-01", to = "2006-01-01")
1                                                                                 EURUSD

Ideally, I would like to avoid referring to the pair EUR/USD when working with the data as in future I will be working to make this component dynamic so having to type test <- as.data.frame(Data$EURUSD) spoils the fun. My ideal code, would work like that:

test <- as.data.frame(getSymbols(Symbols = *user input*, 
   src = "oanda", from = "2005-01-01", to = "2006-01-01"))

At the moment I'm not necessairly interested in the user input but in coercing quantmod output to data frame without the need to call the quantmod object name.

Konrad
  • 17,740
  • 16
  • 106
  • 167

2 Answers2

9

You can try something like this:

userInput="EUR/USD"
test<-as.data.frame(getSymbols(Symbols = userInput, 
    src = "oanda", from = "2005-01-01",to = "2006-01-01", env = NULL))

Setting the env to NULL results in no creating the data in the environment and returning it.

NicE
  • 21,165
  • 3
  • 51
  • 68
  • Thanks, it worked really nicely. In terms future development, considering the [Shiny's Scoping Rules](http://shiny.rstudio.com/articles/scoping.html) is there a chance that `env = NULL` may prove problematic when packed into a ShinyApp? – Konrad Jan 30 '15 at 14:39
  • @Konrad You could probably double check with a minimal Shiny application if it's a big concern, but I don't think this should cause any problems. The help file (`?getSymbols`) states that `As of [version] 0.4-0, this [setting auto.assign=FALSE] is the same as setting env=NULL`, so if you *really* wanted to be sure you could probably use `env=NULL` and `auto.assign=FALSE`. – nrussell Jan 30 '15 at 14:51
  • 5
    You could also use the `return.class` argument. `getSymbols(Symbols=userInput, src="oanda", from="2005-01-01", to="2006-01-01", env = NULL, return.class="data.frame")` – GSee Jan 30 '15 at 14:51
  • Thanks, I'll experiment with this and if something doesn't work add more comments. – Konrad Jan 30 '15 at 14:53
  • Remark: with the ``return-class`` argument of ``getSymbols()``, dates are still stored as a row index rather than as a variable. – PatrickT May 17 '16 at 20:08
1

The tidyquant package is ideal for this scenario. You can try this:

tq_get("EUR/USD", get = "exchange.rates")

If you want more currency pairs in the same data frame, try this:

c("EUR/USD", "EUR/JPY", "EUR/GBP") %>% 
     tq_get(get = "exchange.rates")

You can then use all of the functionality of the "tidyverse" such as grouping with group_by, mutating with mutate, etc. In addition, the tidyquant package has other functions for working specifically with time series data.

Matt Dancho
  • 6,840
  • 3
  • 35
  • 26