2017-07-18 82 views
1

取自Shiny畫廊的示例。我想在第一個選項卡上顯示ex1和ex2,在第二個選項卡上有一些中斷和ex2。在Shiny中使用相同的輸出元素兩次

ui.R

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

server.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')) 
) 

回答

0

你的UI代碼是好的,但:

閃亮不支持多個輸出具有相同的名稱。此代碼 將生成HTML,其中兩個元素具有相同的ID,即 無效的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() 
) 

} 

希望這有助於!

相關問題