How to get currency exchange rates in R

后端 未结 3 1015
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 15:22

Are there are any R packages/functions to get exchange rates in real time, e.g. from Google Finance? Would prefer to avoid RCurl or other parsers if something\'s already out

3条回答
  •  醉话见心
    2020-12-24 15:50

    Looks like TFX and quantmod have functions for this (thanks to @RStudent and @KFB for the tips). I preferred quantmod since it didn't require setting up an account, but AFAICT there's no vectorized current-snapshot function like what I'm seeking. This function GetExchangeRates does this:

    GetExchangeRates <- function(from, to, dt=Sys.Date()) {
      require(quantmod)
      obj.names <- getFX(paste0(from, "/", to), from=dt, to=dt)
      result <- numeric(length(obj.names))
      names(result) <- obj.names
      for (obj.name in obj.names) {
        result[obj.name] <- as.numeric(get(obj.name))[1]
        # Clean up    
        rm(obj.name)
      }
      return(result)
    }
    
    TestExchangeRates <- function() {
      from <- c("CAD", "JPY", "USD")
      to <- c("USD", "USD", "EUR")
      GetExchangeRates(from, to)
    }
    

提交回复
热议问题