Animal Shelter Data Dashboard

Animal Shelter Data Collection & Analysis

The first section is a data collection form.

This form works so that you can enter the data to create a csv file that contains useful information about your shelter that can be used in analysis and decision making. The categories listed in this form represent values that correspond directly with the dashboard analysis and would be helpful for analysis purposes.

This form helps to standardize the data collection process and will allow for the easiest way to plug in data.

Data values that this form includes are: animal_id, animal_type, breed, age, sex, color, name, size, outcome_type, intake_type, intake_condition, intake_weekday, intake_month, outcome_weekday, outcome_month, intake_date, outcome_date.

These categories are some that will be useful in leveraging your data and determining trends that could provide insights into how your shelter works and how you can continue to strategize moving forward.

If you have a file with these values already, you can upload that file into the second section. If you do not, you can use this form to upload your data. Once your file is created and your values filled, you can continue to upload data to it in the future.

library(shiny)

# UI definition
    ui <- fluidPage(
  titlePanel("Animal Shelter Data Entry"),
  sidebarLayout(
    sidebarPanel(
      textInput("file_path", "File Path", placeholder = "Enter file path"),
      textInput("animal_id", label = "Animal ID", placeholder = "Enter Animal ID"),
      selectInput("animal_type", label = "Animal Type", choices = c("Select Animal Type", "Dog", "Cat", "Rabbit", "Other")),
      textInput("breed", label = "Breed", placeholder = "Enter Breed"),
      numericInput("age", label = "Age", value = NULL),
      selectInput("sex", label = "Sex", choices = c("Select Sex", "Intact Male", "Intact Female", "Neutered Male", "Spayed Female", "Unknown")),
      textInput("color", label = "Color", placeholder = "Enter Color"),
      textInput("name", label = "Name", placeholder = "Enter Name"),
      selectInput("size", label = "Size", choices = c("Select Size", "Small", "Medium", "Large")),
      selectInput("outcome_type", label = "Outcome Type", choices = c("Select Outcome Type", "Adoption", "Euthanasia", "Transfer", "Return to Owner", "Other")),
      selectInput("intake_type", label = "Intake Type", choices = c("Select Intake Type", "Stray", "Relinquished by Owner", "Transferred In", "Owner Intended Euthanasia", "Other")),
      selectInput("intake_condition", label = "Intake Condition", choices = c("Select Intake Condition", "Aged", "Feral", "Injured", "Normal", "Nursing", "Other", "Pregnant", "Sick")),
      selectInput("intake_weekday", label = "Intake Weekday", choices = c("Select Intake Weekday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")),
      selectInput("intake_month", label = "Intake Month", choices = c("Select Intake Month", month.name)),
      selectInput("outcome_weekday", label = "Outcome Weekday", choices = c("Select Outcome Weekday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")),
      selectInput("outcome_month", label = "Outcome Month", choices = c("Select Outcome Month", month.name)),
      dateInput("intake_date", label = "Intake Date", value = NULL),
      dateInput("outcome_date", label = "Outcome Date", value = NULL)
    ),
    mainPanel(
      actionButton("submit", "Submit"),
      br(),
      tableOutput("recent_entry_table")
    )
  )
)


