how do you dynamically add sliderInput to your shiny application?

前端 未结 1 1351
再見小時候
再見小時候 2021-01-29 00:37

using shiny, I am uploading a csv file and based on column names, I need to add slider to the ui.

                        sidebarPanel(
                                  


        
相关标签:
1条回答
  • 2021-01-29 01:28

    Here is a quick example.

    library(shiny)
    
    xAxisGroup <- c("Heap", "CPU", "Volume")
    
    ui <- shinyUI(fluidPage(
    
       titlePanel("Dynamic sliders"),
    
       sidebarLayout(
          sidebarPanel(
              # Create a uiOutput to hold the sliders
            uiOutput("sliders")
          ),
    
          mainPanel(
             plotOutput("distPlot")
          )
       )
    ))
    
    server <- shinyServer(function(input, output) {
    
        #Render the sliders
        output$sliders <- renderUI({
            # First, create a list of sliders each with a different name
            sliders <- lapply(1:length(xAxisGroup), function(i) {
                inputName <- xAxisGroup[i]
                sliderInput(inputName, inputName, min=0, max=100, value=0, post="%")
            })
            # Create a tagList of sliders (this is important)
            do.call(tagList, sliders)
        })
    
    
       output$distPlot <- renderPlot({
          hist(rnorm(100), col = 'darkgray', border = 'white')
       })
    })
    
    shinyApp(ui = ui, server = server)
    
    0 讨论(0)
提交回复
热议问题