Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dygraph #60

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions 119-dygraph/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions 119-dygraph/README.md
Original file line number Diff line number Diff line change
@@ -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/).
40 changes: 40 additions & 0 deletions 119-dygraph/server.R
Original file line number Diff line number Diff line change
@@ -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))
})
})
37 changes: 37 additions & 0 deletions 119-dygraph/ui.R
Original file line number Diff line number Diff line change
@@ -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")
)
)
))