基本上,您可以尝试将所有动态生成的组件打包到一个函数中,然后在其中调用此函数 renderUI 。请看下面的玩具示例:
renderUI
library(shiny) server <- function(input, output, session) { panel1Data <- reactive({ panelData(1) }) createPanel <- function(id) { headerID <- paste0("header_", id) defID <- paste0("def_", id) plotID <- paste0("plot_", id) res <- list( h1(headerID), br(), h3(defID), br(), h3(paste0(plotID), " goes here.") ) return(res) } output$UI <- renderUI({ createPanel(input$selection) }) } ui <- basicPage( h3("Using renderUI and uiOutput"), selectInput("selection", label="", choices=c("a", "b", "c")), uiOutput("UI") ) shinyApp(ui = ui, server = server)
这是一个非常好的帖子,显示常见的闪亮技巧: 创建可重用的UI元素