[英]function hideTab() not working in shinydashboard for individual dynamic tabs
我正在基於對數據表的單擊來創建動態選項卡,每個新創建的選項卡上都帶有一個操作按鈕,單擊該按鈕時,該選項卡將隱藏該選項卡。 新標簽頁已按預期創建,但是當我打開多個標簽頁時,隱藏操作按鈕不起作用。 我有什么想念的嗎?
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)
請檢查此版本的服務器功能,我添加了注釋以更改所做的工作:
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)
}
總體而言 :只關注細節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.