我有一个双头闪亮的滑块输入,如下所示:
sliderInput(inputId = "years",
label="Choose base year and target year",
min = 2015,
max = 2060,
value = c(2015,2060),
step = 1,
sep = ""
)
用户上载值后,我想将滑块的下端固定为某个值;上端应仍可移动。我尝试通过使用updateSliderInput函数并仅为“值”属性提供一个值来实现这一点(因此,该滑块应成为常规的单端滑块):
observeEvent(upload(), {
updateSliderInput(session, inputId = "years", min = base_year(), max = 2060, value = 2060)
})
但是,这行不通;滑块仍然是双端的,并且两个手柄都设置为2060。如何将双端滑块转换为常规滑块?
亲切的问候,
Requin
答案 0 :(得分:2)
这是另一个更接近您的问题的解决方案,只需按一下按钮即可触发一次:
library(shiny)
ui <- fluidPage(
uiOutput("Slider_UI"),
conditionalPanel("input.fixLower == 0", {actionButton("fixLower", "Fix lower range")})
)
server <- function(input, output, session) {
fixedLower <- reactiveVal()
observeEvent(input$fixLower, {
fixedLower(min(isolate({input$years})))
}, once = TRUE)
output$Slider_UI <- renderUI({
if(is.null(fixedLower())){
sliderInput(inputId = "years",
label="Choose base year and target year",
min = 2015,
max = 2060,
value = c(2015, 2060),
step = 1,
sep = ""
)
} else {
sliderInput(inputId = "years",
label="Choose base year and target year",
min = fixedLower(),
max = 2060,
value = fixedLower(),
step = 1,
sep = ""
)
}
})
}
shinyApp(ui = ui, server = server)
如@DSGym所建议,以下是使用renderUI
的示例:
library(shiny)
ui <- fluidPage(
radioButtons(inputId="type_select", label="Select slider type", choices = list("Standard slider", "Range slider")),
uiOutput("Slider_UI")
)
server <- function(input, output, session) {
output$Slider_UI <- renderUI({
req(input$type_select)
if(input$type_select == "Range slider"){
presetValues <- c(2015, 2060)
} else {
presetValues <- 2060
}
sliderInput(inputId = "years",
label="Choose base year and target year",
min = 2015,
max = 2060,
value = presetValues,
step = 1,
sep = ""
)
})
}
shinyApp(ui = ui, server = server)