R Shiny : name tabPanel with the number of clicks

标签 r shiny

我正在创建一个应用程序,用户可以通过单击专用的 tabPanel 来创建新的 tabPanel。我知道该怎么做,但我不知道如何命名带有反应数字的 tabPanel。

为了更清楚起见,这里有一个可重现的示例:

library(shiny)
library(shinyWidgets)

ui <- navbarPage(position = "static-top",
                 title = "foo",
                 id = "tabs",
                 tabPanel(title = "Name 1",
                          fluidRow()),
                 tabPanel(title = "More",
                          icon = icon("plus"),
                          fluidRow()
                          )
                 )


server <- function(input, output) {

  count <- reactive({
    i <- 1
      if (input$tabs == "More"){
        i <- i + 1
      }
      else {i <- i}
    i
  })

  observeEvent(input$tabs, {
    id = paste0("Name ", count())
    if (input$tabs == "More")
    appendTab(inputId = "tabs",
              tabPanel(title = id,
                       fluidRow(column(
                         width = 12))
              ),
              select = TRUE)
  })

}


shinyApp(ui = ui, server = server)

如您所见,单击 tabPanel 更多 将创建一个新的 tabPanel。我希望 tabPanel 的名称采用“Name i”形式,其中“i”是现有 tabPanel 的数量(- 1 因为我不想考虑 tabPanel More 和+ 1 以防止开头出现两次相同的数字)。因此,我需要这个“i”具有反应性,因为它必须考虑用户创建的 tabPanel。

在我的代码中,您可以看到我尝试通过计算 tabPanel 更多 上的点击次数来计算 tabPanel 的数量,但这不起作用。我看到this post但我不知道如何修改它,因为我从未使用过 JavaScript。

有人有解决办法吗?

最佳答案

您可以将 count() 设为 reactiveVal 来解决此问题:

此外,您可能需要使用 insertTab 而不是 appendTab 来将“更多”选项卡保留在右侧:

library(shiny)
library(shinyWidgets)

ui <- navbarPage(position = "static-top",
                 title = "foo",
                 id = "tabs",
                 tabPanel(title = "Name 1",
                          fluidRow()),
                 tabPanel(title = "More",
                          icon = icon("plus"),
                          fluidRow()
                 )
)


server <- function(input, output) {

  count <- reactiveVal(1)

  observeEvent(input$tabs, {
    if (input$tabs == "More"){
      count(count()+1)
      id = paste0("Name ", count())
      insertTab(inputId = "tabs",
                tabPanel(title = id,
                         fluidRow(column(
                           width = 12))
                ), target = "More", position = "before",
                select = TRUE)}
  })

}


shinyApp(ui = ui, server = server)

关于R Shiny : name tabPanel with the number of clicks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58503472/

相关文章:

R Shiny : Copying Cells from Excel into SelectizeInput

从长到宽 reshape 并创建具有二进制值的列

r - 根据值填充曲线下面积

R:等温线作为等值线使用 ggplot2

css - R & Shiny - 边栏文本对齐对齐

r - Shiny - 动态地向 rmarkdown 报告添加部分

r - 如何将 RSelenium ALT+S 中的同时键发送到网络驱动程序?

r - R 中的 SVM 特征选择

R Shiny 加速数据加载

renderMenu 导致奇怪的格式,R Shiny Dashboard