R Shiny DataTables: Format numbers not by column but by row

ぃ、小莉子 提交于 2019-12-06 08:39:52

You can use the rowCallback functions:

library(DT)

data <- matrix(c(0,0.64,-0.76234,0.43,1,19),nrow=3,byrow=T)

    datatable(data,options=list(
            rowCallback=JS("function( row, data, index ) {
                           $('td:eq(0)', row).html(data[0] % 1 != 0 | data[0]==0 ? (data[0]*100).toFixed(1) +'%':data[0]);
                           $('td:eq(1)', row).html(data[1] % 1 != 0 | data[1]==0 ? (data[1]*100).toFixed(1) +'%':data[1]);
                           }
                           ")))

This example will multiply the number by 100, round to 1 decimal and add a percent sign if the number has a decimal part or if it is 0. If not, it leaves the number as is.

It is only doing this on the first and second column of the datatable.

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