在Shiny中,我使用renderUI
和tagList
在服务器元素中动态创建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)
答案 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)