Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active February 9, 2022 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/b8001318ce3e25b6920a0f20e9db374e to your computer and use it in GitHub Desktop.
Save timelyportfolio/b8001318ce3e25b6920a0f20e9db374e to your computer and use it in GitHub Desktop.
rhansontable tooltips on column headers
library(rhandsontable)
library(htmltools)
browsable(tagList(
rhandsontable(
data = mtcars,
rowHeaders = NULL,
# see http://jsfiddle.net/pn3rv48p/ for another example with afterGetColHeader
afterGetColHeader = htmlwidgets::JS(htmltools::HTML(
sprintf(
"
function(i, TH) {
var titleLookup = %s;
// destroy previous tippy instance if it exists
if(TH.hasOwnProperty('_tippy')) {TH._tippy.destroy()}
// initialize tooltip and set content to description from our titleLookup
tippy(TH, {
content: titleLookup[i].desc,
});
}
",
# use column information from ?mtcars
# sprintf will place this json array of objects in our script above at %s
jsonlite::toJSON(
read.delim(
textConnection('
[, 1] mpg Miles/(US) gallon
[, 2] cyl Number of cylinders
[, 3] disp Displacement (cu.in.)
[, 4] hp Gross horsepower
[, 5] drat Rear axle ratio
[, 6] wt Weight (1000 lbs)
[, 7] qsec 1/4 mile time
[, 8] vs Engine (0 = V-shaped, 1 = straight)
[, 9] am Transmission (0 = automatic, 1 = manual)
[,10] gear Number of forward gears
[,11] carb Number of carburetors'
),
header = FALSE,
col.names = c("loc","id","desc"),
stringsAsFactors = FALSE
),
auto_unbox = TRUE
)
)
))
),
# use tippy/bootstrap since Bootstrap 3 tooltips are awful
# and don't place nicely with handsontable
# better with htmlDependency but this works fine
tags$script(src = "https://unpkg.com/@popperjs/core@2"),
tags$script(src = "https://unpkg.com/tippy.js@6")
))
@timelyportfolio
Copy link
Author

rhandsontable_column_header_tooltip

@dkngenda
Copy link

dkngenda commented Feb 9, 2022

This is really cool. How would we replicate this functionality in an R shiny app?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment