|
| 1 | +# |
| 2 | +# This is a Shiny web application. You can run the application by clicking |
| 3 | +# the 'Run App' button above. |
| 4 | +# |
| 5 | +# Find out more about building applications with Shiny here: |
| 6 | +# |
| 7 | +# http://shiny.rstudio.com/ |
| 8 | +# |
| 9 | + |
| 10 | +# Load necessary packages |
| 11 | +library(shiny) |
| 12 | +library(shinydashboard) |
| 13 | +library(shinythemes) |
| 14 | +library(tidyverse) |
| 15 | +library(lubridate) |
| 16 | +library(tsibble) |
| 17 | +library(sugrrants) |
| 18 | +library(glue) |
| 19 | +library(bomrang) |
| 20 | +library(viridis) |
| 21 | +library(plotly) |
| 22 | +library(ggmap) |
| 23 | +library(ggthemes) |
| 24 | + |
| 25 | +# Read map data |
| 26 | +load("data/brissy_map.rda") |
| 27 | + |
| 28 | +# Read data, and create species list for menu, and create a date |
| 29 | +wildlife <- read_csv("data/brissy_ala.csv") |
| 30 | +species <- wildlife %>% |
| 31 | + select(`Vernacular name`) %>% |
| 32 | + distinct() %>% |
| 33 | + arrange(`Vernacular name`) |
| 34 | +wildlife <- wildlife %>% |
| 35 | + mutate(date = ymd(paste0(Year,Month,Day))) |
| 36 | + |
| 37 | +# Define UI for application |
| 38 | +ui <- fluidPage(theme = shinytheme("flatly"), |
| 39 | + titlePanel("Exploring wildlife spotted around Brisbane"), |
| 40 | + tabsetPanel( |
| 41 | + # Set up the user interface for the calendar tab |
| 42 | + # to select variables to plot, labels and colouring |
| 43 | + tabPanel("Choose a species", |
| 44 | + # Sidebar choosing variables, labels and colour |
| 45 | + sidebarLayout( |
| 46 | + sidebarPanel( |
| 47 | + selectInput("species", "pick one", choices = species, selected = "Australian Brush-turkey") |
| 48 | + ), |
| 49 | + |
| 50 | + # Show the scatterplot, with a fixed height |
| 51 | + mainPanel( |
| 52 | + plotlyOutput("map", height="400px") |
| 53 | + ) |
| 54 | + ) |
| 55 | + ), |
| 56 | + tabPanel("Choose a time period", |
| 57 | + # Sidebar choosing variables, labels and colour |
| 58 | + sidebarLayout( |
| 59 | + sidebarPanel( |
| 60 | + selectInput("species2", "pick one", choices = species, selected = "Australian Brush-turkey"), |
| 61 | + dateInput("from", "From:", value = ymd(min(wildlife$date))), |
| 62 | + dateInput("to", "To:", value = ymd(max(wildlife$date))) |
| 63 | + ), |
| 64 | + |
| 65 | + # Show the scatterplot, with a fixed height |
| 66 | + mainPanel( |
| 67 | + plotlyOutput("time", height="400px") |
| 68 | + ) |
| 69 | + ) |
| 70 | + ) |
| 71 | + ) |
| 72 | +) |
| 73 | + |
| 74 | +server <- function(input, output) { |
| 75 | + |
| 76 | + # Make the interactive scatterplot of occurrence on map |
| 77 | + output$map <- renderPlotly({ |
| 78 | + # Filter to species of interest and plot locations |
| 79 | + wildlife_sub <- wildlife %>% filter(`Vernacular name` == input$species) |
| 80 | + p <- ggmap(brissy_map) + |
| 81 | + geom_point(data=wildlife_sub, aes(x=Longitude, y=Latitude, label=date), |
| 82 | + alpha=0.5, colour="orange") + |
| 83 | + ggtitle(paste(input$species, ": ", nrow(wildlife_sub))) + |
| 84 | + theme_map() |
| 85 | + ggplotly(p, tooltip = "label") |
| 86 | + }) |
| 87 | + |
| 88 | + # Make the interactive time series plot |
| 89 | + output$time <- renderPlotly({ |
| 90 | + # Filter to species of interest, count number per day and plot over time |
| 91 | + wildlife_sub <- wildlife %>% filter(`Vernacular name` == input$species2) |
| 92 | + wildlife_count <- wildlife_sub %>% count(date) %>% |
| 93 | + filter(date >= input$from, date <= input$to) |
| 94 | + p2 <- ggplot(data=wildlife_count, aes(x=date, y=n, label=date)) + |
| 95 | + geom_point(colour="black", alpha=0.5) + |
| 96 | + geom_smooth(colour="orange", se=FALSE) + |
| 97 | + xlab("") + ylab("Number") + |
| 98 | + ggtitle(paste(input$species2, ": ", nrow(wildlife_sub))) |
| 99 | + ggplotly(p2, tooltip = c("label", "y")) |
| 100 | + }) |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +# Run the application |
| 105 | +shinyApp(ui = ui, server = server) |
| 106 | + |
0 commit comments