Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

r - DT cell hover showing cell based sample sizes from hidden column

I have previously asked how to colour cells based on colours stored in hidden columns (link). I saw that it is also possible to apply hover information for (DT) tables via this and this post.

I want to expand my initial post where I want to add the hover option to display the sample sizes related to the individual cells. These sample sizes are not shown in the table (i.e. hidden) but only display on hover. I am really pushing my knowledge of Java to make this work.

Following on from my initial post the input data frame could look like:

dat <- iris[1:5,1:5]
colours2apply <- sample(x=c(rgb(1, 0, 0 ), rgb(1, 1, 0 ), rgb(0, 1, 1 )), 25, replace = T) %>% 
  matrix(nrow=5) %>% 
  data.frame()
set.seed(1234)
SampleSizesToShowInHover <- matrix(round(runif(n = 25, 10, 1000)), nrow=5)

  dat <- cbind(dat, colours2apply)
  dat <- cbind(dat, SampleSizesToShowInHover)
dat

From the answer in my previous post, this code adds the cell based colouring:

DT <- datatable(dat, 
                options = list(columnDefs = list(list(visible=FALSE, targets = 6:10))))
for(i in 1:5){
  DT <- DT %>%
    formatStyle(i, valueColumns = i+5, backgroundColor = JS("value"))
}
DT

How do I add the cell based hovering information in addition to the colouring? Any help very much appreciated!

Regards,

Luc

EDIT: The final solution I used following the answer, previous posts and various comments was:

solution <- datatable(dat, 
                options = 
                  list(
                    columnDefs = list(
                      list(
                        visible=FALSE, 
                        targets = 6:15
                      )
                    ), 
                    rowCallback = JS(
                      "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                      'for(i=0; i<5; i++ ){',
                      "var full_text = 'n = '+ aData[i+10];",
                      "$('td:eq('+i+')', nRow).attr('title', full_text).css('background-color', aData[i+5]);",
                      '}',
                      "}")


                  )
)
solution
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could simply add a rowcallback to option paramters to get the toopltip from hidden columns. Something like this:

DT <- datatable(dat, 
                options = list(columnDefs = list(list(visible=FALSE, targets = 6:10)), rowCallback = JS(
                  "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                  "$('td:eq(1)', nRow).attr('title',aData[1+5]);",
                  "$('td:eq(2)', nRow).attr('title',aData[2+5]);",
                  "$('td:eq(3)', nRow).attr('title',aData[3+5]);",
                  "$('td:eq(4)', nRow).attr('title',aData[4+5]);",
                  "$('td:eq(5)', nRow).attr('title',aData[6+5]);",
                  "}")))

[EDIT]:

You can do the same thing in loop as follows:

DT <- datatable(dat, 
                options = list(columnDefs = list(list(visible=FALSE, targets = 6:10)), rowCallback = JS(
                  "function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {",
                  'for(i=0; i<5; i++ ){',
                  "$('td:eq('+i+')', nRow).attr('title',aData[i+5]);",
                  '}',
                  "}")))

Hope it helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...