【问题标题】:Cannot get bsModal to trigger within Shiny modules无法在 Shiny 模块中触发 bsModal
【发布时间】:2021-10-28 20:21:33
【问题描述】:

我正在尝试在使用模块的 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 图,但单击动作按钮应该会打开一个具有相同图的新窗口,但实际上并没有。

【问题讨论】:

  • 您缺少一个ns()id = ns("modal_example") 应该这样做。

标签: r shiny shinybs


【解决方案1】:

您必须加载shinyBS 包。

library(shiny)
library(shinyBS)

......

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-31
    • 2018-10-26
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2017-08-19
    • 1970-01-01
    相关资源
    最近更新 更多