【问题标题】:R Shiny table with horizontal scrollbar both at the top and at the bottomR Shiny table,顶部和底部都有水平滚动条
【发布时间】:2020-10-23 11:16:44
【问题描述】:

请我有一个 R 闪亮的应用程序,其中一个页面有一个非常大的表。出于这个原因,我需要在表格的顶部和底部都有水平滚动条。 请记住,我对 HTML、CSS 和 JS 不太熟悉。 此外,我已经设法使用解决方案将水平滚动条移动到表格顶部: R DT Horizontal scroll bar at top of the table

我实际上是在使用那里解释的示例,并且效果很好。我只需要一些帮助来添加底部的滚动条。

css <- HTML(
    "#flipped > .dataTables_wrapper.no-footer > .dataTables_scroll > .dataTables_scrollBody {
        transform:rotateX(180deg);
    }
    #flipped > .dataTables_wrapper.no-footer > .dataTables_scroll > .dataTables_scrollBody table{
        transform:rotateX(180deg);
    }"
)

ui <- fluidPage(
    tags$head(tags$style(css)),
    fluidRow(column(width = 6,
                    h4("Flipped Scrollbar"),
                    br(),
                    DT::dataTableOutput("flipped")
                    ),
             column(width = 6,
                    h4("Regular Scrollbar"),
                    br(),
                    DT::dataTableOutput("regular")
                    )
             )
)

server <- function(input, output, session) {
    output$flipped <- DT::renderDataTable({
        DT::datatable(mtcars, rownames = FALSE,
                      options = list(
                          scrollX = TRUE
                      )
        )
    })
    output$regular <- DT::renderDataTable({
        DT::datatable(mtcars, rownames = FALSE,
                      options = list(
                          scrollX = TRUE
                      )
        )
    })
}

shinyApp(ui, server)

我设法找到了一个类似的问题 (horizontal scrollbar on top and bottom of table) 但是,我不明白如何将该 css 和 JS 代码应用于 Shiny 应用程序。 非常感谢

更新(仍然不起作用)作为 Stéphane Laurent 建议解决方案的后续行动。 我现在的代码是:

library(shiny)
library(DT)

wideTable <- as.data.frame(matrix(rnorm(1000), nrow = 10, ncol = 100))

js <- "
$(document).ready(function(){
  $('#dtable').on('shiny:value', function(e){
    setTimeout(function(){
      $('#dtable table').wrap('<div id=\"scrolldiv\"></div>');
      $('#scrolldiv').doubleScroll({
        contentElement: $('table'),
          scrollCss: {                
              'overflow-x': 'scroll',
              'overflow-y': 'hidden'
          },
          contentCss: {
              'overflow-x': 'scroll',
              'overflow-y': 'hidden'
          },
        resetOnWindowResize: true
      });
      setTimeout(function(){$(window).resize();}, 100);
    }, 0);
  });
});
"

CSS <- "
.doubleScroll-scroll-wrapper {
  clear: both;
}
"

ui <- fluidPage(
  tags$head(
    tags$script(src = "jquery.doubleScroll.js"),
    tags$script(HTML(js)),
    tags$style(HTML(CSS))
  ),
  br(),
  dataTableOutput("dtable")
)

server <- function(input, output, session){
  
  output$dtable <- DT::renderDataTable({
    datatable(wideTable, 
              rownames = T,
              filter = 'top',
              caption = paste0("All columns of CSV report")
)
      })
      
    }
    
    shinyApp(ui, server)

【问题讨论】:

    标签: html r shiny scrollbar


    【解决方案1】:

    这是一个使用 DoubleScroll JavaScript 库的解决方案。

    here 下载文件 jquery.doubleScroll.js。将其放在闪亮应用的 www 子文件夹中。

    然后这里是应用程序:

    library(shiny)
    library(DT)
    
    wideTable <- as.data.frame(matrix(rnorm(1000), nrow = 10, ncol = 100))
    
    js <- "
    $(document).ready(function(){
      $('#dtable').on('shiny:value', function(e){
        setTimeout(function(){
          $('#dtable table').wrap('<div id=\"scrolldiv\"></div>');
          $('#scrolldiv').doubleScroll({
            contentElement: $('table'),
              scrollCss: {                
                  'overflow-x': 'scroll',
                  'overflow-y': 'hidden'
              },
              contentCss: {
                  'overflow-x': 'scroll',
                  'overflow-y': 'hidden'
              },
            resetOnWindowResize: true
          });
          setTimeout(function(){$(window).resize();}, 100);
        }, 0);
      });
    });
    "
    
    CSS <- "
    .doubleScroll-scroll-wrapper {
      clear: both;
    }
    "
    
    ui <- fluidPage(
      tags$head(
        tags$script(src = "jquery.doubleScroll.js"),
        tags$script(HTML(js)),
        tags$style(HTML(CSS))
      ),
      br(),
      DTOutput("dtable")
    )
    
    server <- function(input, output, session){
      
      output[["dtable"]] <- renderDT({
        datatable(wideTable)
      })
      
    }
    
    shinyApp(ui, server)
    

    如果你的数据表的输出id不是"dtable",那么在JS代码(js)中用你的数据表的输出id替换dtable(出现两次)。

    【讨论】:

    • 您好 Stephane,非常感谢您分享所有这些信息。我刚试过,但我仍然在表格底部只看到一个滚动条。我注意到唯一的区别似乎是我的表的类型是“dataTableOutput(outputId="dtable")”。这能解释问题吗?谢谢
    • @Angelo 对不起,我不明白你的意思。您是否删除了scrollX 选项?你能编辑你的帖子来展示你做了什么吗?
    • 当然,Stephane,我刚刚在 postt 中添加了我的代码。我从选项中删除了 scrollX,所以现在表格根本没有任何水平滚动
    • @Angelo 你确定你在 www 子文件夹中有 jquery.doubleScroll.js 文件吗?
    • @lmsimp 你必须创建它。在包含应用程序的 R 代码的文件夹中。
    猜你喜欢
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多