Search code examples
rdtbookdowntufte

DT::datatable in main column in Tufte html output


I am trying to include a DT::datatable in a Tufte html document using Bookdown and knitr packages. However, even if I set the width of the columns using columnDefs option, or the width of the kintr output using fig.fullwidth = FALSE or out.with = '50%' option, the DT::datatable is still displayed along the full width of the page, main column and margin column! As you can see in the image below, the DT::datatable is below the histogram chart, but it should be placed right next to it in the main column only.

My question is simple, do you know how to display a DT::datatable only in the main column of a Tufte html book? you may see the piece of code that produces the DT::datatable

Thank you so much for your help.

enter image description here

{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)

Solution

  • As answered in https://github.com/rstudio/DT/issues/762, use the width argument in DT::datatable() will do the trick:

    DT::datatable(iris, width = '55%', options = list(scrollX = TRUE))