我试图理解存储在shiny inputwidgets
中的值。我编写了下面的代码,该代码输出 sliderInput
并根据 sliderInput 的值生成另一个 o/p 元素,即
如果sliderInput
值> 30,那么它会生成groupedradiobutton
否则它会生成一个文件上传按钮
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,我相信这应该重置 output$RadioButtonValue
中使用的 input$opbAppendRemoveMonthlyOption
的值 block 为 Null,但它仍然保留在步骤 1 中选择的值
我认为这是因为当我在 output$RadioButtonValue
中引用 input$opbAppendRemoveMonthlyOption
时,它返回缓存的值?如果是这样,每次更改 sliderInput 的值时如何将值设置为默认值?
最佳答案
您的应用设置正确,但问题似乎在于 opbAppendRemoveMonthlyOption
的新值“未选择任何内容”的消息丢失。例如,如果您将默认的selected
选项更改为“继续”,那么每次更改 slider 时它都会正确重置。
通常,您还可以在响应式(Reactive)表达式中使用相应的更新函数来更改输入的值,如下所示:
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 来解决此问题,以在单击 slider 时强制重置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)
关于r - 将输入值存储在 Shiny 的小部件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48151915/