【问题标题】:DT:: Datatable is displayed twice in a shiny applicationDT:: Datatable 在闪亮的应用程序中显示两次
【发布时间】:2019-08-23 10:26:56
【问题描述】:

我有一个shiny 应用程序,当用户从侧边栏中的radioButtons 菜单中选择Sector A 时,会显示dataTable。问题是它显示了两次。我也在浏览器模式下检查过。为什么会发生这种情况我在这里显示整个应用程序,因为它可能是由应用程序的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" )
      ) 
    }
  })
}

【问题讨论】:

    标签: r datatable shiny dt


    【解决方案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")
          ) 
        }
    

    【讨论】:

      猜你喜欢
      • 2021-09-02
      • 2022-01-08
      • 2020-01-13
      • 1970-01-01
      • 2019-03-28
      • 2018-11-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多