将自定义数量的 tabPanel 添加到闪亮仪表板中的 tabsetPanel

Posted

技术标签:

【中文标题】将自定义数量的 tabPanel 添加到闪亮仪表板中的 tabsetPanel【英文标题】:Adding custom number of tabPanels to a tabsetPanel in shiny dashboard 【发布时间】:2021-09-12 02:26:55 【问题描述】:

我正在尝试根据变量form 制作一个带有多个tabPanels 的tabsetPanel。我已经尝试了下面的代码,但出现了一些错误:

attr(x, "selected") 中的错误

有人可以帮帮我吗?

library(shinydashboard)
library(tidyverse)

form <- 2

ui <- dashboardPage(
    title = "Rolê de Aventura", skin="blue",
    dashboardHeader(titleWidth = 1024,
                    title=list(title=tags$img(src="LogoPQ.png",
                                              heigth=45, width=45,
                                              align="left"),
                               title=tags$p(style="text-align:center;",
                                            "Rolê de Aventura")
                    )
    ),
    dashboardSidebar(
        selectInput("categoria", label = "Categoria:",
                    choices = list("Quarteto Misto",
                                   "Dupla Masculina",
                                   "Dupla Mista",
                                   "Dupla Feminina"), 
        )
    ),
    dashboardBody(
        textInput("equipe", "Nome da equipe:", width = NULL),
        tabsetPanel(width = NULL, type = "tabs",
                    do.call(tabsetPanel,c(width=NULL, type="tabs",
                                           lapply(1:form, function(i) 
                                               tabPanel(title = str_c("Atleta", i),
                                                        textInput(str_c("atleta", i), "Nome:", width=NULL),
                                                        dateInput(str_c("at.nasc", i), "Nascimento:", ),
                                                        checkboxGroupInput(str_c("at.sex", i), "Sexo:", ,
                                                                           choices=list("Masculino", "Feminino")))
                                           )
                    )
                    )
        )
    )
)

server <- function(input, output) 


shinyApp(ui = ui, server = server)

【问题讨论】:

【参考方案1】:

我似乎无法准确识别您的错误。但是,有一个答案here。

我已经相应地修改了您的代码,并且它按预期工作。见下文,

library(shinydashboard)
library(tidyverse)
library(shiny)

form <- 2

ui <- dashboardPage(
        title = "Rolê de Aventura", skin="blue",
        dashboardHeader(titleWidth = 1024,
                        title=list(title=tags$img(src="LogoPQ.png",
                                                  heigth=45, width=45,
                                                  align="left"),
                                   title=tags$p(style="text-align:center;",
                                                "Rolê de Aventura")
                        )
        ),
        dashboardSidebar(
                selectInput("categoria", label = "Categoria:",
                            choices = list("Quarteto Misto",
                                           "Dupla Masculina",
                                           "Dupla Mista",
                                           "Dupla Feminina"), 
                )
        ),
        dashboardBody(
                textInput("equipe", "Nome da equipe:", width = NULL),
                do.call(
                        tabsetPanel,
                        c(
                                id = "t",
                                lapply(
                                        1:form,
                                        FUN = function(x) 
                                                tabPanel(
                                                        title = paste("tab", x)
                                                )
                                                
                                        
                                )
                        )
                )
        )
)

server <- function(input, output) 


shinyApp(ui = ui, server = server)

【讨论】:

效果很好。谢谢你。我将研究代码以找出我做错了什么以及您做错了什么。【参考方案2】:

我发现在这种情况下使用 exec() 函数和 !!! 很有用。

library(shinydashboard)
library(tidyverse)
library(shiny)

form <- 2

#store the ui code in a list to use later.

tabs <- map(1:form, function(i) 
          tabPanel(title = str_c("Atleta", i),
                   textInput(str_c("atleta", i), "Nome:"),
                   dateInput(str_c("at.nasc", i), "Nascimento:", ),
                   checkboxGroupInput(str_c("at.sex", i), "Sexo:", ,
                                      choices=c("Masculino", "Feminino")))
        )

ui <- dashboardPage(
  title = "Rolê de Aventura", skin="blue",
  dashboardHeader(titleWidth = 1024,
                  title=list(title=tags$img(src="LogoPQ.png",
                                            heigth=45, width=45,
                                            align="left"),
                             title=tags$p(style="text-align:center;",
                                          "Rolê de Aventura")
                  )
  ),
  dashboardSidebar(
    selectInput("categoria", label = "Categoria:",
                choices = list("Quarteto Misto",
                               "Dupla Masculina",
                               "Dupla Mista",
                               "Dupla Feminina"), 
    )
  ),
  dashboardBody(
    textInput("equipe", "Nome da equipe:"),
    exec(tabsetPanel, !!!tabs))
    )


server <- function(input, output) 


shinyApp(ui = ui, server = server)

【讨论】:

哇!这是一个非常聪明的解决方案。我不得不搜索以了解 !!! 运算符。谢谢!!!

以上是关于将自定义数量的 tabPanel 添加到闪亮仪表板中的 tabsetPanel的主要内容,如果未能解决你的问题,请参考以下文章

php DashboardWidgeteinfügen - 将自定义仪表板小部件添加到wordpress

R Shiny - 将tabPanel动态添加到tabsetPanel(使用renderUI)

在后台加载闪亮的 tabPanel

R闪亮仪表板:如何将垂直滚动条添加到仪表板侧栏?

根据 tabPanel 选择从数据库获取数据。闪亮

将垂直滚动条添加到 shinyjs 下拉按钮 - 闪亮