I have a shiny app where the user can choose a range of dates, a wilderness, a species, a life stage, and finally a site id. What should appear is a line plot with bd_load for each time point. However, my script for some reason will only show one data point for the year 2016 no matter what range of dates I choose.
Data
bd_data <- bd_data <- structure(list(id = c(10008, 10008,10008), date = c(2018,2019,2020), species = c("ramu", "ramu", "ramu"),
wilderness = c("yosemite", "yosemite", "yosemite"), visual_life_stage = c("adult", "adult", "adult"),
bd = c(1,2,3)))
UI
ui <- fluidPage(
#includeCSS(here("NPS_ShinyApp/theme.css")),
theme = theme,
titlePanel(""),
fluidPage(
fluidRow(column(8,
h1(strong("National Park Service App - RIBBiTR ")))),
navbarPage("", inverse = T,
tabPanel("Home", icon = icon("info-circle"),
fluidPage(
fluidRow(
h1(strong("Disclaimer"), style = "font-size:20px;"),
column(12, p(""))),
fluidRow(
h1(strong("Intended Use"),style = "font-size:20px;"),
column(12, p(""))),
fluidRow(
h1(strong("Data Collection"),style = "font-size:20px;"),
column(12, p(""))))),
tabPanel(title = "Site Map", icon = icon("globe-asia"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bd_date",
label = "Select an annual range",
min = min(bd_data$date), max = max(bd_data$date),
value = c((max(bd_data$date) - 5), max(bd_data$date)),
sep = ""),
pickerInput(inputId = "wilderness_2",
label = "Select a wilderness",
choices = unique(bd_data$wilderness),
multiple = F,
selected = ""),
pickerInput(inputId = "bd_species",
label = "Select a species",
choices = unique(bd_data$species),
multiple = F,
selected = "ramu"),
pickerInput(inputId = "stage",
label = "Select a life stage",
choices = unique(bd_data$visual_life_stage),
selected = "adult",
multiple = F),
pickerInput(inputId = "bd_id",
label = "select site",
choices = unique(bd_data$id),
multiple = F)),
mainPanel(plotOutput(outputId = "bd_plots")))
)
Server
server <- function(input, output, session){
bd_reac <- reactive({
bd_data %>%
dplyr::filter(date %in% input$bd_date[1:2], wilderness == input$wilderness_2, species == input$bd_species,
visual_life_stage == input$stage, id == input$bd_id)
})
### I believe my error is occuring with the filtering or plotting of the data here or just above
output$bd_plots <- renderPlot({
ggplot(data = bd_reac()) +
geom_point(aes(x = date, y = bd)) +
xlim(c(input$bd_date[1:2]))
})
observeEvent(input$bd_date, {
updatePickerInput(session, inputId = "wilderness_2",
choices = unique(bd_data$wilderness[bd_data$date %in% input$bd_date[1:2]]),
selected = "yosemite")
})
observeEvent(input$wilderness_2, {
updatePickerInput(session, inputId = "bd_species",
choices = unique(bd_data$species[bd_data$date %in% input$bd_date[1:2]
& bd_data$wilderness == input$wilderness_2]))
})
observeEvent(input$bd_species, {
updatePickerInput(session, inputId = "stage",
choices = unique(bd_data$visual_life_stage[bd_data$date %in% input$bd_date[1:2]
& bd_data$wilderness == input$wilderness_2
& bd_data$species == input$bd_species]))
})
observeEvent(input$stage, {
updatePickerInput(session, inputId = "bd_id",
choices = unique(bd_data$id[bd_data$date %in% input$bd_date[1:2]
& bd_data$wilderness == input$wilderness_2
& bd_data$species == input$bd_species
& bd_data$visual_life_stage == input$stage]))
})
}
Global
if (!require(librarian)){
install.packages("librarian")
library(librarian)
}
# librarian downloads, if not already downloaded, and reads in needed packages
librarian::shelf(shiny, tidyverse, here, shinyWidgets, leafem, bslib, thematic, shinymanager, leaflet, ggrepel, sf)
date %in% input$bd_date[1:2]
, A simple check returns FALSE. Try checkingdate > input$date[1] & date < input$date[2]
. I didn't copy and download your code given it's not an MRE, so I may be off the mark in my suggestion.