首页 > 解决方案 > 如何在 R Shiny Report 中为数据表设置自动列宽

问题描述

我无法在作为闪亮仪表板一部分的数据表中设置自动列宽。

它出现在多行中,例如(“Apr-”在一行中,“18”在另一行中;类似地,“Banking”在一行中,“Products”在另一行中)

在此处输入图像描述

在此处输入图像描述

我正在使用以下脚本但无法正常工作。

谁能帮我解决这个问题?

output$Tabledt = DT::renderDataTable({datatable(table1(), rownames = FALSE, plugins = "ellipsis", options = list(autoWidth = TRUE, columnDefs = list(list(width = '200px', targets = c(1))))) %>%
          formatRound(columns = c(2:73), digits = 2)
          })
    }

根据要求在下面给出了一个例子。

数据框:

TableA = data.frame('Description' = c('Marketing & Communications', 'Corp Development & Strategy', 'COG Technology Workplace'), 'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3),'Test Lengthy Column header' = c(1:3))

服务器:

server = function(input, output, session) {

    output$Tabledt = DT::renderDataTable({
    datatable(TableA[], rownames = FALSE, plugins = "ellipsis",
              options = list(autoWidth = TRUE, columnDefs = list(list(width = '200px', targets = c(1))))) %>%
      formatRound(columns = c(2), digits = 2)
  })
  
    output$mainpanel = renderUI({tabsetPanel(tabPanel("Report", value = 'tab1', DT::dataTableOutput("Tabledt")))})

}

用户界面:

library(shiny)
ui = shinyUI(
  fluidPage(
    mainPanel(uiOutput("mainpanel"))
  )
)

应用程序:

shinyApp(ui = ui, server = server)

在上面的示例中,不知何故,值列标题是自动拟合的,但不是第一列。在我的真实世界场景中,所有列宽都没有自动调整。不知道为什么。

在此处输入图像描述

标签: rshinyshinydashboardshinyapps

解决方案


推荐阅读