[英]function hideTab() not working in shinydashboard for individual dynamic tabs
I am creating dynamic tabs based on clicks on a datatable with an action button on each newly created tab which when clicked, hides this tab. 我正在基于对数据表的单击来创建动态选项卡,每个新创建的选项卡上都带有一个操作按钮,单击该按钮时,该选项卡将隐藏该选项卡。 The new tabs are getting created as expected, but the hide action button does not work when I open more than one tab.
新标签页已按预期创建,但是当我打开多个标签页时,隐藏操作按钮不起作用。 Is there something I am missing?
我有什么想念的吗?
library(shiny)
library(DT)
library(shinydashboard)
ui <- function(request) {
dashboardPage(
dashboardHeader(title = "Tabs not Hiding"),
dashboardSidebar(disable = TRUE),
dashboardBody(
tabBox(id = "tabs",
width = 12,
tabPanel("Cars overview",
h1("Cars overview"),
div("Click any cell"),
br(),
DT::dataTableOutput("mtcars")
)
)
)
)
}
server <- function(input, output, session) {
tab_list <- NULL
# Generate data table
output$mtcars <- DT::renderDataTable({
DT::datatable(mtcars)
})
observeEvent(input$mtcars_cell_clicked, {
info <- as.numeric(input$mtcars_cell_clicked$row)
outputID <- glue::glue("dt-{info}")
req(info)
if(!(info %in% tab_list)){
print(info)
appendTab(inputId = "tabs",
tabPanel(title = outputID,
fluidRow(
box(
actionButton("TabHide", "Hide this tab"),
width = 3
),
box(
DT::dataTableOutput(outputID),
width = 9
)
)
)
)
tab_list <<- c(tab_list, outputID)
}
output[[outputID]] <- DT::renderDataTable({
mtcars[info, ]
})
showTab(inputId = "tabs", target = outputID, select = TRUE)
observeEvent(input$TabHide,{
hideTab(input = "tabs", target = outputID)
}, ignoreInit = TRUE)
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
Please check this version of server function, I add comments to changes what I did: 请检查此版本的服务器功能,我添加了注释以更改所做的工作:
server <- function(input, output, session) {
tab_list <- NULL
# Generate data table
output$mtcars <- DT::renderDataTable({
DT::datatable(mtcars)
})
# Add proxy object to manipulate DT
dtProxy <- dataTableProxy('mtcars')
observeEvent(input$mtcars_rows_selected, {
info <- as.numeric(input$mtcars_row_last_clicked)
# Clear DT selection via proxy
selectRows(dtProxy, NULL)
outputID <- glue::glue("dt-{info}")
req(info)
# Check for 'outputID' in tab_list instead of 'info' variable (missprint?)
if(!(outputID %in% tab_list)){
appendTab(inputId = "tabs",
tabPanel(title = outputID,
fluidRow(
box(
# Create buttons with unique inputId
actionButton(paste0("TabHide", outputID), "Hide this tab"),
width = 3
),
box(
DT::dataTableOutput(outputID),
width = 9
)
)
)
)
tab_list <<- c(tab_list, outputID)
}
output[[outputID]] <- DT::renderDataTable({
mtcars[info, ]
})
showTab(inputId = "tabs", target = outputID, select = TRUE)
# Add observer for actionButton from opened tab (part 'input[["some_inputId"]]')
observeEvent(input[[paste0("TabHide", outputID)]],{
# Use 'removeTab' instead of 'hideTab', be cause of tab duplicates
removeTab(input = "tabs", target = outputID)
# Remove tab from 'tab_list', as we know this observer will delete tab from page
tab_list <<- tab_list[!tab_list %in% outputID]
# Add 'once = TRUE' to destroy observer after tab closed (we will create new observer again when open tab)
}, ignoreInit = TRUE, once = TRUE)
}, ignoreInit = TRUE)
}
Overall : just attention to details. 总体而言 :只关注细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.