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.0.2 (2020-06-22)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RTerm                       
 language (EN)                        
 collate  English_Canada.1252         
 ctype    English_Canada.1252         
 tz       America/New_York            
 date     2020-09-04                  

- Packages -------------------------------------------------------------------
 ! package     * version    date       lib source                     
 P assertthat    0.2.1      2019-03-21 [?] CRAN (R 4.0.0)             
 P backports     1.1.8      2020-06-17 [?] CRAN (R 4.0.0)             
 P blogdown      0.20       2020-06-23 [?] CRAN (R 4.0.2)             
 P bookdown      0.20       2020-06-23 [?] CRAN (R 4.0.0)             
 P callr         3.4.3      2020-03-28 [?] CRAN (R 4.0.0)             
 P cli           2.0.2      2020-02-28 [?] CRAN (R 4.0.0)             
 P colorspace    1.4-1      2019-03-18 [?] CRAN (R 4.0.0)             
 P crayon        1.3.4      2017-09-16 [?] CRAN (R 4.0.0)             
 P desc          1.2.0      2018-05-01 [?] CRAN (R 4.0.0)             
 P devtools    * 2.3.1      2020-07-21 [?] CRAN (R 4.0.2)             
 P digest        0.6.25     2020-02-23 [?] CRAN (R 4.0.0)             
 P dplyr       * 1.0.1      2020-07-31 [?] CRAN (R 4.0.2)             
 P ellipsis      0.3.1      2020-05-15 [?] CRAN (R 4.0.2)             
 P emo         * 0.0.0.9000 2020-07-07 [?] Github (hadley/[email protected])
 P evaluate      0.14       2019-05-28 [?] CRAN (R 4.0.0)             
 P fansi         0.4.1      2020-01-08 [?] CRAN (R 4.0.0)             
 P fastmap       1.0.1      2019-10-08 [?] CRAN (R 4.0.0)             
 P fs            1.5.0      2020-07-31 [?] CRAN (R 4.0.2)             
 P generics      0.0.2      2018-11-29 [?] CRAN (R 4.0.0)             
 P ggplot2     * 3.3.2      2020-06-19 [?] CRAN (R 4.0.2)             
 P glue          1.4.1      2020-05-13 [?] CRAN (R 4.0.2)             
 P gtable        0.3.0      2019-03-25 [?] CRAN (R 4.0.0)             
 P here        * 0.1        2017-05-28 [?] CRAN (R 4.0.2)             
 P hms           0.5.3      2020-01-08 [?] CRAN (R 4.0.0)             
 P htmltools     0.5.0      2020-06-16 [?] CRAN (R 4.0.2)             
 P httpuv        1.5.4      2020-06-06 [?] CRAN (R 4.0.2)             
 P knitr         1.29       2020-06-23 [?] CRAN (R 4.0.2)             
 P later         1.1.0.1    2020-06-05 [?] CRAN (R 4.0.2)             
 P lifecycle     0.2.0      2020-03-06 [?] CRAN (R 4.0.0)             
 P lubridate     1.7.9      2020-06-08 [?] CRAN (R 4.0.2)             
 P magrittr      1.5        2014-11-22 [?] CRAN (R 4.0.0)             
 P memoise       1.1.0      2017-04-21 [?] CRAN (R 4.0.0)             
 P mime          0.9        2020-02-04 [?] CRAN (R 4.0.0)             
 P munsell       0.5.0      2018-06-12 [?] CRAN (R 4.0.0)             
 P pillar        1.4.6      2020-07-10 [?] CRAN (R 4.0.2)             
 P pkgbuild      1.1.0      2020-07-13 [?] CRAN (R 4.0.2)             
 P pkgconfig     2.0.3      2019-09-22 [?] CRAN (R 4.0.0)             
 P pkgload       1.1.0      2020-05-29 [?] CRAN (R 4.0.2)             
 P prettyunits   1.1.1      2020-01-24 [?] CRAN (R 4.0.0)             
 P processx      3.4.3      2020-07-05 [?] CRAN (R 4.0.2)             
 P promises      1.1.1      2020-06-09 [?] CRAN (R 4.0.2)             
 P ps            1.3.4      2020-08-11 [?] CRAN (R 4.0.2)             
 P purrr         0.3.4      2020-04-17 [?] CRAN (R 4.0.0)             
 P R6            2.4.1      2019-11-12 [?] CRAN (R 4.0.0)             
 P Rcpp          1.0.5      2020-07-06 [?] CRAN (R 4.0.2)             
 P readr       * 1.3.1      2018-12-21 [?] CRAN (R 4.0.0)             
 P remotes       2.2.0      2020-07-21 [?] CRAN (R 4.0.2)             
   renv          0.11.0     2020-06-26 [1] CRAN (R 4.0.2)             
 P rlang         0.4.7      2020-07-09 [?] CRAN (R 4.0.2)             
 P rmarkdown     2.3        2020-06-18 [?] CRAN (R 4.0.2)             
 P rprojroot     1.3-2      2018-01-03 [?] CRAN (R 4.0.0)             
 P scales      * 1.1.1      2020-05-11 [?] CRAN (R 4.0.2)             
 P sessioninfo   1.1.1      2018-11-05 [?] CRAN (R 4.0.0)             
 P shiny       * 1.5.0      2020-06-23 [?] CRAN (R 4.0.2)             
 P stringi       1.4.6      2020-02-17 [?] CRAN (R 4.0.0)             
 P stringr       1.4.0      2019-02-10 [?] CRAN (R 4.0.0)             
 P testthat      2.3.2      2020-03-02 [?] CRAN (R 4.0.0)             
 P tibble        3.0.3      2020-07-10 [?] CRAN (R 4.0.2)             
 P tidyselect    1.1.0      2020-05-11 [?] CRAN (R 4.0.2)             
 P usethis     * 1.6.1      2020-04-29 [?] CRAN (R 4.0.2)             
 P vctrs         0.3.2      2020-07-15 [?] CRAN (R 4.0.2)             
 P withr         2.2.0      2020-04-20 [?] CRAN (R 4.0.0)             
 P xfun          0.16       2020-07-24 [?] CRAN (R 4.0.2)             
 P xtable        1.8-4      2019-04-21 [?] CRAN (R 4.0.0)             
 P yaml          2.2.1      2020-02-01 [?] CRAN (R 4.0.0)             

[1] C:/Users/shw/Desktop/blog2/renv/library/R-4.0/x86_64-w64-mingw32
[2] C:/Users/shw/AppData/Local/Temp/RtmpeGMEPO/renv-system-library

 P -- Loaded and on-disk path mismatch.