Hamilton Christmas Bird Count: Part 3

Creating a Shiny app for the Hamilton Christmas Bird Count data.
birding
ggplot2
shiny
Author

Sharleen Weatherley

Published

March 24, 2019

Introduction

This post is to demonstrate a Shiny app I made so that people could look through the data themselves!

I was able to make this whole app very easily thanks to the wonderful tutorial found here!

Data visualizing using a Shiny app

Here is the link to my Shiny app: https://sharleenw.shinyapps.io/hamilton_cbc_shiny/

And here is the code I used to make the app:

library(shiny)
library(dplyr)
library(ggplot2)
library(readr)
library(scales)

min_max <- function(vector){
  min_max <- c(min(vector), max(vector))
  return(min_max)
}

hamilton_cbc <- read_rds("hamilton_cbc_output_part_2.rds")

species_list <- hamilton_cbc %>%
  distinct(species) %>%
  rename(Species = species) %>%
  arrange(Species)

years_list <- hamilton_cbc %>%
  distinct(year) %>%
  rename(Year = year) %>%
  arrange(-Year)

year_min_max <- min_max(years_list)


ui <- navbarPage("Hamilton Christmas Bird Count app",
                
                 tabPanel(
                   
                   # App title ----
                   titlePanel(tags$h4("Birds counted over multiple years")),
                   
                   # Sidebar layout with input and output definitions ----
                   sidebarLayout(
                     
                     # Sidebar panel for inputs ----
                     sidebarPanel(
                       
                       # Input: which species ----
                       selectizeInput("species_picked",
                                      multiple = TRUE,
                                      selected = c("American Crow", "Black-capped Chickadee", "American Goldfinch", "Canada Goose", "American Robin", "Northern Cardinal"),
                                      label = "Choose which species you would like to compare (up to six):",
                                      choices = species_list,
                                      options = list(maxItems = 6)),
                       
                       
                       # Input: Slider for the number of years ----
                       sliderInput("years_picked",
                                   label = "Number of years you would like to view:",
                                   sep = "",
                                   min = year_min_max[1],
                                   max = year_min_max[2],
                                   value = c(1955, year_min_max[2])),
                       
                       helpText(tags$ol(
                         tags$li("This data does not include birds counted only during count week"),
                         
                         tags$li("This data does not include hybrids or birds that were only identified to the \"sp.\" level"),
                         
                         tags$li("In 1955, the boundaries of the Hamilton Christmas Bird Count changed. I recommend only looking at data from 1955 onwards. However, the previous years' data have been included for completeness.")
                       )
                       )
                       
                     ),
                     
                     # Main panel for displaying outputs ----
                     mainPanel(
                       
                       # Output: Line graph ----
                       plotOutput(outputId = "time_series_plot", height = "800px")
                       
                     )
                   )
                   ),
                 
                 
                 tabPanel(
                   
                   titlePanel(tags$h4("Birds counted in a particular year")),
                   
                   # Sidebar layout with input and output definitions ----
                   sidebarLayout(
                     
                     # Sidebar panel for inputs ----
                     sidebarPanel(
                       
                       # Input: which year ----
                       selectInput("individual_year_picked",
                                   label = "What year would you like to look at the Hamilton Christmas Bird Count data for:",
                                   choices = years_list)
                       
                     ),
                     
                     # Main panel for displaying outputs ----
                     mainPanel(
                       
                       # Output: Data table ---- 
                       # can also do dataTableOutput
                       tableOutput(outputId = "count_table")
                       
                     )
                   )
                   
                   
                   
                 )
)

# Define server logic required to draw a plot and table ----
server <- function(input, output) {
  
  # First navbar output ----
  data_input <- reactive({
    
    hamilton_cbc %>% 
      filter(year >= req(input$years_picked[1]),
             year <= req(input$years_picked[2]),
             species %in% req(input$species_picked))
    
  })
  
  output$time_series_plot <- renderPlot({
    
    
    plotting_function <- function(input_for_plot){
      
      input_for_plot %>%
        ggplot(aes(x = year, y = how_many_counted, color = species)) +
        geom_line(size = 1) +
        xlab("Year") +
        ylab("Number counted") +
        theme_minimal() +
        theme(text = element_text(size = 18),
              legend.position = "none",
              plot.margin = margin(2, 20, 2, 2)) +
        facet_wrap(vars(species),
                   scales = "free",
                   dir = "v") +
        scale_y_continuous(labels = comma)
      
    }
    
    plotting_function(data_input())

  })
  
  
  # Second navbar output ----
  
  output$count_table <- renderTable({
    
    hamilton_cbc %>%
      filter(year == input$individual_year_picked) %>%
      count(species, how_many_counted) %>%
      select(-n) %>%
      arrange(-how_many_counted) %>%
      filter(how_many_counted > 0) %>%
      mutate(how_many_counted = how_many_counted %>%
               scales::number(big.mark = ",", accuracy = 1)) %>%
      rename(Species = species, `How many were counted` = how_many_counted)
    
  },
  
  align = "lr")
}

shinyApp(ui = ui, server = server)

And thank you to the Christmas Bird Count! The Christmas Bird Count Data was provided by National Audubon Society and through the generous efforts of Bird Studies Canada and countless volunteers across the western hemisphere.


Session info

