如何在数据表显示中向列标题添加工具提示
output$table <- renderDataTable({
df <- iris
colnames(df) <- sapply(names(df), function(x) abbreviate(x))
df
})
我正在使用的实际数据框(我无法显示)有很长的名称,并且有> 20列。因此,缩写是必要的,以最小的水平滚动显示整个数据框。我正在寻找一种方法来添加悬停在工具提示上,当您将鼠标悬停在缩写列标题上时,该工具提示会显示每列的完整未缩写名称。
答案 0 :(得分:1)
dat <- iris[1:3,]
names(dat) <- c(
"A long name",
"Another long name",
"Yet another long name",
"This name is long as well",
"This one is not short"
)
headerCallback <- c(
"function(thead, data, start, end, display){",
" var ncols = data[0].length;",
sprintf(" var shortnames = [%s]",
paste0(paste0("'",abbreviate(names(dat)),"'"), collapse = ",")),
sprintf(" var tooltips = [%s];",
paste0(paste0("'",names(dat),"'"), collapse = ",")),
" for(var i=0; i<ncols; i++){",
" $('th:eq('+i+')',thead).attr('title', tooltips[i]).text(shortnames[i]);",
" }",
"}"
)
datatable(dat, rownames = FALSE,
options = list(
headerCallback = JS(headerCallback)
)
)