apps/stockVis/helpers.R

# install.packages("quantmod")

if (!exists(".inflation")) {
  .inflation <- quantmod::getSymbols('CPIAUCNS', src = 'FRED',
     auto.assign = FALSE)
}

# adjusts yahoo finance data with the monthly consumer price index
# values provided by the Federal Reserve of St. Louis
# historical prices are returned in present values
adjust <- function(data) {

      latestcpi <- last(.inflation)[[1]]
      inf.latest <- time(last(.inflation))
      months <- split(data)

      adjust_month <- function(month) {
        date <- substr(min(time(month[1]), inf.latest), 1, 7)
        coredata(month) * latestcpi / .inflation[date][[1]]
      }

      adjs <- lapply(months, adjust_month)
      adj <- do.call("rbind", adjs)
      axts <- xts(adj, order.by = time(data))
      axts[ , 5] <- Vo(data)
      axts
}
rappster/shinyref documentation built on May 27, 2019, 2:01 a.m.