Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

r - Why does `observe` and `observeEvent` behave differently on change of input variable?

I found out a behavior that is at least confusing in Shiny, and I would like to figure out why it occurs.

I have a Shiny app with just a selectInput input that allows the selection of multiple values (multiple = TRUE) and two observers that listen to changes in the input variable: one that uses observeEvent directly on the input variable and one that uses just observe with the input variable inside its body.

The weirdness occurs when, in the multiple options, one removes all of the options in the selector. In that case, only the observe block of code executes and the observeEvent does not.

Why does Shiny do that? Should it work like that?

A minimal working example (you just need to deselect all of the variables):

library(shiny)

ui <- fluidPage(
    selectInput("select", 
                label = "Multiple choices",
                choices = c(1, 2, 3, 4),
                selected = c(1, 2), 
                multiple = TRUE)
)

server <- function(input, output) {
    # Won't execute with no value selected
    observeEvent(input$select, {
        vec_str <- paste(input$select, collapse = " ")
        print(paste("observeEvent:", vec_str))
    })
    
    # Will execute with no value selected
    observe({
        vec_str <- paste(input$select, collapse = " ")
        print(paste("observe:", vec_str))
    })
}

shinyApp(ui = ui, server = server)

Thanks in advance :)

question from:https://stackoverflow.com/questions/65924161/why-does-observe-and-observeevent-behave-differently-on-change-of-input-vari

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a setting in observeEvent that allows you to ignore the case when is.null(input$select). Set the argument ignoreNULL = FALSE and you get the result you want.

library(shiny)

ui <- fluidPage(
  selectInput("select", 
              label = "Multiple choices",
              choices = c(1, 2, 3, 4),
              selected = c(1, 2), 
              multiple = TRUE)
)

server <- function(input, output) {
   # executes even when input$select is NULL
  observeEvent(input$select, {
    vec_str <- paste(input$select, collapse = " ")
    print(paste("observeEvent:", vec_str))
  }, ignoreNULL = FALSE)
  
  # Will execute with no value selected
  observe({
    vec_str <- paste(input$select, collapse = " ")
    print(paste("observe:", vec_str))
  })
}

shinyApp(ui = ui, server = server)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...