public
Last active

Shiny example with stocks

  • Download Gist
server.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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, and return the data
require_symbol <- function(symbol, envir = parent.frame()) {
if (is.null(envir[[symbol]])) {
envir[[symbol]] <- getSymbols(symbol, auto.assign = FALSE)
}
 
envir[[symbol]]
}
 
 
shinyServer(function(input, output) {
 
# Create an environment for storing data
symbol_env <- new.env()
 
# Make a chart for a symbol, with the settings from the inputs
make_chart <- function(symbol) {
symbol_data <- require_symbol(symbol, symbol_env)
 
chartSeries(symbol_data,
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 <- renderPlot({ make_chart("AAPL") })
output$plot_msft <- renderPlot({ make_chart("MSFT") })
output$plot_ibm <- renderPlot({ make_chart("IBM") })
output$plot_goog <- renderPlot({ make_chart("GOOG") })
output$plot_yhoo <- renderPlot({ make_chart("YHOO") })
})
ui.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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",
br(),
div(plotOutput(outputId = "plot_aapl"))),
 
conditionalPanel(condition = "input.stock_msft",
br(),
div(plotOutput(outputId = "plot_msft"))),
 
conditionalPanel(condition = "input.stock_ibm",
br(),
div(plotOutput(outputId = "plot_ibm"))),
 
conditionalPanel(condition = "input.stock_goog",
br(),
div(plotOutput(outputId = "plot_goog"))),
 
conditionalPanel(condition = "input.stock_yhoo",
br(),
plotOutput(outputId = "plot_yhoo"))
)
))

Change the plotting lines in server.r as follows to avoid warnings with newer versions of shiny:

output$plot_aapl <- renderPlot({ make_chart("AAPL") })
output$plot_msft <- renderPlot({ make_chart("MSFT") })
output$plot_ibm <- renderPlot({ make_chart("IBM") })
output$plot_goog <- renderPlot({ make_chart("GOOG") })
output$plot_yhoo <- renderPlot({ make_chart("YHOO") })

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.