FrancoisGuillem / combineWidgets

An R packages to combine htmlwidgets
8 stars 0 forks source link

combineWidgets with a list of htmlwidgets #2

Open timelyportfolio opened 8 years ago

timelyportfolio commented 8 years ago

This is more of a discussion than an issue, so feel free to close whenever.

I think a common use case with combineWidgets will involve a list of htmlwidgets as the ... argument.
For instance, here is a made-up example for reproducibility.

library(combineWidgets)
library(sparkline)

combineWidgets(
  lapply(1:10, function(x){sparkline(runif(50,0,10))})
)

which of course gives us an error since the generally useful pattern list(...) is used to get the ellipsis.

I don't think there is an easy solution for dealing with this in combineWidgets without some troublesome assumptions. For those that run into this issue and are not as familiar with R programming, here are some ways to deal with the "problem".

do.call

do.call is our friend here, so we can do this.

do.call(
  combineWidgets,
  lapply(1:10, function(x){sparkline(runif(50,0,10))})
)

do.call with arguments

When we want to provide other arguments, the solution is a little trickier but not insurmountable.

do.call(
  combineWidgets,
  # note wrap the arguments with c() and not list()
  c(
    lapply(1:10, function(x){sparkline(runif(50,0,10))}),
    nrow = 2,
    ncol = 5
  )
)

Thanks for this really useful widget of widgets!!

FrancoisGuillem commented 8 years ago

Good point ! I haven't thought about it. I think the easiest and more transparent way to do it is to add a parameter list= (like in function save for instance) so the user can directly pass a list of htmlwidgets. What`s your opinion ?