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

如果sliderInput值> 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,然后从单选按钮中选择Append
第2步-我再次将sliderInput的值更改为32,从而重新生成了groupedradiobutton,我认为应该将input$opbAppendRemoveMonthlyOption块中使用的output$RadioButtonValue的值重置为Null,但仍然保留在Step1中选择的值

我认为这是由于以下事实:当我在input$opbAppendRemoveMonthlyOption中引用output$RadioButtonValue时,它会返回缓存的值吗?如果是这样的话,每当我更改sliderInput的值时,如何将值设置为默认值?

最佳答案

您的应用程序设置正确,但问题出在的消息是opbAppendRemoveMonthlyOption的新值“没有选择”丢失了。例如,如果将默认的selected选项更改为“Continue”,则每次更改滑块时它都会正确重置。

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

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

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



您可以使用一些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

关于r - 将输入值存储在 Shiny 的小部件中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48151915/

10-12 17:34