diff --git a/119-dygraph/DESCRIPTION b/119-dygraph/DESCRIPTION new file mode 100644 index 00000000..93f69221 --- /dev/null +++ b/119-dygraph/DESCRIPTION @@ -0,0 +1,7 @@ +Type: Shiny +Title: dygraphs Lung Disease +Author: JJ Allaire +AuthorUrl: http://www.rstudio.com/ +License: MIT +Tags: dygraphs, time-series +DisplayMode: Showcase diff --git a/119-dygraph/README.md b/119-dygraph/README.md new file mode 100644 index 00000000..71cf104c --- /dev/null +++ b/119-dygraph/README.md @@ -0,0 +1,5 @@ +The dygraphs package provides the dygraphOutput and renderDygraph functions to enable use of [dygraphs](http://dygraphs.com) within Shiny applications and R Markdown interactive documents. + +Note that using dygraphs with Shiny requires a recent version of the Shiny package (>= 0.10.2.1). + +For more information, please see [rstudio.github.io/dygraphs/](http://rstudio.github.io/dygraphs/). diff --git a/119-dygraph/server.R b/119-dygraph/server.R new file mode 100644 index 00000000..229a4a37 --- /dev/null +++ b/119-dygraph/server.R @@ -0,0 +1,40 @@ +# Slightly adapted from http://rstudio.github.io/dygraphs/shiny.html +library(dygraphs) +library(datasets) + +shinyServer(function(input, output) { + + predicted <- reactive({ + hw <- HoltWinters(ldeaths) + predict(hw, n.ahead = input$months, + prediction.interval = TRUE, + level = as.numeric(input$interval)) + }) + + output$dygraph <- renderDygraph({ + dygraph(predicted(), main = "Predicted Deaths/Month") %>% + dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>% + dyAxis("y", label = "Monthly Deaths") %>% + dyAnnotation("1983-08-1", text = "A", tooltip = "Local Min") %>% + dyEvent("1982-06-01", "Summer 1982", labelLoc = "bottom", color="Coral") %>% + dyRangeSelector(dateWindow = c("1981-01-01", "1984-01-01")) %>% + dyOptions(drawGrid = input$showgrid, includeZero = TRUE) + }) + + output$from <- renderText({ + strftime(req(input$dygraph_date_window[[1]]), "%d %b %Y") + }) + + output$to <- renderText({ + strftime(req(input$dygraph_date_window[[2]]), "%d %b %Y") + }) + + output$clicked <- renderText({ + strftime(req(input$dygraph_click$x), "%d %b %Y") + }) + + output$point <- renderText({ + paste0('X = ', strftime(req(input$dygraph_click$x_closest_point), "%d %b %Y"), + '; Y = ', req(input$dygraph_click$y_closest_point)) + }) +}) diff --git a/119-dygraph/ui.R b/119-dygraph/ui.R new file mode 100644 index 00000000..4b637482 --- /dev/null +++ b/119-dygraph/ui.R @@ -0,0 +1,37 @@ +# Slightly adapted from from http://rstudio.github.io/dygraphs/shiny.html +library(dygraphs) + +tags_style <- " + .dygraph-label {color:#555555;} + .dygraph-axis-label {color:#777777} + .dygraph-title {color:#3C8DBC; font-size:120%} +" + +shinyUI(fluidPage( + + titlePanel("Predicted Deaths from Lung Disease (UK)"), + shiny::tags$head( + tags$style(HTML(tags_style)) + ), + + sidebarLayout( + sidebarPanel( + numericInput("months", label = "Months to Predict", + value = 72, min = 12, max = 144, step = 12), + selectInput("interval", label = "Prediction Interval", + choices = c("0.80", "0.90", "0.95", "0.99"), + selected = "0.95"), + checkboxInput("showgrid", label = "Show Grid", value = TRUE), + hr(), + div(strong("From: "), textOutput("from", inline = TRUE)), + div(strong("To: "), textOutput("to", inline = TRUE)), + div(strong("Date clicked: "), textOutput("clicked", inline = TRUE)), + div(strong("Nearest point clicked: "), textOutput("point", inline = TRUE)), + br(), + helpText("Click and drag to zoom in (double click to zoom back out).") + ), + mainPanel( + dygraphOutput("dygraph") + ) + ) +))