R Shiny - radioButtons中没有选定的初始值?

11

来自文档:

radioButtons(inputId, label, choices, selected = NULL, inline = FALSE, width = NULL)

参数

selected 初始选中的值(如果未指定,则默认为第一个值)

但是,你能否指定selected参数,以便呈现的radioButtons没有初始选择的值?该初始值的输出可以是一个空字符串("")。在下面的示例中,当运行应用程序时,既不应选择"a"也不应选择"b"。

library(shiny)

runApp(
  list(
    ui = shinyUI(
      fluidPage(
        radioButtons("test","test",choices=c("a","b"),selected = NULL),
        verbatimTextOutput("value")
      )
    ), server = shinyServer(function(input, output,session) {
      output$value <- renderText(input$test)
    })
  )
)

更新

根据Hackerman的建议,selected = character(0) 可以解决问题。


好的,当你运行那段代码时,会发生什么? - Hackerman
当我运行它时,“a”按钮(第一个值)已经被选中。我希望没有按钮被选中。 - InspectorSands
1
你可以使用 selected = character(0) 替代 selected = NULL - Hackerman
非常完美地工作,谢谢。 - InspectorSands
2个回答

19

selected 的问题在于如果未指定,则默认为第一个值。但是您可以使用以下方法解决此问题:

selected = character(0)

另一种方法是提供一个默认选项,比如“未选择任何内容”。
radioButtons("test","test",choices=c("Nothing Selected"="","a"="a","b"="b"),selected = NULL),

1
selected = character(0) 这个替代方案很简单,但似乎有点像是 hack。我担心它可能会产生副作用(虽然我现在想不出来)。 - InspectorSands
我能想到的最近似的效果是,一旦用户进行选择,那么用户就没有办法回到最初的阶段,也就是没有选中单选按钮的状态。为了解决这个问题,可以添加一个额外的单选按钮,上面写着“未选择”。 - Rohit Saluja
请注意,这不适用于 shinyWidgets 中的 awesomeRadio(尽管它适用于所述的 radioButtons)。 - nico
1
@nico 是的,这是一个将近4年前的答案......在那些年里我离R开发有一点远离 :) - Hackerman
哦,是的,但你的回答仍然很有用! - nico

1
假设您不想要一个初始值,因为它会被观察到,似乎有三种解决方案:
  • 使用额外的选项,例如称为“none”,请参阅radioButtons帮助
  • 使用character(0)(请参阅radioButtons帮助)
  • 使用额外的actionButton并观察该按钮。
如果您不想在用户界面中模糊化额外的radioButtons选择或actionButton,请使用character(0)。
但是,当使用'character(0)'时,您可能还需要解决副作用。问题是'character(0)'将不会将输入参数重置为NULL,因此您不能两次使用相同的选项(这可能是可取的,也可能不是)。这在以下示例程序中显示。
server <- function(input, output) {
output$uiRadioButtons <- renderUI({ radioButtons (inputId='actionId', label='action:', choices = c ('a', 'b', 'c'), selected=character(0)) })
n  <- 0
observe({
    actionId <- input$actionId
    n <<- n+1
    if (!is.null(actionId)) {
        if (actionId=='a')  output$action  <- renderText (paste (n, "action A"))
        if (actionId=='b')  output$action  <- renderText (paste (n, "action B"))
        if (actionId=='c')  output$action  <- renderText (paste (n, "action C"))
        output$uiRadioButtons <- renderUI({ radioButtons (inputId='actionId', label='action:', choices = c ('a', 'b', 'c'), selected=character(0)) })
    } else                  output$action  <- renderText ("actionId equals NULL")
}) } 
ui <- fluidPage (
    sidebarLayout(
        sidebarPanel ( uiOutput('uiRadioButtons')),
        mainPanel    (uiOutput('action'))
    ) ) 
shinyApp (ui = ui, server = server)

这可以通过使用和观察虚拟的radioButtons来解决(尽管对于远程应用程序可能太慢)。
server <- function(input, output) {
showActions <- function() {
    output$uiRadioButtons <- renderUI ({ radioButtons (inputId='actionId', label='action:', choices = c ('a', 'b', 'c'), selected=character(0)) })
}
showActions()
n  <- 0
observe({
    actionId <- input$actionId
    n <<- n+1
    if (!is.null(actionId)) {
        if (actionId=='dummy')  showActions ()
        else {
            if (actionId=='a')  output$action  <- renderText (paste (n, "action A"))
            if (actionId=='b')  output$action  <- renderText (paste (n, "action B"))
            if (actionId=='c')  output$action  <- renderText (paste (n, "action C"))
            output$uiRadioButtons <- renderUI({ radioButtons (inputId='actionId', label='action:', choices = 'dummy') })
        }
    } else                  output$action  <- renderText ("actionId equals NULL")

})
}
ui <- fluidPage (
    sidebarLayout(
        sidebarPanel (
            # radioButtons (inputId='objectId', label='selct object:', choices = c ('o1', 'o2', 'o3'), inline = TRUE),
            uiOutput('uiRadioButtons')
        ),
        mainPanel    (uiOutput('action'))
    )
)
shinyApp (ui = ui, server = server)

这看起来很丑,但它能正常工作。如果有人能向我展示更好的解决方案,比如将输入变量重置为NULL的方法,我会很高兴。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接