且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将闪亮的单选按钮重置为空值

更新时间:2023-01-28 10:59:21

人们普遍同意(尽管当然没有任何基于意见是 100% 的),如果您希望用户能够恢复,则不应使用单选按钮回到原来的无选择状态.甚至闪亮单选按钮的文档也是这样说的:

It's generally agreed upon (though of course nothing opinion-based is 100%) that radio buttons should not be used if you want the user to be able to revert back to the original no selection state. Even the documentation for radio buttons in shiny says this:

如果您需要表示未选择"状态,可以使用 selected = character(0) 将单选按钮默认为不选择任何选项.但是,不建议这样做,因为它使用户在做出选择后无法返回到该状态.相反,考虑让您的第一个选择是 c("None selected" = "").

If you need to represent a "None selected" state, it's possible to default the radio buttons to have no options selected by using selected = character(0). However, this is not recommended, as it gives the user no way to return to that state once they've made a selection. Instead, consider having the first of your choices be c("None selected" = "").

如果您在网上查找讨论,您还会看到人们普遍认为没有初始选择的单选按钮是可以的,但需要注意的是,这通常意味着用户在做出选择后无法返回该状态,因为-单选按钮本身并不真正支持选择.

If you look up discussions online you'll also see that it's commonly argued that a radio button with no initial selection is ok, with the caveat that often it means the user cannot go back to that state after making a selection because de-selecting is not really natively supported by radio buttons.

更新

使用updateRadioButtons() 的更新可能看起来在视觉上工作,但它实际上并没有将值重置为未选中状态.输入的基础价值不变.证据如下:

An update using updateRadioButtons() may appear to work visually, but it doesn't actually reset the value to be unselected. The underlying value of the input is unchanged. Here's proof:

library(shiny)

ui <- fluidPage(
  radioButtons("btn","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,{
    updateRadioButtons(session,"btn",choices  = c("Choice 1","Choice s"),selected = character(0))
  })

  observeEvent(input$showValue, {
    print(input$btn)
  })
}

shinyApp(ui, server)