【问题标题】:Get list of elements in shinydashboard body获取闪亮仪表板正文中的元素列表
【发布时间】:2021-10-21 04:45:54
【问题描述】:

我有一个闪亮的应用程序,它使用shinydashboard 侧边栏作为导航。每个命名选项卡都有自己的tabSetPanel。因为每个tabSetPanel 可以有不同数量的选项卡(随着时间的推移,我希望可以添加更多选项卡并且整个应用程序仍然可以运行),我想以编程方式获取选项卡的名称.

有没有办法获得body 对象的选项卡元素的列表输出?例如

tabs

tabs$plots
"iris"

tabs$plots
"mtcars"

代表:

library(shiny)
library(shinydashboard)
library(magrittr)
library(ggplot2)


body <- dashboardBody(
  tabItem(
    "plots",
    tabsetPanel(
      tabPanel(
        "iris",
        tags$h1("iris"),
        sidebarLayout(
          sidebarPanel(
            selectInput("x_var", "Select X variable: ", choices = names(iris)),
            selectInput("y_var", "Select Y variable: ", choices = names(iris))
          ),
          mainPanel(
            plotOutput("iris_plot"),
            textOutput("ui_elements")
          )
        )
      ),
      tabPanel(
        "mtcars",
        tags$h1("mtcars"),
        sidebarLayout(
          sidebarPanel(
            selectInput("x_var_cars", "Select X variable: ", choices = names(mtcars)),
            selectInput("y_var_cars", "Select Y variable: ", choices = names(mtcars))
          ),
          mainPanel(
            plotOutput("mtcars_plot")
          )
        )
      )
    )
  )
)

ui <- dashboardPage(
  dashboardHeader(title = "Test App"),
  dashboardSidebar(
    sidebarMenu(
      id = "sidebar_menu",
      menuItem(
        "plots", tabName = "plots"
      )
    )
  ),
  body
)

server <- function(input, output) {
  output$iris_plot <- renderPlot({
    iris %>% 
      ggplot() +
      aes(x = .data[[input$x_var]], y = .data[[input$y_var]], colour = Species) +
      geom_point()
  })
  
  output$mtcars_plot <- renderPlot({
    mtcars %>% 
      ggplot() +
      aes(x = .data[[input$x_var_cars]], y = .data[[input$y_var_cars]]) +
      geom_point()
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    我的简单尝试。挖掘body 对象,它是列表列表的嵌套列表......大多数列表的长度为1(我认为它们对应于闪亮应用程序中的div 标签)所以我可以得到标签的名称主要“页面”通过循环浏览页面数。作为参考,我将这些选项卡称为:“子选项卡”。

    限制

    硬编码

    这只有在我知道确切路线(之间有多少子元素)的情况下才有效:

    • 从根级别到页面标签
    • 从页面标签到标签标签

    显然,如果我想扩大规模,我还需要一种程序化方式来获取路线。幸运的是,在这种情况下,我知道这些主要级别之间的标签数量不会改变。

    速度

    这也很慢。我不确定这是否源于我使用循环。我想过取消列出body 对象,只为某些元素(标签)编制索引,但这也很慢。它也无法区分来自tabsetPanel(子标签)的标签和来自tabBox(孙标签)的标签。

    # set 'root' to pages
    page_layer <- body$children[[1]]$children
    
    # get number of pages
    n_pages <- length(page_layer)
    
    child_tabs <- list()
    
    # for each page...
    for (i in 1:n_pages) {
      # get the name of the page
      page <- page_layer[[i]]$attribs$`id`
      # Get the number of child tabs. If the page has no child tabs, indexing will
      # throw error so it needs to be caught.
      child_tabs[[page]] <- tryCatch(
        length(page_layer[[i]]$children[[1]]$children[[1]]$children[[1]]),
        error = function(e) 0
        )
      
      # if there are any child tabs...
      if (child_tabs[[i]] > 0) {
        # get the name of each 
        for (j in 1:child_tabs[[i]]) {
        child_tabs[[i]][j] <- page_layer[[i]]$children[[1]]$children[[1]]$children[[1]][[j]]$children[[1]]$attribs$`data-value`
        }
      }
    }
    
    child_tabs
    

    输出

    $`shiny-tab-plots`
    [1] "iris"   "mtcars"
    

    【讨论】:

      猜你喜欢
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 2018-08-05
      • 2018-04-28
      • 2019-06-20
      • 2016-05-02
      • 1970-01-01
      相关资源
      最近更新 更多