# Server logic
server <- function(input, output, session) {
  # Store the Data
  data <- reactiveValues(entries = data.frame())

  # Show the most recent entry
  output$recent_entry_table <- renderTable({
    data$entries
  })

  # Submit button
observeEvent(input$submit, {
  # Show loading
  showModal(modalDialog(
    title = "Processing",
    "Please wait while the data is being processed...",
    footer = NULL,
    size = "m",
    backdrop = "static",
    keyboard = FALSE
  ))

  # validate fields
  if (input$animal_id == "" || input$animal_type == "Select Animal Type" || input$breed == "" || is.null(input$age) || input$sex == "Select Sex" || input$color == "" || input$name == "" || input$size == "Select Size" || input$outcome_type == "Select Outcome Type" || input$intake_type == "Select Intake Type" || input$intake_condition == "Select Intake Condition" || input$intake_weekday == "Select Intake Weekday" || input$intake_month == "Select Intake Month" || input$outcome_weekday == "Select Outcome Weekday" || input$outcome_month == "Select Outcome Month" || is.null(input$intake_date) || is.null(input$outcome_date)) {
    # close loading
    removeModal()

    # validation error
    showModal(modalDialog(
      title = "Validation Error",
      "Please fill out all required fields.",
      easyClose = TRUE
    ))
    return()
  }

  tryCatch({
    # data frame creation
    new_entry <- data.frame(
      animal_id = input$animal_id,
      animal_type = input$animal_type,
      breed = input$breed,
      age = input$age,
      sex = input$sex,
      color = input$color,
      name = input$name,
      size = input$size,
      outcome_type = input$outcome_type,
      intake_type = input$intake_type,
      intake_condition = input$intake_condition,
      intake_weekday = input$intake_weekday,
      intake_month = input$intake_month,
      outcome_weekday = input$outcome_weekday,
      outcome_month = input$outcome_month,
      intake_date = input$intake_date,
      outcome_date = input$outcome_date
    )

    # length of stay
    new_entry$time_in_shelter_days <- as.numeric(difftime(input$outcome_date, input$intake_date, units = "days"))

    # new entry
    data$entries <- rbind(data$entries, new_entry)

    # file path
    file_path <- input$file_path

    # CSV file
    if (!file.exists(file_path)) {
      write.csv(new_entry, file_path, row.names = FALSE)
    } else {
      write.table(new_entry, file_path, append = TRUE, sep = ",", row.names = FALSE, col.names = FALSE)
    }

    # clear form after submit
    updateTextInput(session, "animal_id", value = "")
    updateSelectInput(session, "animal_type", selected = "Select Animal Type")
    updateTextInput(session, "breed", value = "")
    updateNumericInput(session, "age", value = NULL)
    updateSelectInput(session, "sex", selected = "Select Sex")
    updateTextInput(session, "color", value = "")
    updateTextInput(session, "name", value = "")
    updateSelectInput(session, "size", selected = "Select Size")
    updateSelectInput(session, "outcome_type", selected = "Select Outcome Type")
    updateSelectInput(session, "intake_type", selected = "Select Intake Type")
    updateSelectInput(session, "intake_condition", selected = "Select Intake Condition")
    updateSelectInput(session, "intake_weekday", selected = "Select Intake Weekday")
    updateSelectInput(session, "intake_month", selected = "Select Intake Month")
    updateSelectInput(session, "outcome_weekday", selected = "Select Outcome Weekday")
    updateSelectInput(session, "outcome_month", selected = "Select Outcome Month")
    updateDateInput(session, "intake_date", value = NULL)
    updateDateInput(session, "outcome_date", value = NULL)

    # close
    removeModal()

    # success
    showModal(modalDialog(
      title = "Success",
      "Successful Data Submission:)",
      easyClose = TRUE
    ))
  }, error = function(e) {
    # close
    removeModal()

    # error
    showModal(modalDialog(
      title = "Error",
      paste("An error occurred:(", e$message),
      easyClose = TRUE
    ))
  })
})

}

# Run shiny App
shinyApp(ui = ui, server = server)

Here, you can upload your csv file and a dashboard with various tabs of analytics will be displayed.

library(shiny)
library(shinydashboard)

