R Shiny - observeEvent 不拾取清除 selectInput 事件
R Shiny - observeEvent do not pick up clear selectInput event
我不确定我在这里是否遗漏了什么,当 selectInput(multi-select on)被清除时,似乎事件没有被 observeEvent 捕获。但是,它是使用 reactive() 捕获的。
请参阅下面的示例,目标是随着 select 输入的任何更改,程序将获取更改并显示在屏幕上。我使用了 2 个示例:
- 无法在 html_component2
上显示
- ReactiveValues 使用 observeEvent 在 html_component
上显示
对于反应函数,它工作得很好。对于后者,它适用于所有组合,除非 if user unselect everything。我真的很困惑为什么,以前有没有人见过这个问题,是否有任何解决方法?在这种情况下,我更愿意为我的应用程序使用反应值。
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("sinput", "select here", c(1,2,3,4,5), multiple = T),
),
# Show a plot of the generated distribution
mainPanel(
htmlOutput("html_component"),
htmlOutput("html_component2"),
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
rv <- reactiveValues()
sel <- reactive({
input$sinput
})
observeEvent(input$sinput, {
rv$selected = input$sinput
})
output$html_component <- renderUI({
HTML(paste(c("1:", rv$selected)))
})
output$html_component2 <- renderUI({
HTML(paste(c("2:", sel())))
})
}
# Run the application
shinyApp(ui = ui, server = server)
默认observeEvent
会忽略eventExpr
中的NULL,需要设置ignoreNULL = FALSE
:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("sinput", "select here", c(1,2,3,4,5), multiple = T),
),
# Show a plot of the generated distribution
mainPanel(
htmlOutput("html_component"),
htmlOutput("html_component2"),
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
rv <- reactiveValues()
sel <- reactive({
input$sinput
})
observeEvent(input$sinput, {
rv$selected = input$sinput
}, ignoreNULL = FALSE)
output$html_component <- renderUI({
HTML(paste(c("1:", rv$selected)))
})
output$html_component2 <- renderUI({
HTML(paste(c("2:", sel())))
})
}
# Run the application
shinyApp(ui = ui, server = server)
我不确定我在这里是否遗漏了什么,当 selectInput(multi-select on)被清除时,似乎事件没有被 observeEvent 捕获。但是,它是使用 reactive() 捕获的。
请参阅下面的示例,目标是随着 select 输入的任何更改,程序将获取更改并显示在屏幕上。我使用了 2 个示例:
- 无法在 html_component2 上显示
- ReactiveValues 使用 observeEvent 在 html_component 上显示
对于反应函数,它工作得很好。对于后者,它适用于所有组合,除非 if user unselect everything。我真的很困惑为什么,以前有没有人见过这个问题,是否有任何解决方法?在这种情况下,我更愿意为我的应用程序使用反应值。
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("sinput", "select here", c(1,2,3,4,5), multiple = T),
),
# Show a plot of the generated distribution
mainPanel(
htmlOutput("html_component"),
htmlOutput("html_component2"),
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
rv <- reactiveValues()
sel <- reactive({
input$sinput
})
observeEvent(input$sinput, {
rv$selected = input$sinput
})
output$html_component <- renderUI({
HTML(paste(c("1:", rv$selected)))
})
output$html_component2 <- renderUI({
HTML(paste(c("2:", sel())))
})
}
# Run the application
shinyApp(ui = ui, server = server)
默认observeEvent
会忽略eventExpr
中的NULL,需要设置ignoreNULL = FALSE
:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("sinput", "select here", c(1,2,3,4,5), multiple = T),
),
# Show a plot of the generated distribution
mainPanel(
htmlOutput("html_component"),
htmlOutput("html_component2"),
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
rv <- reactiveValues()
sel <- reactive({
input$sinput
})
observeEvent(input$sinput, {
rv$selected = input$sinput
}, ignoreNULL = FALSE)
output$html_component <- renderUI({
HTML(paste(c("1:", rv$selected)))
})
output$html_component2 <- renderUI({
HTML(paste(c("2:", sel())))
})
}
# Run the application
shinyApp(ui = ui, server = server)