Set sliderInput values as characters in shiny

别等时光非礼了梦想. 提交于 2019-12-01 18:37:41

问题


My shiny app has a sliderInput, but want to replace values as character labels. How could I implement it? Thanks for any suggestions.

This is my example code:

library(shiny)
values <- as.factor(c('Label 1', 'Label 3', 'Label 3'))
ui <- shinyUI(bootstrapPage(
    headerPanel("test"),
    sliderInput("foo", "Animation duration", 
                min = 1,
                max = length(values), 
                value = values)
))

server <- shinyServer(function(input, output, session) {
})

shinyApp(ui = ui, server = server)

回答1:


Thanks @daattli for pointing me the right direction and letting me know how to use js to change the shiny element.

I have implemented a solution to change labels of sliderInput and a selectInput to switch different values (and length). I think this feature should be implemented into shiny which uses ionRangeSlider.

Please improve my codes if you think there is a better way to implement it, as it is my first js script.

library(shiny)
values <- list(A = c('A1', 'A2', 'A3'),
               B = c('B1', 'B2', 'B3', 'B4'))

ui <- shinyUI(bootstrapPage(
    selectInput('selection', 'selection', c('A',  'B'), 'A'),
    uiOutput('selectUI'),
    sliderInput(inputId = "target", label = "Target",
                min = 0, max = length(values$A) - 1,
                step = 1,
                value = length(values$A) - 1),
    verbatimTextOutput('summary')
))

server <- shinyServer(function(input, output, session) {
    output$summary <- renderPrint({
        print(input$target)
        print(values[[input$selection]][input$target + 1])
    })
    output$selectUI <- renderUI({

        sel_values <- paste(paste0('"', values[[input$selection]], '"'), collapse = ',')
        print(sel_values)
        list(
            (HTML(
                sprintf('
                        <script type="text/javascript">
                        $(document).ready(function() {
                        var vals = [%s];
                        $(\'#target\').data(\'ionRangeSlider\').update(
                        {values:vals,
                        min: 0,
                        max: %s,
                        from:%s})
                        })
                        </script>
                        ', sel_values, 
                        length(values[[input$selection]]) - 1,
                        length(values[[input$selection]]) - 1)))
        )}
    )}
)

shinyApp(ui = ui, server = server)



回答2:


This can be easily done using sliderTextInput function in shiny. No need to add all this complex js function. Just a few lines of code will do the trick.Install the shinywidgets package which contains the sliderTextInput function. Do the following :

  sliderTextInput("foo","Animation Duration" , 
                  choices = c("Label 1", "Label 3", "Label 3"), 
                  selected = c("Label 1", "Label 3", "Label 3"), #incase you want all values by default 
                  animate = FALSE, grid = FALSE, 
                  hide_min_max = FALSE, from_fixed = FALSE,
                  to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
                  to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
                  post = NULL, dragRange = TRUE)


来源:https://stackoverflow.com/questions/35121917/set-sliderinput-values-as-characters-in-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!