【问题标题】:R Shiny Data Table is not showing fully as per widthR Shiny Data Table 未按宽度完全显示
【发布时间】:2021-11-24 01:11:47
【问题描述】:

我试图显示数据表是 R 闪亮并在语法中传递宽度参数。但是,表格没有显示为全宽。所以,我试图减少表格和输入滑块之间的差距。任何帮助表示赞赏。下面是输出的语法和图像。

#### Required Packages

Pckg_to_Inst = c("dashboardthemes", "shiny", "shinythemes",
                 "shinydashboard", "shinyWidgets", "DT", "bslib", "thematic", "shinydashboardPlus", "summaryBox")



##### Defining Negation 

'%not_in%' = Negate('%in%')

for(i in 1:length(Pckg_to_Inst)){
  
  if(Pckg_to_Inst[i] %not_in% installed.packages()) install.packages(Pckg_to_Inst[i],
                                                                     dependencies = TRUE)
  library(Pckg_to_Inst[i], character.only = TRUE)
}

#######################################################

ui <- fluidPage(
 
  titlePanel("Dashboard"),
  br(),
  navbarPage("",
             
             ###### Here : insert shinydashboard dependencies ######
             header = tagList(
               useShinydashboard()
             ),
             #######################################################
             
             navbarMenu("Data Tracker", icon = icon("database"),
                        
                        tabPanel("Test", fluid = TRUE,
 
                                 hr(),
                                 
                                 fluidRow(
                                   column(12,
                                          uiOutput("summarybox"))
                                 ),
                                 
                                 hr(),
                                 
                                 fluidRow(
                                   
                                   column(8, 
                                          column(width = 12,
                                                 mainPanel(DT::dataTableOutput("table1")))),
                                   
                                   
                                   column(4,
                                          box(
                                            width = 12,
                                            title = "Data Secription - ",
                                            selectInput(inputId = "Data_Type",
                                                        label = "Select Time Frame/Data Type",
                                                        choices = c("A", "B", "C", "D"),
                                                        selected = "A",
                                                        width = "220px"),
                                            background = "black",
                                            "A box with a solid black background"
                                          )))
                                 
                        ))

             
  ))

server <- function(input, output) {
  
  output$table1 <- DT::renderDataTable({
  
    DT::datatable(iris, class = 'cell-border stripe', 
                  options = list(
                    initComplete = JS(
                      "function(settings, json) {","$(this.api().table().header()).css({'background-color': 'teal', 'color': 'black'});","}"),
                    scrollX = TRUE,  
                    scrollY = TRUE,
                    pageLength = 5),
                  extensions = 'Buttons',
                  selection = 'single',       ## enable selection of a single row
                  filter = 'top',              ## include column filters at the bottom
    )
    
  })

}

shinyApp(ui, server)

附加的是输出图像。

【问题讨论】:

    标签: r shiny datatable


    【解决方案1】:

    您的DT::dataTableOutput("table1")mainPanel 内,默认情况下,mainPanel 的宽度为 8(侧边栏占用其他 4 个)。

    来自?mainPanel

    用法 侧边栏布局( 侧边栏面板, 主面板, 位置 = c("左", "右"), 流体=真 )

    sidebarPanel(..., width = 4)

    mainPanel(..., width = 8)

    【讨论】:

    • 好的。但在这种情况下,不应该在两者之间产生空格,对吧?
    • mainPanel 中的width = 8 表示“占用父容器的 12 列中的 8 列”。或者占据父容器宽度的 2/3。去掉mainPanel,直接把数据表放到列里,应该可以解决问题了。
    • 是的,我做了同样的事情并删除了宽度以及主面板。它解决了这个问题。谢谢
    【解决方案2】:

    你可以给dataTableOutput一个自定义的heightwidth

    library(shiny)  
    
    ui <- fluidPage(
        
        titlePanel("Dashboard"),
        br(),
        navbarPage("",
                   
                   ###### Here : insert shinydashboard dependencies ######
                   header = tagList(
                     useShinydashboard()
                   ),
                   #######################################################
                   
                   navbarMenu("Data Tracker", icon = icon("database"),
                              
                              tabPanel("Test", fluid = TRUE,
                                       
                                       hr(),
                                       
                                       fluidRow(
                                         column(12,
                                                uiOutput("summarybox"))
                                       ),
                                       
                                       hr(),
                                       
                                       fluidRow(
                                         
                                         column(8, 
                                                column(width = 12,                                                    mainPanel(DT::dataTableOutput("table1", width = '800px')))),
                                         
                                         
                                         column(4,
                                                box(
                                                  width = 12,
                                                  title = "Data Secription - ",
                                                  selectInput(inputId = "Data_Type",
                                                              label = "Select Time Frame/Data Type",
                                                              choices = c("A", "B", "C", "D"),
                                                              selected = "A",
                                                              width = "220px"),
                                                  background = "black",
                                                  "A box with a solid black background"
                                                )))
                                       
                              ))
                   
                   
        ))
      
      server <- function(input, output) {
        
        output$table1 <- DT::renderDataTable({
          
          DT::datatable(iris, class = 'cell-border stripe', 
                        options = list(
                          initComplete = JS(
                            "function(settings, json) {","$(this.api().table().header()).css({'background-color': 'teal', 'color': 'black'});","}"),
                          scrollX = TRUE,  
                          scrollY = TRUE,
                          pageLength = 5),
                        extensions = 'Buttons',
                        selection = 'single',       ## enable selection of a single row
                        filter = 'top',              ## include column filters at the bottom
          )
          
        })
        
      }
      
      shinyApp(ui, server)
    

    【讨论】:

    • 感谢您的解决方案,但它仍然显示中间的空白。
    • 您可以将DT::dataTableOutput("table1", width = '800px') 中的width 更改为您选择的任何内容。它对我有用。
    • 实际上,我在发布问题之前已经尝试过这个解决方案,但不确定它对我不起作用。但是,删除 mainPanel 参数对我有用。感谢您的回复。
    猜你喜欢
    • 2017-04-03
    • 2018-02-06
    • 2021-07-20
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2020-11-06
    相关资源
    最近更新 更多