shinyWidgets中noUiSliderInput()的range与pips参数如何使用?
解决shinyWidgets中noUiSliderInput非线性滑块空白问题
我在用shinyWidgets::noUiSliderInput()实现非线性滑块时遇到了问题:官方文档只说明range和pips参数需要传入列表,但没给出具体的结构示例。我参照noUiSlider的非线性滑块配置方式写了range参数,但配置后UI直接空白,也没有任何报错,注释掉range参数后滑块就能正常显示。
我的测试代码如下:
library(shiny) library(shinyWidgets) if(interactive()) { ui <- fluidPage( tags$br(), noUiSliderInput( inputId = "noui2", label = "Slider vertical:", min = 20, max = 400000, #step = 50, value = c(100), orientation = "vertical", range = list("min" = 0, "x20" = c(20, 5), "x100" = c(100, 20), "x250" = c(250, 50), "x500" = c(500, 100), "x5000" = c(5000, 1000), "x20000" = c(20000, 20000), "max" = 400000), width = "100px", height = "300px" ), verbatimTextOutput(outputId = "res2") ) server <- function(input, output, session) { output$res2 <- renderPrint(input$noui2) } shinyApp(ui, server) }
问题原因与解决方法
问题出在shinyWidgets对range参数的格式要求和原生noUiSlider不同,需要调整以下几点:
- 不需要用
"x20"这类带前缀的键,直接用数值作为列表的键 - 不要在
range里单独写"min"和"max",直接把范围的首尾数值作为键 - 必须移除原来的
min和max参数(因为range已经定义了滑块的取值范围)
修正后的代码:
library(shiny) library(shinyWidgets) if(interactive()) { ui <- fluidPage( tags$br(), noUiSliderInput( inputId = "noui2", label = "Slider vertical:", # 移除原有的min和max参数 #step = 50, value = c(100), orientation = "vertical", range = list( 0 = 0, 20 = list(20, 5), 100 = list(100, 20), 250 = list(250, 50), 500 = list(500, 100), 5000 = list(5000, 1000), 20000 = list(20000, 20000), 400000 = 400000 ), width = "100px", height = "300px" ), verbatimTextOutput(outputId = "res2") ) server <- function(input, output, session) { output$res2 <- renderPrint(input$noui2) } shinyApp(ui, server) }
内容的提问来源于stack exchange,提问作者jzadra
相关产品推荐
相关产品推荐

