uiOutput in Shiny中的列

时间:2016-08-22 15:28:07

标签: r shiny

在Shiny中,我使用renderUItagList在服务器元素中动态创建UI,然后使用uiOutput显示它。它有效,但我想将uiOutput的内容分解为列。我该怎么做?

这里是Shiny gallery的(略微缩写的)动态UI示例。请注意output$ui如何包含最终显示为一列的tagList,但我希望它显示为两列。

请注意,我无法在column内拨打renderUI

library(shiny)

server <- shinyServer(function(input, output) {
  output$ui <- renderUI({
    if (is.null(input$input_type)) {return()}
    tagList(list(div("Want this to be left column"),
         switch(input$input_type,
           "slider" = sliderInput("dynamic", "Want this to be right column",
                                  min = 1, max = 20, value = 10),
           "text" = textInput("dynamic", "Want this to be right column",
                              value = "starting value"),
           "numeric" =  numericInput("dynamic", "Want this to be right column",
                                     value = 12),
           "checkbox" = checkboxInput("dynamic", "Want this to be right column",
                                      value = TRUE))))
  })
  output$input_type_text <- renderText({input$input_type})
  output$dynamic_value <- renderPrint({str(input$dynamic)})
})

ui <- shinyUI(fluidPage(
  titlePanel("Dynamically generated user interface components"),
  wellPanel(selectInput("input_type", "Input type",
                        c("slider", "text", "numeric", "checkbox")
  )),
  wellPanel(uiOutput("ui")),
  wellPanel(
    tags$p("Input type:"),
    verbatimTextOutput("input_type_text"),
    tags$p("Dynamic input value:"),
    verbatimTextOutput("dynamic_value")
  )
))

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

而不是将div和选定的小部件包装到list然后再装入tagList(您不需要它们 - 其中一个就足够了)

tagList(list(div("Want this to be left column"), switch(...) ...)

创建fluidRow并添加两列 - 一列包含div,另一列包含switch

fluidRow(column(6, div("Want this to be left column")),
         column(6, switch(input$input_type,
                        "slider" = sliderInput("dynamic", "Want this to be right column",
                                               min = 1, max = 20, value = 10),
                        "text" = textInput("dynamic", "Want this to be right column",
                                           value = "starting value"),
                        "numeric" =  numericInput("dynamic", "Want this to be right column",
                                                  value = 12),
                        "checkbox" = checkboxInput("dynamic", "Want this to be right column",
                                                   value = TRUE)
                  )
          )
        )

完整示例:

library(shiny)

server <- shinyServer(function(input, output) {
  output$ui <- renderUI({
    if (is.null(input$input_type)) {return()}
    fluidRow(column(6, div("Want this to be left column")),
             column(6, switch(input$input_type,
                              "slider" = sliderInput("dynamic", "Want this to be right column",
                                                     min = 1, max = 20, value = 10),
                              "text" = textInput("dynamic", "Want this to be right column",
                                                 value = "starting value"),
                              "numeric" =  numericInput("dynamic", "Want this to be right column",
                                                        value = 12),
                              "checkbox" = checkboxInput("dynamic", "Want this to be right column",
                                                         value = TRUE)
             )
             )
    )
  })
  output$input_type_text <- renderText({input$input_type})
  output$dynamic_value <- renderPrint({str(input$dynamic)})
})

ui <- shinyUI(fluidPage(
  titlePanel("Dynamically generated user interface components"),
  wellPanel(selectInput("input_type", "Input type",
                        c("slider", "text", "numeric", "checkbox")
  )),
  wellPanel(uiOutput("ui")),
  wellPanel(
    tags$p("Input type:"),
    verbatimTextOutput("input_type_text"),
    tags$p("Dynamic input value:"),
    verbatimTextOutput("dynamic_value")
  )
))

shinyApp(ui = ui, server = server)