且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Shiny 中两次使用相同的输出元素

更新时间:2023-12-04 12:09:34

你的 ui 代码很好,但是:

Your ui code is fine, but:

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

Shiny doesn't support multiple outputs with the same name. This code would generate HTML where two elements have the same ID, which is invalid HTML.

所以,我认为您唯一的解决方案是创建第三个表.***的选择是在中间使用反应式,这样你就可以避免两次使用相同的代码.

So, I think your only solution would be to create a third table. The best option is to use a reactive in the middle, so you avoid having the same code used twice.

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()
  )

}

希望这有帮助!