Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio

timelyportfolio/example.R

Last active Jul 4, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@timelyportfolio timelyportfolio commented Jul 4, 2020

rhandsontable_column_header_tooltip

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