我不确定这是否可以使用闪亮的标准shiny::sliderInput()
(javascript解决方案也欢迎)。我有一系列离散值,我想在滑块小部件中使用,但这个范围有"间歇性"步骤进行:
> # MWE
>
> # step sequence is c(2, 4, 4)
> steps <- rep(c(2, 4, 4), 5, each = 1)
>
> steps[1:3]
[1] 2 4 4
>
> # this is the sequence
> years <- Reduce(`+`, var.freq, init = 2010, accumulate = T)
>
> years
[1] 2010 2012 2016 2020 2022 2026 2030 2032 2036 2040 2042 2046 2050 2052 2056 2060
>
> # minimum and maximum values for slider
> minmax <- sapply(c(min, max), function(x) x(years))
>
> minmax
[1] 2010 2060
我想要的是一个滑块,让用户只选择years
中指定的值。我知道我可以转换滑块中选择的对象以符合years
中的值,但我对滑块的美感更感兴趣,我想避免让用户感到困惑。 step
中的shiny::sliderInput()
参数似乎只占用steps
对象中的第一个元素:
# define UI
ui <- function(){
fluidPage(
titlePanel("Varying steps..."), # define title of panel
sidebarLayout( # create layout for shiny app
sidebarPanel(
sliderInput(inputId = "range",
label = h4(HTML("<b>Year interval:</b>")),
min = minmax[[1]], max = minmax[[2]],
value = minmax,
ticks = T,
step = steps,
sep = "")
),
mainPanel(
verbatimTextOutput("result")
)
)
)
}
# server side
server <- function(input, output){
output$result <- renderPrint({
input$range
})
}
runApp(list(ui = ui, server = server))
提前致谢...
答案 0 :(得分:2)
您可以尝试shinyWidgets
中的sliderTextInput
功能,它允许您使用带有自定义值的滑块进行选择:
library(shiny)
library(shinyWidgets)
years <- c(2010 ,2012, 2016, 2020, 2022, 2026,
2030, 2032, 2036, 2040, 2042, 2046,
2050, 2052, 2056, 2060)
ui <- fluidPage(
sliderTextInput(
inputId = "range", label = h4(tags$b("Year interval:")),
choices = years, selected = range(years),
grid = TRUE
),
verbatimTextOutput(outputId = "res")
)
server <- function(input, output, session) {
output$res <- renderPrint(input$range)
}
shinyApp(ui, server)