2

我正在尝试使用和包DT::datatable在 Tufte html 文档中包含一个。但是,即使我使用选项设置列的宽度,或者使用或选项设置 输出的宽度,仍然会沿页面、主列和边距列的整个宽度显示!正如您在下图中看到的,它位于直方图下方,但应仅在主列中放置在其旁边。BookdownknitrcolumnDefskintrfig.fullwidth = FALSEout.with = '50%'DT::datatableDT::datatable

我的问题很简单,你知道如何DT::datatable只在Tufte html 书籍的主栏中显示 a 吗?您可能会看到产生DT::datatable

非常感谢你的帮助。

在此处输入图像描述

{r high-range-cv, echo = FALSE, fig.fullwidth = FALSE}
sketch = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(rowspan = 2, 'Indicator'),
      th(colspan = 2, 'Highest Variability'),
      th(colspan = 2, 'Lowest Variability')
    ),
    tr(
      lapply(rep(c('Country', 'CV'), 2), th),
      th("Diff")
    )
  )
))

diff_ind %>%
  select(ind_name, max_cty, max, min_cty, min, diff) %>%
  datatable(colnames = c("Indicator", "Country", "Max", "Country", "Min", "Diff"),
            container = sketch,
            rownames = FALSE,
            filter = 'top', 
            extensions = 'FixedColumns',
            options = list(pageLength = 6,
                           autowidth = TRUE,
                           columnDefs = list(list(width = '50px', targets = c(2, 4, 5)),
                                              list(width = '100px', targets = c(1, 3)),
                                              list(width = '300px', targets = 0)
                           ),
                           scrollX = TRUE,
                           fixedColumns = TRUE)) %>%
  formatRound(c("max", "min", "diff") , 2)
4

1 回答 1

3

正如在https://github.com/rstudio/DT/issues/762中回答的那样,使用 width 参数DT::datatable()可以解决问题:

DT::datatable(iris, width = '55%', options = list(scrollX = TRUE))
于 2020-01-30T13:26:06.927 回答