简体   繁体   English

多个条件 observeEvent - R Shiny

[英]multiple conditions observeEvent - R Shiny

I am trying to do a Shiny app with an interactive map displaying marine animal tags.我正在尝试制作一个 Shiny 应用程序,其中包含一个显示海洋动物标签的交互式 map。 Currently, I have three possible inputs: (1) The tag numbers, (2) the size of the animals and (3) the month I want to display.目前,我有三个可能的输入:(1) 标签编号,(2) 动物的大小和 (3) 我想要显示的月份。 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/rc.net/ABFT_migration/blob/main/test_file.csv (line with #### What happens when you click on a tag? ####):这是一个代表和我的数据样本https://github.com/rc.net/ABFT_migration/blob/main/test_file.csv (符合 #### 当你点击标签时会发生什么?### #):

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)

''' '''

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结合使用:

observeEvent(toListen(),{...})

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.该错误确实在 leaflet 中。问题在于 addAwesomeMarkers 的 label 参数。 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) (因为我不得不减少我的代表,错误不再出现在我最初的帖子中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM