R Shiny 如何实现 tabItem 仅在用户点击时渲染而非预加载
Shinydashboard 实现Tab按需加载的方法
原有实现的问题点
当前开发的Shinydashboard应用存在全量预渲染的特性:所有侧边栏导航的Tab项,无论用户是否手动点击选择,都会在应用启动时被提前渲染完成。之前尝试结合observeEvent编写按钮触发逻辑实现按需加载,但始终没有跑通可行的实现路径。
应用的界面参考如下:
原有代码中实现不了懒加载的核心原因有两个:
sidebarMenu没有设置唯一id,否则server端根本无法捕获到用户点击Tab的切换事件,之前写的observeEvent(input$tabs)逻辑因为没有匹配到正确的输入值,完全不会触发- 所有Tab的内容都是静态写死在UI初始化代码里的,Shiny在生成初始页面时就会把这些内容全部渲染到DOM里,自然做不到按需加载
具体修改步骤
- 第一步:调整侧边栏的
sidebarMenu配置,给菜单加上id
把原来的侧边栏代码里的sidebarMenu(...)部分修改为:dashboardSidebar( sidebarMenu( id = "sidebar_selector", # 新增id,后续server端可通过input$sidebar_selector获取当前选中的tabName menuItem("Main menu", tabName = "main_menu", icon = icon("home")), menuItem("Peripherals", tabName = "peripherals", icon = icon("hdd")), menuItem("Database repair", tabName = "widgets", icon = icon("th")), menuItem("Polling", tabName = "polling", icon = icon("cloud")), menuItem("more issues!!", tabName = "issues", icon = icon("ad")) ) ) - 第二步:把原来静态写在
dashboardBody里的所有tabItem内容全部移除,替换成一个动态输出的占位节点,不再在UI初始化阶段加载任何Tab内容
修改后的dashboardBody结构为:dashboardBody( tags$head( tags$link(rel = "stylesheet", type = "text/css", href = "Custom.css") ), # 仅留一个动态输出占位,替代原来所有静态tabItem的位置 uiOutput("rendered_tab") ) - 第三步:在server端通过
renderUI根据用户当前选中的Tab值,动态返回对应Tab的UI内容
对应的server逻辑代码:server <- function(input, output) { output$rendered_tab <- renderUI({ switch(input$sidebar_selector, # 每个分支对应一个tabName,返回原来对应的静态tabItem内容即可 "main_menu" = tabItem(tabName = "main_menu", h1("Main menu", style = "color:#15942B"), strong("Here we can add the news of the day or a welcome image"),br(), br(), br(), strong("This is a work in progress, to be presented to our team leads so we can make it an aid page for all of us") ), "peripherals" = tabItem(tabName = "peripherals", h1("Peripherals", style = "color:#15942B"), # 把原来写在peripherals tab里的所有嵌套tabsetPanel、tabPanel内容原封不动迁移到这里 tabsetPanel(type = "tabs", tabPanel("Printers",br(), tabsetPanel(type = "tabs", tabPanel("M452DW",br(), tabsetPanel(type = "tabs", tabPanel("Not pinging"), tabPanel("Error messages") ) ), tabPanel("M402DW",br(), tabsetPanel(type = "tabs", tabPanel("Not pinging") ) ) ) ), tabPanel("Pinpad",br(), tabsetPanel(type = "tabs", tabPanel("Offline / busy"), tabPanel("Not turning on") ) ), tabPanel("Scanners",br(), tabsetPanel(type = "tabs", tabPanel("GBT4400"), tabPanel("DS2278") ) ), tabPanel("Receipt printer / cashdrawer",br(), tabsetPanel(type = "tabs", tabPanel("Receipt printer"), tabPanel("Cash drawer") ) ), tabPanel("Label printer",br(), tabsetPanel(type ="tabs", tabPanel("ZD410"), tabPanel("LP2824 & +") ) ) ) ), "issues" = tabItem(tabName = "issues", h1("More issue resolutions to come!!!!!!", style = "color:#15942B" ), strong("My plan is to add the hardest issue resolutions for our team, so they can access this web page and with a glipse they can resolve the issue in hand") ), "polling" = tabItem(tabName = "polling"), "widgets" = tabItem(tabName = "widgets") ) }) }
实现效果
- 应用启动时仅渲染默认选中的首个Tab(
main_menu)对应的内容,其余Tab的UI代码完全不会被执行,不会产生预渲染的开销 - 用户每次点击侧边栏切换Tab时,才会触发对应Tab的内容渲染逻辑,从未被点击过的Tab不会被加载
- 整套实现完全复用Shiny原生的响应式渲染机制,不会改变原有Tab的交互样式、切换体验,不需要额外添加按钮、弹窗这类额外交互控件
内容的提问来源于stack exchange,提问作者Raúl Gustavo Blé Rosique
相关产品推荐
相关产品推荐

