From 658862d8a5c7563f8e757d19f5b99c42646131e1 Mon Sep 17 00:00:00 2001 From: Will Beasley Date: Mon, 20 Mar 2017 16:18:03 -0500 Subject: [PATCH 1/3] starting dygraph example Copied from http://rstudio.github.io/dygraphs/shiny.html --- 119-dygraph/DESCRIPTION | 7 +++++++ 119-dygraph/README.md | 3 +++ 119-dygraph/server.R | 36 ++++++++++++++++++++++++++++++++++++ 119-dygraph/ui.R | 28 ++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 119-dygraph/DESCRIPTION create mode 100644 119-dygraph/README.md create mode 100644 119-dygraph/server.R create mode 100644 119-dygraph/ui.R 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..1c1ac00c --- /dev/null +++ b/119-dygraph/README.md @@ -0,0 +1,3 @@ +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) so you should be sure to update Shiny before trying out dygraphs with it. diff --git a/119-dygraph/server.R b/119-dygraph/server.R new file mode 100644 index 00000000..a70f72d4 --- /dev/null +++ b/119-dygraph/server.R @@ -0,0 +1,36 @@ +# Copied 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") %>% + dyOptions(drawGrid = input$showgrid) + }) + + 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..d9307809 --- /dev/null +++ b/119-dygraph/ui.R @@ -0,0 +1,28 @@ +# Copied from http://rstudio.github.io/dygraphs/shiny.html +library(dygraphs) + +shinyUI(fluidPage( + + titlePanel("Predicted Deaths from Lung Disease (UK)"), + + 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") + ) + ) +)) From 5607510c5b7f29edb938e21d1f95421ce4366fcd Mon Sep 17 00:00:00 2001 From: Will Beasley Date: Mon, 20 Mar 2017 16:25:19 -0500 Subject: [PATCH 2/3] Include more dygraph features --- 119-dygraph/README.md | 4 +++- 119-dygraph/server.R | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/119-dygraph/README.md b/119-dygraph/README.md index 1c1ac00c..71cf104c 100644 --- a/119-dygraph/README.md +++ b/119-dygraph/README.md @@ -1,3 +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) so you should be sure to update Shiny before trying out dygraphs with it. +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 index a70f72d4..229a4a37 100644 --- a/119-dygraph/server.R +++ b/119-dygraph/server.R @@ -1,4 +1,4 @@ -# Copied from http://rstudio.github.io/dygraphs/shiny.html +# Slightly adapted from http://rstudio.github.io/dygraphs/shiny.html library(dygraphs) library(datasets) @@ -14,7 +14,11 @@ shinyServer(function(input, output) { output$dygraph <- renderDygraph({ dygraph(predicted(), main = "Predicted Deaths/Month") %>% dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>% - dyOptions(drawGrid = input$showgrid) + 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({ From 0ffe4c87e7d3caf4565ee37f9ac163bda33061a8 Mon Sep 17 00:00:00 2001 From: Will Beasley Date: Mon, 20 Mar 2017 16:37:26 -0500 Subject: [PATCH 3/3] dygraph CSS classes --- 119-dygraph/ui.R | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/119-dygraph/ui.R b/119-dygraph/ui.R index d9307809..4b637482 100644 --- a/119-dygraph/ui.R +++ b/119-dygraph/ui.R @@ -1,9 +1,18 @@ -# Copied from http://rstudio.github.io/dygraphs/shiny.html +# 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(