如何动态地将sliderInput添加到闪亮的应用程序?

时间:2016-06-22 20:18:04

标签: r shiny

使用闪亮,我上传了一个csv文件,并根据列名,我需要添加滑块到ui。

                        sidebarPanel(
                                  fileInput('file1', 'Upload CSV File to Create a Model',
                                            accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
                                  tags$hr(),
                                  checkboxInput('header', 'Header', TRUE),
                                  fluidRow(
                                    column(6,checkboxGroupInput("xaxisGrp","X-Axis:", c("1"="1","2"="2"))),
                                    column(6,radioButtons("yaxisGrp","Y-axis:", c("1"="1","2"="2")))
                                  ),
                                  radioButtons('sep', 'Separator',
                                               c(Comma=',', Semicolon=';',Tab='\t'), ','),
                                  radioButtons('quote', 'Quote',
                                               c(None='','Double Quote'='"','Single Quote'="'"),'"'),
                                  uiOutput("choose_columns")
                                )

如您所见,有xaxisGrp和yaxisGrp。 xasixGrp在我上传的csv文件中有列名。

我需要在xasisGrp中动态添加尽可能多的sliderInput减去yaxisGrp中的列名。例如,让我们说

xasisGrp has "Heap", "CPU", "Volume",

我需要能够像这样生成3个sliderInput:

sliderInput("Heap", "Heap Growth %",min=0, max=100, value=0,post="%"),
sliderInput("CPU", "CPU Growth %", min=0, max=100, value=0,post="%"),
sliderInput("Volume", "Volume Growth%", min=0, max=100, value=0,post="%"),

我有什么想法可以做到这一点?

1 个答案:

答案 0 :(得分:2)

这是一个简单的例子。

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)