【问题标题】:Using the same output element twice in Shiny在 Shiny 中使用相同的输出元素两次
【发布时间】:2017-12-23 01:40:14
【问题描述】:

示例来自 Shiny 画廊。我想在第一个选项卡上显示 ex1 和 ex2,在第二个选项卡上和 ex2 之间有一些中断。

ui.R

navbarPage(
  title = 'DataTable Options',
  tabPanel('Display length',     DT::dataTableOutput('ex1')),
  tabPanel('Length menu',        DT::dataTableOutput('ex2'))
)

服务器.R

function(input, output) {

  # display 10 rows initially
  output$ex1 <- DT::renderDataTable(
    DT::datatable(iris, options = list(pageLength = 25))
  )

  # -1 means no pagination; the 2nd element contains menu labels
  output$ex2 <- DT::renderDataTable(
    DT::datatable(
      iris, options = list(
        lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
        pageLength = 15
      )
    )
  )

}

我认为下面的代码会起作用,但事实并非如此。它确实在任何选项卡中显示任何内容。

navbarPage(
  title = 'DataTable Options',
  tabPanel('Display length',     DT::dataTableOutput('ex1'),
           HTML("<br><br><br>"),
           DT::dataTableOutput('ex2')),
  tabPanel('Length menu',        DT::dataTableOutput('ex2'))
)

【问题讨论】:

    标签: r datatables shiny


    【解决方案1】:

    你的用户界面代码很好,但是:

    Shiny 不支持同名的多个输出。这段代码 将生成两个元素具有相同 ID 的 HTML,即 无效的 HTML。

    所以,我认为您唯一的解决方案是创建第三个表。最好的选择是在中间使用响应式,这样可以避免重复使用相同的代码。

    function(input, output) {
    
      # display 10 rows initially
      output$ex1 <- DT::renderDataTable(
        DT::datatable(iris, options = list(pageLength = 25))
      )
    
      # -1 means no pagination; the 2nd element contains menu labels
    
      iris_table <- reactive({
        DT::datatable(
          iris, options = list(
            lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
            pageLength = 15
          )
        )
      })
    
      output$ex2 <- DT::renderDataTable(
        iris_table()
      )
      output$ex3 <- DT::renderDataTable(
        iris_table()
      )
    
    }
    

    希望这会有所帮助!

    【讨论】:

    • 仅供参考:这也有效。 output$ex2
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2017-10-15
    • 2013-02-17
    相关资源
    最近更新 更多