r - 如何调整数据表的大小以将其放入 Shiny 仪表板的框()中

标签 r shiny shinydashboard

我不知道如何确保 DT::renderDataTable 的大小适合我的盒子。

这是我的 Shiny 渲染的图片

enter image description here

有人知道如何确保 table 适合盒子吗?或者我可以在表格下方添加一个 slider 来滚动其他不在屏幕上的变量吗?

这是我的代码:

server.R

output$table = DT::renderDataTable({
    DT::datatable(
      round(df,2), 
      rownames = TRUE,
      extensions = 'Buttons',
      options = list(
        autoWidth = FALSE, 
        columnDefs = list(list(width = "125px", targets = "_all")),
        dom = 'tpB',
        lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
        pageLength = 15,
        buttons = list(
          list(
            extend = "collection",
            text = 'Show More',
            action = DT::JS("function ( e, dt, node, config ) {
                              dt.page.len(50);
                              dt.ajax.reload();}")
          ),list(
            extend = "collection",
            text = 'Show Less',
            action = DT::JS("function ( e, dt, node, config ) {
                              dt.page.len(10);
                              dt.ajax.reload();}")
            
          )
        )
      )
    )
  
  })

body.R

        box( title = "A little taste of the dataset",
             width = 12,
             DT::dataTableOutput("table") )

最佳答案

您可以简单地将 scrollX = TRUE 添加到数据表选项中:

library(shiny)
library(shinydashboard)

DF <- data.frame(replicate(50, runif(1000, 0, 10)))

ui <- fluidPage(box(
  title = "A little taste of the dataset",
  width = 12,
  DT::dataTableOutput("myTable")
))

server <- function(input, output, session) {
  output$myTable = DT::renderDataTable({
    DT::datatable(
      round(DF, 2),
      rownames = TRUE,
      extensions = 'Buttons',
      options = list(
        autoWidth = FALSE, scrollX = TRUE,
        columnDefs = list(list(
          width = "125px", targets = "_all"
        )),
        dom = 'tpB',
        lengthMenu = list(c(5, 15,-1), c('5', '15', 'All')),
        pageLength = 15,
        buttons = list(
          list(
            extend = "collection",
            text = 'Show More',
            action = DT::JS(
              "function ( e, dt, node, config ) {
                              dt.page.len(50);
                              dt.ajax.reload();}"
            )
          ),
          list(
            extend = "collection",
            text = 'Show Less',
            action = DT::JS(
              "function ( e, dt, node, config ) {
                              dt.page.len(10);
                              dt.ajax.reload();}"
            )

          )
        )
      )
    )
  })
}

shinyApp(ui = ui, server = server)

关于r - 如何调整数据表的大小以将其放入 Shiny 仪表板的框()中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57946206/

相关文章:

r - geom_step 的空心直方图或分箱

java - 如何使用 R、Java 和 JRI 绘制图形?

r - 如何在 R 中使用 read.csv() 导入最后 100 行

R - xyplot(格子);多重响应(Y 轴)- 如何识别各个点?

r - 我可以为我的 r Shiny 界面使用 index.html 和 ui.r 吗?

css - Shinydashboard - 根据所选选项卡更改背景

r - 如何从服务器端查看 Shiny 的仪表板框是否折叠

r - 如何在不终止 R 进程的情况下终止 ESS 中的 Shiny 应用程序

r - 更新R Shiny 的绘图数据(叶绿体),无需重新渲染整个 map

r - 输出表未显示在 R Shiny 应用程序中