─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.0 (2023-04-21 ucrt)
 os       Windows 11 x64 (build 22000)
 system   x86_64, mingw32
 ui       RTerm
 language (EN)
 collate  English_Canada.utf8
 ctype    English_Canada.utf8
 tz       Pacific/Honolulu
 date     2023-09-21
 pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.3.1)
 cachem        1.0.8      2023-05-01 [1] CRAN (R 4.3.0)
 callr         3.7.3      2022-11-02 [1] CRAN (R 4.3.0)
 cli           3.6.1      2023-03-23 [1] CRAN (R 4.3.0)
 colorspace    2.1-0      2023-01-23 [1] CRAN (R 4.3.0)
 crayon        1.5.2      2022-09-29 [1] CRAN (R 4.3.0)
 devtools    * 2.4.5      2022-10-11 [1] CRAN (R 4.3.1)
 digest        0.6.31     2022-12-11 [1] CRAN (R 4.3.0)
 dplyr       * 1.1.2      2023-04-20 [1] CRAN (R 4.3.0)
 ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.3.0)
 emo         * 0.0.0.9000 2023-07-22 [1] Github (hadley/emo@3f03b11)
 evaluate      0.20       2023-01-17 [1] CRAN (R 4.3.0)
 fansi         1.0.4      2023-01-22 [1] CRAN (R 4.3.0)
 fastmap       1.1.1      2023-02-24 [1] CRAN (R 4.3.0)
 fs            1.6.2      2023-04-25 [1] CRAN (R 4.3.0)
 generics      0.1.3      2022-07-05 [1] CRAN (R 4.3.0)
 ggplot2     * 3.4.3      2023-08-14 [1] CRAN (R 4.3.1)
 glue          1.6.2      2022-02-24 [1] CRAN (R 4.3.0)
 gtable        0.3.3      2023-03-21 [1] CRAN (R 4.3.0)
 here        * 1.0.1      2020-12-13 [1] CRAN (R 4.3.0)
 hms           1.1.3      2023-03-21 [1] CRAN (R 4.3.0)
 htmltools     0.5.5      2023-03-23 [1] CRAN (R 4.3.0)
 htmlwidgets   1.6.2      2023-03-17 [1] CRAN (R 4.3.0)
 httpuv        1.6.11     2023-05-11 [1] CRAN (R 4.3.1)
 jsonlite      1.8.4      2022-12-06 [1] CRAN (R 4.3.0)
 knitr         1.42       2023-01-25 [1] CRAN (R 4.3.0)
 later         1.3.1      2023-05-02 [1] CRAN (R 4.3.0)
 lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.3.0)
 lubridate     1.9.2      2023-02-10 [1] CRAN (R 4.3.0)
 magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.3.0)
 memoise       2.0.1      2021-11-26 [1] CRAN (R 4.3.0)
 mime          0.12       2021-09-28 [1] CRAN (R 4.3.0)
 miniUI        0.1.1.1    2018-05-18 [1] CRAN (R 4.3.0)
 munsell       0.5.0      2018-06-12 [1] CRAN (R 4.3.0)
 pillar        1.9.0      2023-03-22 [1] CRAN (R 4.3.0)
 pkgbuild      1.4.0      2022-11-27 [1] CRAN (R 4.3.0)
 pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.3.0)
 pkgload       1.3.2      2022-11-16 [1] CRAN (R 4.3.0)
 prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.3.0)
 processx      3.8.1      2023-04-18 [1] CRAN (R 4.3.0)
 profvis       0.3.8      2023-05-02 [1] CRAN (R 4.3.0)
 promises      1.2.0.1    2021-02-11 [1] CRAN (R 4.3.0)
 ps            1.7.5      2023-04-18 [1] CRAN (R 4.3.0)
 purrr         1.0.1      2023-01-10 [1] CRAN (R 4.3.0)
 R6            2.5.1      2021-08-19 [1] CRAN (R 4.3.0)
 Rcpp          1.0.10     2023-01-22 [1] CRAN (R 4.3.0)
 readr       * 2.1.4      2023-02-10 [1] CRAN (R 4.3.0)
 remotes       2.4.2      2021-11-30 [1] CRAN (R 4.3.0)
 rlang         1.1.1      2023-04-28 [1] CRAN (R 4.3.0)
 rmarkdown     2.21       2023-03-26 [1] CRAN (R 4.3.0)
 rprojroot     2.0.3      2022-04-02 [1] CRAN (R 4.3.0)
 rstudioapi    0.15.0     2023-07-07 [1] CRAN (R 4.3.1)
 scales      * 1.2.1      2022-08-20 [1] CRAN (R 4.3.1)
 sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.3.0)
 shiny       * 1.7.4      2022-12-15 [1] CRAN (R 4.3.0)
 stringi       1.7.12     2023-01-11 [1] CRAN (R 4.3.0)
 stringr       1.5.0      2022-12-02 [1] CRAN (R 4.3.0)
 tibble        3.2.1      2023-03-20 [1] CRAN (R 4.3.0)
 tidyselect    1.2.0      2022-10-10 [1] CRAN (R 4.3.0)
 timechange    0.2.0      2023-01-11 [1] CRAN (R 4.3.0)
 tzdb          0.3.0      2022-03-28 [1] CRAN (R 4.3.0)
 urlchecker    1.0.1      2021-11-30 [1] CRAN (R 4.3.0)
 usethis     * 2.2.2      2023-07-06 [1] CRAN (R 4.3.1)
 utf8          1.2.3      2023-01-31 [1] CRAN (R 4.3.0)
 vctrs         0.6.2      2023-04-19 [1] CRAN (R 4.3.0)
 withr         2.5.0      2022-03-03 [1] CRAN (R 4.3.0)
 xfun          0.39       2023-04-20 [1] CRAN (R 4.3.0)
 xtable        1.8-4      2019-04-21 [1] CRAN (R 4.3.0)
 yaml          2.3.7      2023-01-23 [1] CRAN (R 4.3.0)

 [1] C:/Users/sharl/AppData/Local/R/win-library/4.3
 [2] C:/Program Files/R/R-4.3.0/library

──────────────────────────────────────────────────────────────────────────────