rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
215 stars 69 forks source link

Make `attachDependencies()` a generic that understands htmlwidgets #314

Open stla opened 2 years ago

stla commented 2 years ago

Hello,

I've defined an html dependency htmlDep. When I do that, it works:

dtable <- 
  datatable(
    dat, 
    container = sketch, 
    options = list(
      orderCellsTop = TRUE,
      initComplete = JS(js),
      columnDefs = list(
        list(targets = "_all", className = "dt-center")
      )
    )
  )
dtable[["dependencies"]] <- c(dtable[["dependencies"]], list(htmlDep))
dtable

But when I do that, it doesn't work, as if the dependency were not attached:

datatable(
  dat, 
  container = sketch, 
  options = list(
    orderCellsTop = TRUE,
    initComplete = JS(js),
    columnDefs = list(
      list(targets = "_all", className = "dt-center")
    )
  )
) %>% attachDependencies(htmlDep)
cpsievert commented 2 years ago

If it helps at all, a workaround is to work directly with the $dependencies element of an htmlwidget:

dt <- datatable(dat)
dt$dependencies <- c(dt$dependencies, htmldep)

But yes, it'd be nice if attachDependencies() actually worked on htmlwidget