将数据表放在shinyjs模式中

时间:2018-05-01 10:38:25

标签: r shiny shinyjs

我正在使用Shiny与Shiny Semantic库来使应用程序看起来不错。使用showModal(modalDialog())的标准Shiny弹出消息不能与Semantic UI一起使用,所以我使用了shinyjs来使用一些javascript来允许模态。使用全局定义的js模式,如何将数据表集成到模态中?这是一个最小的例子:

library(shiny)
library(shinyjs)
library(shiny.semantic)
library(DT)

modal.js = "$('.ui.modal')
.modal('show')
;"

server <- function(input, output, session) {
  output$my_table = DT::renderDataTable({
    head(iris)
  })
  observeEvent(input$open_modal, {
    runjs(modal.js)
  })
}

ui <- semanticPage(
  suppressDependencies("bootstrap"),
  useShinyjs(),
  div(class = "ui modal",
      div(class="header", 'Modal header'),
      p('Placing DT::dataTableOutput("my_table") here fails.
         If it was displayed, I would select a row and return the value to a reactiveValue.')
  ),
  div(class = "ui basic button action-button", id = "open_modal", "Open modal ui"),
  div(class = "ui raised segment", DT::dataTableOutput("my_table"))
)

shinyApp(ui, server, options = list(launch.browser = TRUE))

2 个答案:

答案 0 :(得分:0)

我今天遇到了同样的问题,并设法找到一种解决方法。当模式在shiny.semantic中打开时,数据表输出仍被分类为不可见。要解决此问题,您只需在JavaScript中添加额外的一行:

$('#my_table').show().trigger('shown');

如果要在关闭时再次将其隐藏,则需要添加更多JS,但是在UI中使用tags$script()而不是在服务器中可能更好。

更新的应用程序:

library(shiny)
library(shinyjs)
library(shiny.semantic)
library(DT)

modal.js <- "$('.ui.modal').modal('show');
             $('#my_table').show().trigger('shown');"

server <- function(input, output, session) {
  output$my_table = DT::renderDataTable(head(iris))
  observeEvent(input$open_modal, runjs(modal.js))
}

ui <- semanticPage(
  suppressDependencies("bootstrap"),
  useShinyjs(),
  div(
    class = "ui modal",
    div(class = "header", "Modal header"),
    div(class = "content", div(class = "ui raised segment", DT::dataTableOutput("my_table")))
  ),
  div(class = "ui basic button action-button", id = "open_modal", "Open modal ui")
)

shinyApp(ui, server, options = list(launch.browser = TRUE))

答案 1 :(得分:0)

放入dataTableOutput

output$my_table = DT::renderDataTable({
    datatable(head(iris), class = "compact", escape = FALSE)   })