Varun Kumar
05/15/2015
This small Shiny application demonstrates Shiny's automatic UI updates. Make selections for the stock and notice how the renderPlot expression is automatically re-evaluated when differnt stocks, chart types and date ranges are selected.
there are three slections that can be made
In addition to the slections followong code sinppets are also mand avaliable for review.
Following code snippets are avaliable.
To run the application please select a stock and make changes to the chart type.
library(shiny)
library(quantmod)
#download required symbol data
require_symbol <- function(symbol, envir = parent.frame()) {
if (is.null(envir[[symbol]])) {
envir[[symbol]] <- getSymbols(symbol, auto.assign = FALSE)
}
envir[[symbol]]
}
# Define server logic required to plot recent trends in the Apple and MSFT stocks
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(input$daterange, collapse = "::"),
theme = "black")
}
output$plot_aapl <- renderPlot({ make_chart("AAPL") })
output$plot_msft <- renderPlot({ make_chart("MSFT") })
})
library(shiny)
library(quantmod)
# Define UI for application that shows the recent two years stock prices of apple
# and microsoft from the current date based on user selection.
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)
),
selectInput(inputId = "chart_type",
label = "Chart type",
choices = c("Candlestick" = "candlesticks",
"Matchstick" = "matchsticks",
"Bar" = "bars",
"Line" = "line")
),
dateRangeInput(inputId = "daterange", label = "Date range",
start = Sys.Date() - 365*2, end = Sys.Date())
),
mainPanel(
conditionalPanel(condition = "input.stock_aapl",
br(),
div(plotOutput(outputId = "plot_aapl"))),
conditionalPanel(condition = "input.stock_msft",
br(),
div(plotOutput(outputId = "plot_msft")))
)
))
<!–html_preserve–>