r - 当输入更改(例如,选项卡更改)时,我如何告诉我的 modalDialog 自动关闭?

标签 r shiny reactable

我有一个带有 navbarPage 和两个 tabPanel 的 Shiny App。在第一个选项卡中,我有一个 actionLink,它初始化一个 modalDialog,包括一个以单元格作为按钮的 reactable。当我单击表格中的按钮时,我想更改来自 navbarPage 的输入,即将 tabPanel 从“tabone”更改为“tabtwo”。同时,我希望在 tabPanel 更改时关闭 modalDialog。我怎样才能让我的 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)

最佳答案

您可以在 change 按钮的 observeEvent 中添加 removeModal()

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)

enter image description here

关于r - 当输入更改(例如,选项卡更改)时,我如何告诉我的 modalDialog 自动关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68993936/

相关文章:

r - 我如何从 2004 Wayback machine 站点上抓取这段文本/为什么我运行的代码是错误的?

r - 如何使用嵌套图加速 react 速度?

以编程方式进行 R react 分组列

javascript - Shiny htmlOutput 中的 for 循环未按顺序执行

updateData 上可 react 的 R Shiny 条件颜色列

r - 选择包含超过 X 个非零连续值的行

r - 如何找到覆盖R中一组点的给定分数的最小椭圆?

r - 使用solve/optim根据两点(半径已知)确定圆心

r - 如何基于 Shiny 的 react 变量的线性模型进行预测

r - R Shiny 中带有 leafletProxy 的 Leaflet drawToolbar