Skip to content

Instantly share code, notes, and snippets.

@loiyumba
Created April 8, 2016 17:54
Show Gist options
  • Save loiyumba/4869df51139643aaaf40d0c5a881dbbc to your computer and use it in GitHub Desktop.
Save loiyumba/4869df51139643aaaf40d0c5a881dbbc to your computer and use it in GitHub Desktop.
dygraphs for indian cities
library(shiny)
library(xts)
library(dygraphs)
library(data.table)
india <- fread("india.csv")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Average Temperature of Indian Cities"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
textInput("text", "Enter a City", value = ""),
submitButton("Submit"),
br(),
p("Note:"),
p("1. Start the city name with capital letter. It is case sensitive. "),
p("2. Some cities are named as their old name, e.g. Bombay for Mumbai."),
p("3. Some cities are not in the data. If the city you've entered doesn't
return any result, please try some other city."),
p("4. Some months or years might be missing for some cities."),
p("5. Hover your mouse over the graph to see month, year and temperature"),
p("6. Drag and select the region in the plot to zoom-in the desired time period. Double click on the plot to zoom-out."),
hr(),
p("Data is provided by", a("kaggle", href = "https://www.kaggle.com/berkeleyearth/climate-change-earth-surface-temperature-data", target = "_blank"))
),
# Show a plot of the generated distribution
mainPanel(
dygraphOutput("dygraph")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
enter_city <- reactive({
validate(
need(input$text != "", "Plot will be here when you enter a city")
)
select_city <- subset(india, City == input$text)
validate(
need(input$text %in% select_city$City, "City not in the data. Please enter another city.")
)
select_city[, .(dt, AverageTemperature)]
xts(select_city$AverageTemperature, as.Date(select_city$dt, format = "%Y-%m-%d"))
})
output$dygraph <- renderDygraph({
dygraph(enter_city(),
main = paste("Average temperature of", input$text)) %>%
dyAxis("y", label = "Temp (C)")
})
}
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment