在闪亮的小部件中存储输入值?

Stored Input values in shiny widgets?

我正在尝试了解存储在 shiny inputwidgets 中的值。我在下面编写了输出 sliderInput 的代码,并根据 sliderInput 的值生成了另一个 o/p 元素,即

如果sliderInput value > 30 那么它生成groupedradiobutton 否则它会生成一个 file upload button

library(shiny)
library(shinyWidgets)
ui <- fluidPage(
  sliderInput(inputId = "num", 
              label = "Choose a number", 
              value = 25, min = 1, max = 100),
  uiOutput("uploadorSelect"),
  textOutput("RadioButtonValue")
)

server <- function(input, output) {
  output$uploadorSelect <- renderUI({
    x<-input$num

    if( x > 30 ){
      # render grouped radio buttons  
      radioGroupButtons(inputId = "opbAppendRemoveMonthlyOption",choices =c("Append","Continue"), status = "success",
                        direction = "horizontal",justified = F,checkIcon = list(yes=icon("ok",lib="glyphicon")),selected = character(0))

    }else{
      # render upload button
      fileInput(inputId="btnUploadMonthlyFile",label = "Upload Monthly Data file") 
    }
  })

  output$RadioButtonValue<-renderText({
    x<-(input$opbAppendRemoveMonthlyOption)
    return(x)

  })
}

shinyApp(ui = ui, server = server)

情况:-

第 1 步 - 我将 sliderInput 的值更改为 31,这会生成 groupedradiobutton,然后我从 radiobutton

select Append

第 2 步 - 我再次将 sliderInput 的值更改为 32,这会重新生成 groupedradiobutton,我认为这应该将 output$RadioButtonValue 块中使用的 input$opbAppendRemoveMonthlyOption 的值重置为 Null,但不是那个它仍然保留在步骤 1

中编辑的值 select

我认为这是因为当我在 output$RadioButtonValue 中引用 input$opbAppendRemoveMonthlyOption 时它 returns 缓存值?如果是这样,每次更改 sliderInput 的值时如何将值设置为默认值?

您的应用设置正确,但问题似乎与 opbAppendRemoveMonthlyOption 的新值是 "nothing selected" 的消息丢失有关。例如,如果您将默认 selected 选项更改为 "Continue",则每次更改滑块时它都会正确重置。

通常,您也可以在反应式表达式中使用相应的更新函数来更改输入的值,如下所示:

  observeEvent({input$num}, {
    updateRadioGroupButtons(session, "opbAppendRemoveMonthlyOption",
                            selected = character(0))
  })

但是,就像在创建输入时一样,在尝试将值设置回未选择状态时会忽略此消息,但如果将其设置为其中一个选项,则此消息会起作用。请注意,Shiny 文档 (?radioButtons) 不鼓励使用未选中状态,因此可能无法完全支持它。

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" = "").

您可以通过一点 JavaScript 来解决这个问题,以在每次单击滑块时强制重置 input 值。

library(shiny)
library(shinyWidgets)
ui <- fluidPage(
  tags$div(
    sliderInput(inputId = "num", 
                label = "Choose a number", 
                value = 25, min = 1, max = 100),
    onchange = "Shiny.onInputChange('opbAppendRemoveMonthlyOption', '')"
  ),
  uiOutput("uploadorSelect"),
  textOutput("RadioButtonValue")
)

server <- function(input, output) {
  output$uploadorSelect <- renderUI({
    x<-input$num

    if( x > 30 ){
      # render grouped radio buttons  
      radioGroupButtons(inputId = "opbAppendRemoveMonthlyOption",choices =c("Append","Continue"), status = "success",
                        direction = "horizontal",justified = F,checkIcon = list(yes=icon("ok",lib="glyphicon")),selected = character(0))

    }else{
      # render upload button
      fileInput(inputId="btnUploadMonthlyFile",label = "Upload Monthly Data file") 
    }
  })

  output$RadioButtonValue<-renderText({
    x<-(input$opbAppendRemoveMonthlyOption)
    return(x)

  })
}

shinyApp(ui = ui, server = server)

More info Shiny<->JavaScript messaging from RStudio.com