簡體   English   中英

你如何動態地將sliderInput 添加到你閃亮的應用程序中?

[英]how do you dynamically add sliderInput to your shiny application?

使用閃亮,我正在上傳一個 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 中減去 yaxisGrp 中的列名動態添加盡可能多的滑塊輸入。 例如,讓我們說

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

我需要能夠像這樣生成 3 個滑塊輸入:

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="%"),

任何想法我怎么能做這個Shiny?

這是一個快速示例。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM