R datatable rowCallback with DT -


i trying perform 2 distinct formatting operations on datatable object using dt , magrittr packages. 1 uses helper function formatround() , other passed in javascript rowcallback option in datatable function.

when run either of formatting operations individually datatable renders expected formatting. however, when both datatable renders blank not error.

this code shows behavior describing.

library(magrittr) library(dt)  df = data.frame(matrix(rnorm(20), nrow=10))  datatable(   data = df ) %>%   formatround(c("x1", "x2"), 1)  #table renders expected  datatable(   data = df,   options = list(     rowcallback = js("      function( row, data, index ) {        if ( index > 2 ) {         $(row).css('background-color', '#ededed');        }        else if ( index > 0 ) {         $(row).css('background-color', '#dedede');        }        else {         $(row).css('background-color', '#d3d3d3');        }      }"     )   ) )  #table renders expected  datatable(   data = df,   options = list(     rowcallback = js("      function( row, data, index ) {       if ( index > 2 ) {         $(row).css('background-color', '#ededed');       }       else if ( index > 0 ) {         $(row).css('background-color', '#dedede');       }       else {         $(row).css('background-color', '#d3d3d3');       }      }"     )   ) ) %>%   formatround(c("x1", "x2"), 1)  #table renders blank no error returned 

if have @ js function of third attempt in browser's js console(click on "inspect element option in browser"), shows error saying 'var' unidentified because outside scope of js function:

( var d = parsefloat(data[1]); $(this.api().cell(row, 1).node()).html(isnan(d) ? '' : d.tofixed(1)); var d = parsefloat(data[2]); $(this.api().cell(row, 2).node()).html(isnan(d) ? '' : d.tofixed(1));      function( row, data, index ) {       if ( index > 2 ) {         $(row).css('background-color', '#ededed');       }       else if ( index > 0 ) {         $(row).css('background-color', '#dedede');       }       else {         $(row).css('background-color', '#d3d3d3');       }      }) 

if put 2 lines inside js function, works perfectly.

you can find updated code below.

datatable(     data = df,     options = list(         rowcallback = js("      function( row, data, index ) {         var d = parsefloat(data[1]);          $(this.api().cell(row, 1).node()).html(isnan(d) ? '' : d.tofixed(1));         var d = parsefloat(data[2]);          $(this.api().cell(row, 2).node()).html(isnan(d) ? '' : d.tofixed(1));        if ( index > 2 ) {         $(row).css('background-color', '#ededed');        }        else if ( index > 0 ) {         $(row).css('background-color', '#dedede');        }        else {         $(row).css('background-color', '#d3d3d3');        }      }"         )     ) ) 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -