Set sliderInput values as characters in shiny

前端 未结 2 1577
别那么骄傲
别那么骄傲 2021-01-18 18:29

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

2条回答
  •  深忆病人
    2021-01-18 19:03

    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('
                            
                            ', sel_values, 
                            length(values[[input$selection]]) - 1,
                            length(values[[input$selection]]) - 1)))
            )}
        )}
    )
    
    shinyApp(ui = ui, server = server)
    

提交回复
热议问题