'multiple conditions observeEvent - R Shiny
I am trying to do a Shiny app with an interactive map displaying marine animal tags. Currently, I have three possible inputs : (1) The tag numbers, (2) the size of the animals and (3) the month I want to display. For the size it works easily as I update the tag number input, but I am stuck with the months ... I already tried with :
observeEvent({input$selected_tag
input$selected_month},{...
and by including the inputs inside a reactive expression but an error occur :
Avis : Error in sum: 'type' (list) de l'argument incorrect
Would you have an idea please ? Or are my inputs poorly defined (cf the error) ? Here is a reprex and a sample of my data https://github.com/rcanet/ABFT_migration/blob/main/test_file.csv (line with #### What happens when you click on a tag ? ####):
library(shiny)
library(leaflet)
library(lubridate)
#### Data loading ####
### Tags table
tag_data <- read.csv("test_file.csv", header = TRUE, sep=",",dec=".")
tag_data$tag <- as.factor(tag_data$tag)
id_tag <- levels(tag_data$tag)
tag_data$date = ymd(tag_data$date)
## Definition of the applicable filters
filtering_option <- c("Size", "Months")
## Definition of the seasons
months_tag <- c("January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December")
################################################################################
#### Shiny app UI ####
ui <- fluidPage(
tabsetPanel(
column(2, selectizeInput("selected_tag", "Which tags do you want to see ?",
choices = id_tag, multiple = TRUE),
hr(), ## Filters
p(strong("Which filter do you want to use ?")),
checkboxInput("filter_size", "Size"),
conditionalPanel(condition = "input.filter_size == 1",
sliderInput("slider_size", "Choose a size range (in cm)",
value = c(min(unique(tag_data$size_at_tagging)),
max(unique(tag_data$size_at_tagging))),
min = min(unique(tag_data$size_at_tagging)),
max = max(unique(tag_data$size_at_tagging)))
),
checkboxInput("filter_month", "Months"),
conditionalPanel(condition = "input.filter_month == 1",
selectizeInput("selected_month",
"Which months do you want to see ?",
choices = months_tag,
selected = months_tag[1],
multiple = TRUE)),
actionButton("reset_button", "Reset"),
textOutput("test_debbuging")),
column(10, leafletOutput("map_tag", height = "850px"))
))
################################################################################
#### Shiny app server ####
server <- function(input, output, session) {
observeEvent(input$selected_month, {cat(input$selected_month, "\n")})
observeEvent(input$selected_tag, {cat(input$selected_tag)})
}
shinyApp(ui, server)
'''
Solution 1:[1]
So after a few tries, I finally figure it out. The bug indeed was in the leaflet. The problem lied in the label argument of addAwesomeMarkers. Change "label" by "popout" and it should be okay ! (As I had to reduce my reprex the error is no longer in my initial post)
Solution 2:[2]
you can define a list of desired input parameters:
toListen <- reactive({
list(input$selected_tag, input$selected_month)
})
and then use your reactive list in combination with your observeEvent:
observeEvent(toListen(),{...})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rémy C. |
| Solution 2 | Daniel |
