首页 文章

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

提问于
浏览
10

我试图理解 shiny inputwidgets 中存储的值 . 我写下面的代码输出 sliderInput 并根据sliderInput的值,生成另一个o / p元素,即

如果 sliderInput 值> 30,那么它会生成groupsradiobutton,否则会生成 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,生成分组数据,然后从radiobutton中选择 Append

第2步 - 我再次将sliderInput的值更改为32,重新生成分组的radiobutton,我认为应该将 output$RadioButtonValue 块中使用的 input$opbAppendRemoveMonthlyOption 的值重置为Null,但是它仍然保留在Step1中选择的值

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

1 回答

  • 5

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

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

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

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

    如果需要表示“未选择”状态,则可以通过使用selected = character(0)来默认单选按钮没有选择任何选项 . 但是,建议不要这样做,因为一旦他们做出选择,用户就无法返回该状态 . 相反,考虑让你的第一个选择是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

相关问题