Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Shiny example with stocks

View server.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") })
})
View server.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") })

Error in source(file, ..., keep.source = TRUE, encoding = checkEncoding(file)) :
C:\Users\karansheraz\Documents\testapp/ui.R:63:9: unexpected symbol

unable to run app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.