0

我正在尝试在使用模块的 Shiny 应用程序中创建和触发 bsModal,但我无法使用操作按钮打开它。我最初的想法是它与操作按钮 ID 上的 ns() 函数有关,但我尝试过使用带和不带 ns() 的触发器 ID,但都没有奏效。我创建了下面的示例来模拟问题。

test_mod_ui <- function(id) {
  ns <- NS(id)
  sidebarLayout(
    sidebarPanel(
      actionButton(ns("test"), "Show Plot")
    ), 
    mainPanel(
      plotOutput(ns("cars")),
      shinyBS::bsModal(
        id = "modal_example",
        title = "Example",
        trigger = ns("test"),
        size = "large",
        plotOutput(ns("cars_modal"))
      )
    )
  )
}

test_mod_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    
    output$cars <- renderPlot(
      pairs(
        ~mpg+disp+drat+wt,
        data=mtcars,
        main="Simple Scatterplot Matrix"
      )
    )
    
    output$cars_modal <- renderPlot(
      pairs(
        ~mpg+disp+drat+wt,
        data=mtcars,
        main="Simple Scatterplot Matrix"
      )
    )
  })  
}


ui <- fluidPage(
  tagList(
    test_mod_ui("test")
  )
)

server <- function(input, output) {
  test_mod_server("test")
}

shinyApp(ui, server)

动作按钮和 cars_modal 图出现,但单击动作按钮应该打开一个具有相同图的新窗口,但它没有。

4

1 回答 1

0

你必须加载shinyBS包。

library(shiny)
library(shinyBS)

......

然后它工作正常(你可以删除shinyBS::)。

于 2021-08-30T05:34:51.610 回答