R Using JavaScript to customize DT tables

自古美人都是妖i 提交于 2019-12-11 18:46:01

问题


I have previously asked how to colour cells based on colours stored in hidden columns (link) and how to get information when hovering a cell (link). I would also like to do some formatting at the same time.

I want to expand my initial posts where I want to

  1. add colour depending on colour specified in the data frame
  2. add the hover option to display the sample sizes related to the individual cells (also in the data frame).
  3. apply number formatting to specified columns

Example data:

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

The final solution to do 1 and 2 at the same time is:

library(DT)
solution12 <- DT::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]);",
                      '}',
                      "}")


                  )
)
solution12

How would integrate JavaScript in order to present the data in columns 3 and 4 as percentages with 1 decimal place, while keeping the solutions to 1 and 2?

Any help much appreciated! Regards, Luc


回答1:


To change the number to percentage we could just multiply the number by 100. To round it to 1 decimal place we can use js function toFixed(1) and then we can just add the percentage sign using + %. To preserve your initial formatting and add the new changes it would be as follows: "$('td:eq('+i+')', nRow).attr('title', full_text).css('background-color', aData[i+5]).html((aData[i]*100).toFixed(1)+ '%');",

The change in code to just add this to column 3 and 4 would be as follows:

  DT::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];",
                                 "if(i === 3 || i=== 4) {",
                                 "$('td:eq('+i+')', nRow).attr('title', full_text).css('background-color', aData[i+5]).html((aData[i]*100).toFixed(1)+ '%');",
                                 "}",
                                 "else{",
                                 "$('td:eq('+i+')', nRow).attr('title', full_text).css('background-color', aData[i+5]);",
                                 "}",
                                  '}',
                                  "}")
                              )
)

With this change the output you get is:

Hope it helps!



来源:https://stackoverflow.com/questions/58296972/r-using-javascript-to-customize-dt-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!