简体   繁体   English

R Shiny 表格在顶部和底部都有水平滚动条

[英]R Shiny table with horizontal scrollbar both at the top and at the bottom

please I have a R shiny application and one of the pages have a really big table.请我有一个 R shiny 应用程序,其中一个页面有一个非常大的表。 For this reason, I would need to have the horizontal scrollbar both at the top as well as the bottom of the table.出于这个原因,我需要在表格的顶部和底部都有水平滚动条。 Please, bear in mind I'm very little familiar with HTML, CSS and JS.请记住,我对 HTML、CSS 和 JS 不太熟悉。 Also, I already managed to move the horizontal scrollbar to the top of the table using solution: R DT Horizontal scroll bar at top of the table此外,我已经设法使用解决方案将水平滚动条移动到表格顶部: R DT 表格顶部的水平滚动条

I'm literally using the example explained there and it works perfectly.我实际上是在使用那里解释的示例,并且效果很好。 I would just need some help in adding the scrollbar at the bottom as well.我只需要一些帮助来添加底部的滚动条。

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)

I managed to find a similar question ( horizontal scrollbar on top and bottom of table ) however, I can't understand how to apply that css and JS code to a Shiny application.我设法找到了一个类似的问题( 表格顶部和底部的水平滚动条)但是,我不明白如何将 css 和 JS 代码应用于 Shiny 应用程序。 Many thanks非常感谢

Update (that still doesn't work) as a follow-up to Stéphane Laurent suggested solution.更新(仍然不起作用)作为 Stéphane Laurent 建议解决方案的后续行动。 My current code now is:我现在的代码是:

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)

Here is a solution using the DoubleScroll JavaScript library.这是使用DoubleScroll JavaScript 库的解决方案。

Download the file jquery.doubleScroll.js from here .这里下载文件jquery.doubleScroll.js Put it in the www subfolder of your shiny app.将其放在 shiny 应用程序的www子文件夹中。

Then here is the app:然后这里是应用程序:

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)

If the output id of your datatable is not "dtable" , then in the JS code ( js ) replace dtable (two occurences) with the output id of your datatable.如果您的数据表的 output id 不是"dtable" ,则在 JS 代码 ( js ) dtable (两次出现) 替换为您的数据表的 output id。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM