将闪亮的单选按钮重置为空值 [英] Reset radioButtons in shiny to empty value

查看:66
本文介绍了将闪亮的单选按钮重置为空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 radioButtons 重置为空值(即好像单选按钮不会被点击并且默认值为空(selected = ""))?

Is it possible in shiny to reset radioButtons to an empty value (i.e. as if the radiobuttons would not have been clicked and the default value was empty (selected = ""))?

我尝试使用shinyjs() 和updateRadioButtons(session, "btn",...) 重置("btn").但是,我只设法将单选按钮重置为特定值(例如列表中的第一个值或其他预定义值),而不是空值.

I tried reset("btn") with shinyjs() and updateRadioButtons(session, "btn",...). However, I only managed to reset the radiobuttons to a specific value (e.g. the first value in the list or another predefined value), but not to an empty value.

重置为空值可以在指定的选择实验中使用一组单选按钮进行多次重复选择(结合 renderUI,其中单选按钮连续分配给不同的变量.

The reset to an empty value would make it possible to use a single set of radiobuttons for many repeated choices in a stated choice experiment (in combination with renderUI, where the radiobuttons are successively assinged to different variables.

推荐答案

人们普遍同意(尽管当然没有任何基于意见是 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)

这篇关于将闪亮的单选按钮重置为空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