Attaching package: 'shinydashboard'
The following object is masked from 'package:graphics':

    box
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
# Main Shiny dashboard UI
ui <- dashboardPage(
  dashboardHeader(title = "Animal Shelter Analysis"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Upload CSV", tabName = "upload_csv", icon = icon("upload")), # New menu item for uploading CSV
      menuItem("Breed Counts", tabName = "breed_counts"),
      menuItem("Average Length of Stay", tabName = "length_of_stay"),
      menuItem("Breed by Animal Type", tabName = "breed_by_animal_type"),
      menuItem("Interactive Breed Chart", tabName = "interactive_breed_chart"),
      menuItem("Interactive Age Chart",tabName = "interactive_age_chart"),
      menuItem("Intake Months by Breed", tabName = "intake_months_by_breed"),
      menuItem("Outcome Months by Breed", tabName = "outcome_months_by_breed"),
      menuItem("Intake Months by Color", tabName = "intake_months_by_color"),
      menuItem("Outcome Months by Color", tabName = "outcome_months_by_color")
      # Add additional menu items for other analyses or tasks
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "upload_csv",
              fileInput("file", "Upload CSV file")), # File input control in the upload tab
      
      tabItem(tabName = "breed_counts",
              plotlyOutput("breed_counts_plot")),
      
      tabItem(tabName = "length_of_stay",
              plotOutput("length_of_stay_plot")),
      
      tabItem(tabName = "breed_by_animal_type",
              plotlyOutput("breed_by_animal_type_plot")),
      
      tabItem(tabName = "interactive_breed_chart",
              uiOutput("breed_selection"), # Render breed selection UI
              plotOutput("avg_los_plot")), # Render plot of average length of stay by breed
      
      tabItem(tabName = "interactive_age_chart",
              uiOutput("age_selection"), # Render breed selection UI
              plotOutput("avg_los_plot_age")),
      
      tabItem(tabName = "intake_months_by_breed",
              uiOutput("breed_selection_intake"),
              plotOutput("intake_months_plot_by_breed")),
      
      tabItem(tabName = "outcome_months_by_breed",
              uiOutput("breed_selection_outcome"),
              plotOutput("outcome_months_plot_by_breed"),
              plotOutput("outcome_type_plot")),
      
      
      tabItem(tabName = "intake_months_by_color",
              uiOutput("color_selection_intake"),
              plotOutput("intake_months_plot_by_color")),
      
      tabItem(tabName = "outcome_months_by_color",
              uiOutput("color_selection_outcome"),
              plotOutput("outcome_months_plot_by_color"))
    )
  )
)

