Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sparkline support #64

Closed
bgreenwell opened this issue Feb 18, 2019 · 0 comments
Closed

Add sparkline support #64

bgreenwell opened this issue Feb 18, 2019 · 0 comments

Comments

@bgreenwell
Copy link
Member

@bgreenwell bgreenwell commented Feb 18, 2019

Minimal example:

# Load required packages
library(dplyr)
library(ranger)
library(pdp)
library(vip)
library(sparkline)
library(DT)
library(htmlwidgets)

# Feature importance and feature effects from a random forest fit to the Boston
# housing data
set.seed(101)
fit <- ranger(cmedv ~ ., data = pdp::boston)
vi_scores <- vi(fit, method = "pdp", sort = FALSE)
pdps <- attr(vi_scores, which = "pdp")
# ylim <- range(as.numeric(sapply(pdps, FUN = function(x) range(x$yhat))))

# Prep data for sparklines
vi_scores$Effect <- NA
for (i in seq_len(nrow(vi_scores))) {
  vi_scores[["Effect"]][i] <- 
    paste(pdps[[vi_scores$Variable[i]]][["yhat"]], collapse = ",")
}
vi_scores$Importance <- round(vi_scores$Importance, digits = 3)
vi_scores <- arrange(vi_scores, desc(Importance))

columnDefs = list(list(
  targets = 3,
  render = JS("function(data, type, full){
    return '<span class=spark>' + data + '</span>'           
  }")
))

fnDrawCallback = JS("function (oSettings, json) {
  $('.spark:not(:has(canvas))').sparkline('html', {
    type: 'line',
    highlightColor: 'orange'
  });
}")

d <- datatable(vi_scores, options = list(
  columnDefs = columnDefs,
  fnDrawCallback = fnDrawCallback
))
d$dependencies <- append(d$dependencies, htmlwidgets:::getDependency('sparkline'))
d

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.