ramnathv / htmlwidgets

HTML Widgets for R
http://htmlwidgets.org
Other
792 stars 206 forks source link

Dependency ordering #179

Open cpsievert opened 8 years ago

cpsievert commented 8 years ago

I'd like to use htmlwidgets::createWidget(dependencies = ...) to add a conditional dependency and I need that dependency to be placed above "official" dependencies declared in the YAML. Is there a slick way to do this that I'm unaware of?

timelyportfolio commented 8 years ago

Not that I know of considering lines. A not slick way is to wrap in a tagList and declare the dependencies there.

Interesting, since generally this flow from YAML to added dependencies is what I need.

timelyportfolio commented 8 years ago

Even less slick is to leave the YAML blank and declare all dependencies with createWidget(dependencies=...).

timelyportfolio commented 8 years ago

or get return value from htmltools::as.tags(plot_ly(...)) and then order the dependencies, so something like

p <- plot_ly(...)
p_tag <- htmltools::as.tags(p)

attr(p_tag,"html_dependencies") <- rev(attr(p_tag,"html_dependencies"))
ramnathv commented 8 years ago

Just so I understand @cpsievert, you can use the dependencies argument in createWidget, but the trouble is that it would place the dependency at the end of the other official dependencies. Is that correct?

cpsievert commented 8 years ago

yep

ramnathv commented 8 years ago

Okay. I don't see any other ways of doing it other than the ones proposed by @timelyportfolio. However, this use case does motivate the need to provide more control for placement of js/css assets. One idea I have toyed around with is introducing a priority argument in htmltools::htmlDependency. It would function like a z-index in html. I am not very clear how priorities would interact across widgets. @jjallaire @jcheng5 any thoughts on this?

timelyportfolio commented 8 years ago

Could we just provide a key/priority as @ramnathv suggests, and then sort by the key, so lines become? This would require a change to htmltools::htmlDependency also though.

dependencies <- c(widget_dependencies(class(x)[1], attr(x, 'package')),
  x$dependencies)

priority <- unique(
  unlist(
    lapply(
      dependencies,
      function(dep, i){
        if(is.null(dep$priority)){
          i
        } else as.numeric(dep$priority)
      },
      # more robust way of doing but for now just
      i = seq_len(length(dependencies))+100
    )
  )
)

html <- htmltools::attachDependencies(html, dependencies[order(priority,decreasing=FALSE)])
timelyportfolio commented 8 years ago

@cpsievert, I just thought of another option that I implemented in treebar lines. Create a custom *_html function and attach dependencies there. These will be added before the htmlwidget dependencies.

#' @keywords internal
treebar_html <- function(id, style, class, ...){
  htmltools::attachDependencies(
    htmltools::tagList(
      tags$div(id=id, style=style, class=class, ...)
    ),
    d3r::d3_dep_v4()
  )
}