3

我有这个带有 anavbarPage和两个tabPanels 的 Shiny App。在第一个选项卡中,我有一个actionLink初始化modalDialog包括一个reactable单元格作为按钮的一个。当我单击表中的按钮时,我想更改我的输入navbarPage,即tabPanel从“tabone”更改为“tabtwo”。同时我希望在更改modalDialog时关闭tabPanel。我怎么能告诉我modalDialog关闭呢?

library(Shiny)
library(reactable)

ui = fluidPage(
  
  navbarPage(title = "tabs", id = "nav",
             
  tabPanel(title = "tabone",
           actionLink(inputId = "action", "open modalbox")),
  
  tabPanel(title = "tabtwo")
  
))

server = function(input, output, session){
  
  shinyInput = function(FUN, len, id, labels, ...) {
    inputs = character(len)
    for (i in seq_len(len)) {
      inputs[i] = as.character(FUN(paste0(id, i), label = labels[i], ...))
    }
    inputs
  }
  
 observeEvent(input$action, {
   showModal(
     modalDialog(
       tagList(reactableOutput(outputId = "table"))
     ))})
  
  output$table = renderReactable({
    data = tibble(c = "click to change navbar input") %>%
      mutate(c = shinyInput(actionButton, n(), 'id', labels = c, onclick = "Shiny.setInputValue('change', this.innerText)")) %>%
      reactable(data = .,
              sortable = FALSE,
              columns = list(
                `c` = colDef(
                  html = TRUE)
              ))})
    
  observeEvent(input$change, {
  updateTabsetPanel(session = session, inputId = "nav", selected = "tabtwo")
  })  
  
   
}

shinyApp(ui, server)
4

1 回答 1

1

您可以添加按钮removeModal()observeEventchange

library(shiny)
library(reactable)

ui = fluidPage(
  
  navbarPage(title = "tabs", id = "nav",
             
             tabPanel(title = "tabone",
                      actionLink(inputId = "action", "open modalbox")),
             
             tabPanel(title = "tabtwo")
             
  ))

server = function(input, output, session){
  
  shinyInput = function(FUN, len, id, labels, ...) {
    inputs = character(len)
    for (i in seq_len(len)) {
      inputs[i] = as.character(FUN(paste0(id, i), label = labels[i], ...))
    }
    inputs
  }
  
  observeEvent(input$action, {
    showModal(
      modalDialog(
        tagList(reactableOutput(outputId = "table"))
      ))
    })
  
  output$table = renderReactable({
    data = tibble(c = "click to change navbar input") %>%
      mutate(c = shinyInput(actionButton, n(), 'id', labels = c, onclick = "Shiny.setInputValue('change', this.innerText)")) %>%
      reactable(data = .,
                sortable = FALSE,
                columns = list(
                  `c` = colDef(
                    html = TRUE)
                ))})
  
  observeEvent(input$change, {
    updateTabsetPanel(session = session, inputId = "nav", selected = "tabtwo")
    removeModal()
  })  
  
  
}

shinyApp(ui, server)

在此处输入图像描述

于 2021-08-31T06:51:12.480 回答