# Main Shiny dashboard server logic
server <- function(input, output, session) {
  # Reactive expression to read uploaded CSV file
  dataset <- reactive({
    req(input$file) # Require a file to be uploaded
    read.csv(input$file$datapath, stringsAsFactors = FALSE)
  })
  
  # Render breed counts plot
  output$breed_counts_plot <- renderPlotly({
    req(dataset())
    breed_counts <- dataset() %>% count(breed)
    plot_ly(data = breed_counts, x = ~breed, y = ~n, type = 'bar') %>%
      layout(title = "Breed Counts", xaxis = list(title = "Breed"), yaxis = list(title = "Count"))
  })
  
  # Render average length of stay plot
  output$length_of_stay_plot <- renderPlot({
    req(dataset())
    # Example: Plot average length of stay
    # Assuming 'time_in_shelter_days' is the column representing length of stay
    ggplot(dataset(), aes(x = animal_type, y = time_in_shelter_days)) +
      geom_boxplot() +
      labs(title = "Average Length of Stay by Animal Type", x = "Animal Type", y = "Length of Stay (Days)")
  })
  
  # Render breed by animal type plot
  output$breed_by_animal_type_plot <- renderPlotly({
    req(dataset())
    breed_by_animal_type <- dataset() %>% count(breed, animal_type) %>% 
      mutate(animal_type = factor(animal_type, levels = c("Cat", "Dog"))) # Ensure correct ordering in plot
    plot_ly(data = breed_by_animal_type, x = ~breed, y = ~n, color = ~animal_type, type = 'bar') %>%
      layout(title = "Breed by Animal Type", xaxis = list(title = "Breed"), yaxis = list(title = "Count"))
  })
  
  # Render interactive breed selection input
  output$breed_selection <- renderUI({
    selectInput("selected_breed", "Select Breed", choices = c("All", unique(dataset()$breed)))
  })
  
  # Reactive expression to calculate average length of stay by breed
  avg_los_by_breed <- reactive({
    req(dataset()) # Require dataset to be available
    
    # Calculate average length of stay by breed
    breed_los <- dataset() %>%
      group_by(breed) %>%
      summarize(avg_length_of_stay = mean(time_in_shelter_days, na.rm = TRUE))
    
    # Remove breeds with NA values
    breed_los <- breed_los[!is.na(breed_los$avg_length_of_stay), ]
    
    # Sort by average length of stay in descending order
    breed_los <- breed_los[order(breed_los$avg_length_of_stay, decreasing = TRUE), ]
    
    return(breed_los)
  })
  
  # Render plot of average length of stay by breed
  output$avg_los_plot <- renderPlot({
    req(input$selected_breed) # Require breed selection
    
    # Filter data based on selected breed
    filtered_data <- dataset()
    if (input$selected_breed != "All") {
      filtered_data <- filtered_data[filtered_data$breed == input$selected_breed, ]
    }
    
    # Plot average length of stay
    ggplot(filtered_data, aes(x = breed, y = time_in_shelter_days)) +
      geom_bar(stat = "summary", fun = "mean", fill = "skyblue", color = "black") +
      labs(title = "Average Length of Stay by Breed", x = "Breed", y = "Average Length of Stay (Days)") +
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
  })
  
  # Render interactive age selection input
  output$age_selection <- renderUI({
    selectInput("selected_age", "Select Age", choices = c("All", unique(dataset()$age)))
  })
  
  # Reactive expression to calculate average length of stay by age
  avg_los_by_age <- reactive({
    req(dataset()) # Require dataset to be available
    
    # Calculate average length of stay by age
    age_los <- dataset() %>%
      group_by(age) %>%
      summarize(avg_length_of_stay = mean(time_in_shelter_days, na.rm = TRUE))
    
    # Remove ages with NA values
    age <- age_los[!is.na(age_los$avg_length_of_stay), ]
    
    # Sort by average length of stay in descending order
    age_los <- age_los[order(age_los$avg_length_of_stay, decreasing = TRUE), ]
    
    return(age_los)
  })
  
  # Render plot of average length of stay by age
  output$avg_los_plot_age <- renderPlot({
    req(input$selected_age) # Require breed selection
    
    # Filter data based on selected age
    filtered_data <- dataset()
    if (input$selected_age != "All") {
      filtered_data <- filtered_data[filtered_data$age == input$selected_age, ]
    }
    
    # Plot average length of stay
    ggplot(filtered_data, aes(x = age, y = time_in_shelter_days)) +
      geom_bar(stat = "summary", fun = "mean", fill = "skyblue", color = "black") +
      labs(title = "Average Length of Stay by Age", x = "Age", y = "Average Length of Stay (Days)") +
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
  })
  
  # Render breed selection dropdown for intake months
  output$breed_selection_intake <- renderUI({
    selectInput("selected_breed_intake", "Select Breed", choices = c("All", unique(dataset()$breed)))
  })
  
  # Render breed selection dropdown for outcome months
  output$breed_selection_outcome <- renderUI({
    selectInput("selected_breed_outcome", "Select Breed", choices = c("All", unique(dataset()$breed)))
    selectInput("outcome_month_filter", "Filter by Outcome Month", choices = c("All", unique(dataset()$outcome_month)))
    selectInput("outcome_type_filter", "Filter by Outcome Type", choices = c("All", unique(dataset()$outcome_type)))
  })
  
  
  # Render color selection dropdown for intake months
  output$color_selection_intake <- renderUI({
    selectInput("selected_color_intake", "Select Color", choices = c("All", unique(dataset()$color)))
  })
  
  # Render color selection dropdown for outcome months
  output$color_selection_outcome <- renderUI({
    selectInput("selected_color_outcome", "Select Color", choices = c("All", unique(dataset()$color)))
  })
  
  

  
  # Reactive expression to filter data based on selected breed for intake months
  filtered_data_intake_by_breed <- reactive({
    req(dataset()) # Require dataset to be available
    
    filtered <- dataset()
    if (input$selected_breed_intake != "All") {
      filtered <- filtered[filtered$breed == input$selected_breed_intake, ]
    }
    filtered
  })
  
  # Reactive expression to filter data based on selected breed for outcome months
  filtered_data_outcome_by_breed <- reactive({
    req(dataset()) # Require dataset to be available
    
    filtered <- dataset()
    if (input$selected_breed_outcome != "All") {
      filtered <- filtered[filtered$breed == input$selected_breed_outcome, ]
    }
    filtered
  })
  
  # Reactive expression to filter data based on selected color for intake months
  filtered_data_intake_by_color <- reactive({
    req(dataset()) # Require dataset to be available
    
    filtered <- dataset()
    if (input$selected_color_intake != "All") {
      filtered <- filtered[filtered$color == input$selected_color_intake, ]
    }
    filtered
  })
  
  # Reactive expression to filter data based on selected color for outcome months
  filtered_data_outcome_by_color <- reactive({
    req(dataset()) # Require dataset to be available
    
    filtered <- dataset()
    if (input$selected_color_outcome != "All") {
      filtered <- filtered[filtered$color == input$selected_color_outcome, ]
    }
    filtered
  })
  
  # Render plot for intake months by breed
  output$intake_months_plot_by_breed <- renderPlot({
    req(filtered_data_intake_by_breed())
    ggplot(filtered_data_intake_by_breed(), aes(x = intake_month)) +
      geom_bar() +
      labs(title = "Intake Months by Breed", x = "Month", y = "Count")
  })
  # Reactive expression to filter data based on selected outcome month
filtered_data_outcome_by_month <- reactive({
  req(dataset()) # Require dataset to be available
  
  filtered <- dataset()
  if (input$outcome_month_filter != "All") {
    filtered <- filtered[filtered$outcome_month == input$outcome_month_filter, ]
  }
  filtered
})


  # Render plot for outcome months by breed
  output$outcome_months_plot_by_breed <- renderPlot({
    req(filtered_data_outcome_by_breed())
    filtered_data <- filtered_data_outcome_by_breed()
    adoption_data <- filtered_data[filtered_data$outcome_type == "Adoption", ]
    ggplot(adoption_data, aes(x = outcome_month)) +
      geom_bar() +
      labs(title = "Adoptions by Month", x = "Month", y = "Count")
  })

  # Render plot for outcome months by month
  output$outcome_months_plot_by_month <- renderPlot({
    req(filtered_data_outcome_by_month())
    adoption_data <- filtered_data_outcome_by_month()[filtered_data_outcome_by_month()$outcome_type == "Adoption", ]
    ggplot(adoption_data, aes(x = outcome_month)) +
      geom_bar() +
      labs(title = "Adoptions by Month", x = "Month", y = "Count")
  })
  
  # Render plot for outcome type distribution
  output$outcome_type_plot <- renderPlot({
    req(dataset())
  
  # Filter data based on selected outcome type
    filtered_data <- dataset()
    if (input$outcome_type_filter != "All") {
      filtered_data <- filtered_data[filtered_data$outcome_type == input$outcome_type_filter, ]
  }
  
  # Count occurrences of each outcome type
    outcome_counts <- table(filtered_data$outcome_type)
  
  # Create a bar plot
   barplot(outcome_counts, main = "Outcome Type Distribution", xlab = "Outcome Type", ylab = "Count")
})

  
  # Render plot for intake months by color
  output$intake_months_plot_by_color <- renderPlot({
    req(filtered_data_intake_by_color())
    ggplot(filtered_data_intake_by_color(), aes(x = intake_month)) +
      geom_bar() +
      labs(title = "Intake Months by Color", x = "Month", y = "Count")
  })
  
  # Render plot for outcome months by color
  output$outcome_months_plot_by_color <- renderPlot({
    req(filtered_data_outcome_by_color())
    ggplot(filtered_data_outcome_by_color(), aes(x = outcome_month)) +
      geom_bar() +
      labs(title = "Outcome Months by Color", x = "Month", y = "Count")
  })
}

# Run the Shiny app
shinyApp(ui, server)

The echo: false option disables the printing of code (only output is displayed).