htmlwidgets / sparkline

jQuery Sparkline HTML Widget for R
Other
244 stars 40 forks source link

Add helper function for as.character(as.tags(sparkline())) #18

Closed timelyportfolio closed 7 years ago

timelyportfolio commented 7 years ago

In nearly all of the examples in which I combine sparkline with dplyr, I used the pattern

as.character( as.tags( sparkline(1:10) ) )

It seems like we could provide a helper function spk_chr (unsure of name but think it should be short for easy typing) that accepts the same arguments as sparkline() but returns character instead of an htmlwidget. I think this usage will become more popular with formattable and DT especially considering RStudio's new notebooks.

Thoughts??

timelyportfolio commented 7 years ago

Here is a very ugly sketch.

spk_chr <- function(
  values, ..., width = 60, height = 20,
  elementId = NULL, renderSelector = NULL
){
  if(inherits(values, "sparkline")) {
    spk <- values
  } else {
    spk <- sparkline(
      values = values,
      width = width,
      height = height,
      elementId = elementId,
      renderSelector = renderSelector,
      ...
    )
  }

  as.character(as.tags(spk))
}