使用sidebarPanel附加和删除选项卡 [英] Append and remove tabs using sidebarPanel
本文介绍了使用sidebarPanel附加和删除选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在删除Shiny中新创建的标签时遇到麻烦.
Having trouble deleting tabs that have been newly created in Shiny.
场景:
- 添加新标签"A"
- 添加新标签"B"
- 单击选项卡B上的删除-不删除
- 选择标签A,点击删除即可生效
我觉得它与id有关,而被逻辑所困扰.
I feel its something to do with ids, stumped with the logic.
谢谢.
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
navbarPage(title = "Test", id="tabs",
tabPanel("Home",
sidebarPanel(
selectInput("testlist", "Select test:",
list("A", "B", "C")),
actionButton("append", "New tab")),
mainPanel()
)
)
)
server <- function(input, output, session) {
tabnamesinput<-reactive({
input$testlist
})
observeEvent(input$append,{
id<-paste0(tabnamesinput())
appendTab(inputId = "tabs",
tabPanel(id,
sidebarPanel(
actionButton("remove", "Delete")
)
)
)
})
observeEvent(input$remove,{
removeTab(inputId = "tabs", target = input$tabs)
})
}
shinyApp(ui, server)
推荐答案
使用上述方法,您试图将相同的 id ="remove"
分配给每个删除按钮.这行不通.每个按钮都需要自己的 id
.
With your above approach you are trying to assign the same id = "remove"
to each delete-button. This won't work. Every button needs it's own id
.
每个按钮具有唯一的ID后,您需要一个观察者来监听由这些按钮触发的所有事件.以下内容查找与模式"^^ remove _"
匹配的所有输入:
Once each button has it's unique id you need an observer listening to all events triggered by those buttons. The following looks for all inputs matching the pattern "^remove_"
:
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
navbarPage(title = "Test", id = "tabs",
tabPanel("Home",
sidebarPanel(
selectInput("testlist", "Select test:", list("A", "B", "C"), selected = "A"),
actionButton("append", "New tab")),
mainPanel()
)
)
)
server <- function(input, output, session) {
observeEvent(input$append,{
appendTab(inputId = "tabs",
tabPanel(input$testlist,
sidebarPanel(
actionButton(paste0("remove_", input$testlist), "Delete")
)
)
)
})
observeEvent(lapply(grep(pattern = "^remove_", x = names(input), value = TRUE), function(x){input[[x]]}),{
if(input$tabs != "Home"){
removeTab(inputId = "tabs", target = input$tabs)
updateSelectInput(session, "testlist", selected = input$testlist) # keep the selection when re-rendering sidebarPanel
}
})
}
shinyApp(ui, server)
这篇关于使用sidebarPanel附加和删除选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文