Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Forked from wch/server.r
Created November 8, 2012 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/4041692 to your computer and use it in GitHub Desktop.
Save timelyportfolio/4041692 to your computer and use it in GitHub Desktop.
Shiny example with stocks
if (!require(quantmod)) {
stop("This app requires the quantmod package. To install it, run 'install.packages(\"quantmod\")'.\n")
}
# Download data for a stock, if needed
require_symbol <- function(symbol) {
if (!exists(symbol))
getSymbols(symbol, from = "1900-01-01")
}
shinyServer(function(input, output) {
# Make a chart for a symbol, with the settings from the inputs
make_chart <- function(symbol) {
require_symbol(symbol)
chartSeries(get(symbol),
name = symbol,
type = input$chart_type,
subset = paste("last", input$time_num, input$time_unit),
log.scale = input$log_y,
theme = "white")
}
output$plot_aapl <- reactivePlot(function() { make_chart("AAPL") })
output$plot_msft <- reactivePlot(function() { make_chart("MSFT") })
output$plot_ibm <- reactivePlot(function() { make_chart("IBM") })
output$plot_goog <- reactivePlot(function() { make_chart("GOOG") })
output$plot_yhoo <- reactivePlot(function() { make_chart("YHOO") })
})
shinyUI(pageWithSidebar(
headerPanel("Stocks"),
sidebarPanel(
wellPanel(
p(strong("Stocks")),
checkboxInput(inputId = "stock_aapl", label = "Apple (AAPL)", value = TRUE),
checkboxInput(inputId = "stock_msft", label = "Microsoft (MSFT)", value = FALSE),
checkboxInput(inputId = "stock_ibm", label = "IBM (IBM)", value = FALSE),
checkboxInput(inputId = "stock_goog", label = "Google (GOOG)", value = TRUE),
checkboxInput(inputId = "stock_yhoo", label = "Yahoo (YHOO)", value = FALSE)
),
selectInput(inputId = "chart_type",
label = "Chart type",
choices = c("Candlestick" = "candlesticks",
"Matchstick" = "matchsticks",
"Bar" = "bars",
"Line" = "line")
),
wellPanel(
p(strong("Date range (back from present)")),
sliderInput(inputId = "time_num",
label = "Time number",
min = 1, max = 24, step = 1, value = 6),
selectInput(inputId = "time_unit",
label = "Time unit",
choices = c("Days" = "days",
"Weeks" = "weeks",
"Months" = "months",
"Years" = "years"),
selected = "Months")
),
checkboxInput(inputId = "log_y", label = "log y axis", value = FALSE)
),
mainPanel(
conditionalPanel(condition = "input.stock_aapl",
plotOutput(outputId = "plot_aapl")),
conditionalPanel(condition = "input.stock_msft",
plotOutput(outputId = "plot_msft")),
conditionalPanel(condition = "input.stock_ibm",
plotOutput(outputId = "plot_ibm")),
conditionalPanel(condition = "input.stock_goog",
plotOutput(outputId = "plot_goog")),
conditionalPanel(condition = "input.stock_yhoo",
plotOutput(outputId = "plot_yhoo"))
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment