Skip to content

Instantly share code, notes, and snippets.

@wimsy
Created October 11, 2014 12:28
Show Gist options
  • Save wimsy/d425459a8aeb892f14c3 to your computer and use it in GitHub Desktop.
Save wimsy/d425459a8aeb892f14c3 to your computer and use it in GitHub Desktop.
just learning about shiny sharing options - nothing to see here
if (!exists(".inflation")) {
.inflation <- 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
}
# server.R
library(quantmod)
source("helpers.R")
shinyServer(function(input, output) {
dataInput <- reactive({
getSymbols(input$symb, src = "yahoo",
from = input$dates[1],
to = input$dates[2],
auto.assign = FALSE)
})
data <- reactive({
if (input$adjust) adjust(dataInput()) else dataInput()
})
output$plot <- renderPlot({
# data <- dataInput()
# if (input$adjust) data <- adjust(dataInput())
chartSeries(data(), theme = chartTheme("white"),
type = "line", log.scale = input$log, TA = NULL)
})
})
library(shiny)
shinyUI(fluidPage(
titlePanel("stockVis"),
sidebarLayout(
sidebarPanel(
helpText("Select a stock to examine.
Information will be collected from yahoo finance."),
textInput("symb", "Symbol", "SPY"),
dateRangeInput("dates",
"Date range",
start = "2013-01-01",
end = as.character(Sys.Date())),
actionButton("get", "Get Stock"),
br(),
br(),
checkboxInput("log", "Plot y axis on log scale",
value = FALSE),
checkboxInput("adjust",
"Adjust prices for inflation", value = FALSE)
),
mainPanel(plotOutput("plot"))
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment