DT ::数据表在闪亮的应用程序中显示了两次

时间:2019-08-23 10:26:56

标签: r datatable shiny dt

我有一个shiny应用程序,当用户从侧边栏中的dataTable菜单中选择Sector A时,显示radioButtons。问题是它显示两次。我也在浏览器模式下检查过它。为什么会发生这种情况,我在此处显示整个应用程序,因为它可能是由应用程序的if逻辑引起的。 renderTable()正常工作,所以我想DT

有问题
#ui.r
library(shiny)
library(shinythemes)  
library(DT)

ui <- fluidPage(
  theme=shinytheme("slate") ,
  # App title ----
  titlePanel("Tabsets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      uiOutput("rad")
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      uiOutput("tabers"),
      DT::dataTableOutput("table")
    )
  )
)
#server.r
library(shiny)
library(DT)
server = function(input, output) {

  output$rad<-renderUI({
    radioButtons("radio", label = "",
                 choices = list("Home"=1,"About" = 2, "Sector A" = 3, "Sector B" = 4,"Sector C" = 5), 
                 selected = 1)
                 #selected = character(0))
  })

  output$tabers<-renderUI({
    if(is.null(input$radio)) {
      tabsetPanel(
        id="tabF",
        type = "tabs",
        tabPanel("Global"),
        tabPanel("Performance")
      )
    }
    else if(input$radio==3){
      tabsetPanel(
        id="tabC",
        type = "tabs",
        tabPanel("Constituents",
                 output$table <- renderDataTable({
                   mtcars
                 })
                 ),
        tabPanel("Clusters" ),
        tabPanel("Index")
      )
    }
    else if(input$radio==4){
      tabsetPanel(
        id="tabD",
        type = "tabs",
        tabPanel("Constituents"

                 ),
        tabPanel("Clusters" ),
        tabPanel("Index")
      ) 
    }
    else if(input$radio==5){
      tabsetPanel(
        id="tabE",
        type = "tabs",
        tabPanel("Constituents"

                 ),
        tabPanel("Clusters" ),
        tabPanel("Index")
      ) 
    }
    else if(input$radio==2){

    }
    # Left last else in here but should not get called as is
    else if(input$radio==1){
      tabsetPanel(
        id="tabA",
        type = "tabs",
        tabPanel("Global"),
        tabPanel("Performance" )
      ) 
    }
  })
}

1 个答案:

答案 0 :(得分:1)

看起来renderTable做同样的事情。由于某种原因,renderDataTable({mtcars})的输出显示两次,第一次显示到uiOutput,第二次显示到dataTableOutput()(都在mainPanel中)。注释行dataTableOutput("table")可解决此问题,因为该行为仅显示该表一次。有趣的是,像这样删除任务:

else if(input$radio==3){
      tabsetPanel(
        id="tabC",
        type = "tabs",
        tabPanel("Constituents",
                 renderDataTable({
                   mtcars
                 })
        ),
        tabPanel("Clusters" ),
        tabPanel("Index")
      )
    }

还会渲染一次表格。因此,看起来像在renderUI内时,renderDataTable仅创建输出而无需在UI中使用dataTableOutput

这似乎允许(无论好坏)可以轻松地在不同选项卡中呈现不同的表,而无需在UI中使用相应的输出条目。

else if(input$radio==3){
      tabsetPanel(
        id="tabC",
        type = "tabs",
        tabPanel("Constituents",
                 renderDataTable({
                   mtcars
                 })
        ),
        tabPanel("Clusters" ),
        tabPanel("Index")
      )
    }
    else if(input$radio==4){
      tabsetPanel(
        id="tabD",
        type = "tabs",
        tabPanel("Constituents",
                 renderDataTable({
                   iris
                 })
        ),
        tabPanel("Clusters" ),
        tabPanel("Index")
      ) 
    }