在一个观察者中更新多个单选按钮仅适用于第一个单选按钮
Updating multiple radiobuttons in one observer only works on the first radiobutton
我完全不明白为什么两行代码(单独),其中 reset
各自 radiobutton
空(selected = character(0)
)只有 reset
第一个放在一起时的行 1 'observer'
这是最小的例子:
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
tags$script("
Shiny.addCustomMessageHandler('resetValue', function(variableName) {
Shiny.onInputChange(variableName, null);
});
"),
radioButtons("radio","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
radioButtons("radio2","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
actionButton("clickMe","Click Me to Reset"),
actionButton("showValue", "Show value")
)
server <- function(input, output, session) {
observeEvent(input$clickMe,{
session$sendCustomMessage(type = "resetValue", message ='radio')
updateRadioButtons(session,"radio",choices = c("Choice 1","Choice s"),selected = character(0))
session$sendCustomMessage(type = "resetValue", message ='radio2')
updateRadioButtons(session,"radio2",choices = c("Choice 1","Choice s"),selected = character(0))
})
observeEvent(input$showValue, {
print(input$radio)
print(input$radio2)
})
}
shinyApp(ui, server)
其中sendcustomMessages
用于reset
values
,这不是通过选择character(0)
实现的,它来自即
更新
我无法弄清楚为什么不能在一个 reactiveEvent 中更新两个单选按钮,但错误似乎源于将 selected =
参数设置为 character(0)
。虽然第一个 updateRadioButtons
可以与 character(0)
一起使用,但第二个不能。
将 updateRadioButtons
的 selected =
参数中的 character(0)
更改为 0
会产生所需的结果,并且不会以某种方式导致 problem/bug。比较按钮值,将按钮最初设置为 character(0)
或 0
似乎没有区别。两种方式的按钮值都是 NULL
。使用自定义 JavaScript 函数重置值后,JS 中的值将更新为 null
.
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
tags$script("
Shiny.addCustomMessageHandler('resetValue', function(variableName) {
Shiny.onInputChange(variableName, null);
});
"),
radioButtons("radio","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
radioButtons("radio2","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
actionButton("clickMe","Click Me to Reset"),
actionButton("showValue", "Show value")
)
server <- function(input, output, session) {
observeEvent(input$clickMe,{
session$sendCustomMessage(type = "resetValue", message ='radio')
updateRadioButtons(session,"radio",choices = c("Choice 1","Choice s"),selected = 0)
session$sendCustomMessage(type = "resetValue", message ='radio2')
updateRadioButtons(session,"radio2",choices = c("Choice 1","Choice s"),selected = 0)
})
observeEvent(input$showValue, {
print(input$radio)
print(input$radio2)
})
}
shinyApp(ui, server)
我完全不明白为什么两行代码(单独),其中 reset
各自 radiobutton
空(selected = character(0)
)只有 reset
第一个放在一起时的行 1 'observer'
这是最小的例子:
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
tags$script("
Shiny.addCustomMessageHandler('resetValue', function(variableName) {
Shiny.onInputChange(variableName, null);
});
"),
radioButtons("radio","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
radioButtons("radio2","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
actionButton("clickMe","Click Me to Reset"),
actionButton("showValue", "Show value")
)
server <- function(input, output, session) {
observeEvent(input$clickMe,{
session$sendCustomMessage(type = "resetValue", message ='radio')
updateRadioButtons(session,"radio",choices = c("Choice 1","Choice s"),selected = character(0))
session$sendCustomMessage(type = "resetValue", message ='radio2')
updateRadioButtons(session,"radio2",choices = c("Choice 1","Choice s"),selected = character(0))
})
observeEvent(input$showValue, {
print(input$radio)
print(input$radio2)
})
}
shinyApp(ui, server)
其中sendcustomMessages
用于reset
values
,这不是通过选择character(0)
实现的,它来自即
更新
我无法弄清楚为什么不能在一个 reactiveEvent 中更新两个单选按钮,但错误似乎源于将 selected =
参数设置为 character(0)
。虽然第一个 updateRadioButtons
可以与 character(0)
一起使用,但第二个不能。
将 updateRadioButtons
的 selected =
参数中的 character(0)
更改为 0
会产生所需的结果,并且不会以某种方式导致 problem/bug。比较按钮值,将按钮最初设置为 character(0)
或 0
似乎没有区别。两种方式的按钮值都是 NULL
。使用自定义 JavaScript 函数重置值后,JS 中的值将更新为 null
.
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
tags$script("
Shiny.addCustomMessageHandler('resetValue', function(variableName) {
Shiny.onInputChange(variableName, null);
});
"),
radioButtons("radio","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
radioButtons("radio2","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
actionButton("clickMe","Click Me to Reset"),
actionButton("showValue", "Show value")
)
server <- function(input, output, session) {
observeEvent(input$clickMe,{
session$sendCustomMessage(type = "resetValue", message ='radio')
updateRadioButtons(session,"radio",choices = c("Choice 1","Choice s"),selected = 0)
session$sendCustomMessage(type = "resetValue", message ='radio2')
updateRadioButtons(session,"radio2",choices = c("Choice 1","Choice s"),selected = 0)
})
observeEvent(input$showValue, {
print(input$radio)
print(input$radio2)
})
}
shinyApp(ui, server)