【问题标题】:Display multiple DT tables in R Viewer在 R Viewer 中显示多个 DT 表
【发布时间】:2020-02-05 13:48:19
【问题描述】:

我有两个列/行数相同的 DT 表。我想在 R Viewer 中显示它们。我尝试使用 print 但它只是创建了两个不同的视图。

我尝试使用 htmlTools 包中的可浏览,我认为它工作正常。但是,它使两个表格重叠,右侧的滚动条也与表格重叠。只是想知道是否有办法设置表格边距?我尝试添加到样式选项中,但它不起作用。任何帮助将不胜感激!

library(DT)
library(htmltools)

t1<-data.frame(matrix(data = round(rnorm(285), 3), nrow=15, ncol=19,
                      dimnames = list(NULL,seq(from=0, to=0.9, by=0.05))), check.names=FALSE)

t2<-data.frame(matrix(data = round(rnorm(285), 3), nrow=15, ncol=19,
                      dimnames = list(NULL,seq(from=0, to=0.9, by=0.05))), check.names=FALSE)

dt_t1 <- datatable(t1,
                      caption = paste0("Variable tested: "),
                      rownames=TRUE,
                     fillContainer = F,
                      options =list(pageLength = 20,
                                    dom = "t", 
                                    ordering = F,
                                    autoWidth = TRUE,
                                    initComplete = htmlwidgets::JS(
                                     "function(settings, json) {",
                                    paste0("$(this.api().table().container()).css({'font-size': '", "9pt", "'});"),
                                    "}"),
                                    columnDefs = list(list(width = '30px', targets = 0), list(width = '10px', targets = c(1,2)))
                                    )
                                   )


dt_t2 <- datatable(t1,
                   caption = paste0("Variable tested: "),
                   rownames=TRUE,
                   fillContainer = F,
                   options =list(pageLength = 20,
                                 dom = "t", 
                                 ordering = F,
                                 autoWidth = TRUE,
                                 initComplete = htmlwidgets::JS(
                                   "function(settings, json) {",
                                   paste0("$(this.api().table().container()).css({'font-size': '", "9pt", "'});"),
                                   "}"),
                                 columnDefs = list(list(width = '30px', targets = 0), list(width = '10px', targets = c(1,2)))
                   ))



browsable(
  tagList(list(
    tags$div(
      style = 'display:block; float:top; margin-right:30px;',
      dt_t1
    ),
    tags$div(
      style = 'display:block;float:top; margin-right:30px; margin-top:0px;',
      dt_t2))))

【问题讨论】:

    标签: html r dt


    【解决方案1】:

    理论上combineWidgetsmanipulateWidget 包中:

    library(manipulateWidget)
    library(DT)
    
    combineWidgets(
      datatable(iris[1:5,]),
      datatable(iris[1:5,]),
      ncol = 2, colsize = c(1,1)
    )
    

    但奇怪的是这不起作用:这会导致一个空白窗口。

    但是,如果您封装在 manipulateWidget 中,则此方法有效:

    manipulateWidget({
      combineWidgets(title = "The iris data set", 
                     ncol = 2, colsize = c(1,1),
                     datatable(iris),
                     datatable(iris))
    }, select = mwSelect(1:3))
    

    【讨论】:

    • 嗨斯蒂芬妮,这对我不起作用。我在 R Studio 中运行它而不是 Shiny,我认为这就是原因。我在上面添加了一些代码,它们在一个视图中生成两个表,但需要一些我正在努力解决的格式。
    • @Joana manipulateWidget 在 RStudio 查看器中工作。我不知道你为什么说闪亮。
    • @StéphaneLaurent 请注意,operatedWidget 实际上确实使用了闪亮,这就是你得到一个空白屏幕的原因,它确实通过封装工作(参见cran.r-project.org/web/packages/manipulateWidget/vignettes/…)。
    • @Joana 您可能会尝试使用 R-markdown 笔记本并将其编织成 HTML。我需要诚实地告诉你,我正在尝试做同样的事情并使用 htmlwidgets i(datatable) n Bookdown,我仍在尝试按照我想要的方式对其进行格式化。因此,请尽快发布您的解决方案(也会这样做)
    【解决方案2】:

    我设法通过编辑格式使其工作。我在数据表中添加了class="compact",这减少了列之间的空间,然后从固定表重叠的可浏览中删除了display:block;float:top;

    library(DT)
    library(htmltools)
    
    t1<-data.frame(matrix(data = round(rnorm(285), 3), nrow=15, ncol=19,
                          dimnames = list(NULL,seq(from=0, to=0.9, by=0.05))), check.names=FALSE)
    
    t2<-data.frame(matrix(data = round(rnorm(285), 3), nrow=15, ncol=19,
                          dimnames = list(NULL,seq(from=0, to=0.9, by=0.05))), check.names=FALSE)
    
    dt_t1 <- datatable(t1,
                       caption = paste0("Variable tested: "),
                       rownames=TRUE,
                       width = "95%",
                       height = "95%",
                       class="compact",
                       fillContainer = F,
                       options =list(pageLength = 20,
                                     dom = "t", 
                                     ordering = F,
                                     autoWidth = TRUE,
                                     columnDefs = list(list(width = '40px', targets = 0)),
                                     initComplete = htmlwidgets::JS(
                                       "function(settings, json) {",
                                       paste0("$(this.api().table().container()).css({'font-size': '", "9pt", "'});"),
                                       "}"))
                       )
    )
    
    
    dt_t2 <- datatable(t1,
                       caption = paste0("Variable tested: "),
                       rownames=TRUE,
                       width = "95%",
                       height = "95%",
                       class="compact",
                       fillContainer = F,
                       options =list(pageLength = 20,
                                     dom = "t", 
                                     ordering = F,
                                     autoWidth = TRUE,
                                     columnDefs = list(list(width = '40px', targets = 0)),
                                     initComplete = htmlwidgets::JS(
                                       "function(settings, json) {",
                                       paste0("$(this.api().table().container()).css({'font-size': '", "9pt", "'});"),
                                       "}"))
                       )
    
    
    
    browsable(
      tagList(list(
        tags$div(
          dt_t1,
          style = 'width:95%; margin-bottom: 30px;'
        ),
        tags$div(
          dt_t2,
          style = 'width:95%; margin-bottom: 30px;'))))```
    

    【讨论】:

      猜你喜欢
      • 2017-11-06
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 2018-07-13
      • 2021-09-02
      • 2018-04-01
      • 2019-03-28
      相关资源
      最近更新 更多