From 6fc64bbaf00ac3556e4eb7f63487939328d8322f Mon Sep 17 00:00:00 2001 From: Giovanni Marques de Castro Date: Fri, 22 Mar 2019 14:03:57 -0300 Subject: [PATCH 1/8] Format exponential Added format exponential function --- inst/htmlwidgets/datatables.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/inst/htmlwidgets/datatables.js b/inst/htmlwidgets/datatables.js index a097c68c..ebe0ba9c 100644 --- a/inst/htmlwidgets/datatables.js +++ b/inst/htmlwidgets/datatables.js @@ -8,8 +8,9 @@ var DTWidget = {}; // 123456666.7890 -> 123,456,666.7890 -var markInterval = function(d, digits, interval, mark, decMark, precision) { +var markInterval = function(d, digits, interval, mark, decMark, precision, exponential) { x = precision ? d.toPrecision(digits) : d.toFixed(digits); + x = exponential ? d.toExponential(digits) : d.toFixed(digits); if (!/^-?[\d.]+$/.test(x)) return x; var xv = x.split('.'); if (xv.length > 2) return x; // should have at most one decimal point @@ -53,6 +54,13 @@ DTWidget.formatSignif = function(thiz, row, data, col, digits, interval, mark, d .html(markInterval(d, digits, interval, mark, decMark, true)); }; +DTWidget.formatExp = function(thiz, row, data, col, digits, interval, mark, decMark) { + var d = parseFloat(data[col]); + if (isNaN(d)) return; + $(thiz.api().cell(row, col).node()) + .html(markInterval(d, digits, interval, mark, decMark, false,true)); +}; + DTWidget.formatDate = function(thiz, row, data, col, method, params) { var d = data[col]; if (d === null) return; From f89d02bb7b9fe14b8af782dfd7381fec1c267b42 Mon Sep 17 00:00:00 2001 From: Giovanni Marques de Castro Date: Fri, 22 Mar 2019 14:10:30 -0300 Subject: [PATCH 2/8] Added formatExp Use the exponential format in a fixed number of digits --- R/format.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/R/format.R b/R/format.R index c77529fb..1939629a 100644 --- a/R/format.R +++ b/R/format.R @@ -93,6 +93,12 @@ formatSignif = function( formatColumns(table, columns, tplSignif, digits, interval, mark, dec.mark) } +formatExp = function( + table, columns, digits = 2, interval = 3, mark = ',', dec.mark = getOption('OutDec') +) { + formatColumns(table, columns, tplExp, digits, interval, mark, dec.mark) +} + #' @export #' @rdname formatCurrency #' @param method the method(s) to convert a date to string in JavaScript; see @@ -210,6 +216,12 @@ tplSignif = function(cols, digits, interval, mark, dec.mark, ...) { ) } +tplSignif = function(cols, digits, interval, mark, dec.mark, ...) { + sprintf( + "DTWidget.formatExp(this, row, data, %d, %d, %d, '%s', '%s');", + cols, digits, interval, mark, dec.mark + ) +} tplDate = function(cols, method, params, ...) { params = if (length(params) > 0) paste(',', toJSON(params)) else '' sprintf("DTWidget.formatDate(this, row, data, %d, '%s'%s);", cols, method, params) From 9301e1ee937c5306d17ce3e88958d9bbd14a5bed Mon Sep 17 00:00:00 2001 From: Giovanni Marques de Castro Date: Fri, 22 Mar 2019 14:51:20 -0300 Subject: [PATCH 3/8] Update format.R --- R/format.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/format.R b/R/format.R index 1939629a..984ad9fc 100644 --- a/R/format.R +++ b/R/format.R @@ -216,7 +216,7 @@ tplSignif = function(cols, digits, interval, mark, dec.mark, ...) { ) } -tplSignif = function(cols, digits, interval, mark, dec.mark, ...) { +tplExp = function(cols, digits, interval, mark, dec.mark, ...) { sprintf( "DTWidget.formatExp(this, row, data, %d, %d, %d, '%s', '%s');", cols, digits, interval, mark, dec.mark From aa7e361195ed56a487d876969766b0115d4dfc0e Mon Sep 17 00:00:00 2001 From: Giovanni Marques de Castro Date: Fri, 22 Mar 2019 14:58:52 -0300 Subject: [PATCH 4/8] Format Exponential added --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index b0d1f015..539465fc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ export(formatDate) export(formatPercentage) export(formatRound) export(formatSignif) +export(formatExp) export(formatString) export(formatStyle) export(hideCols) From 63de49b6ad65fe721945ffb801c7be0f456880a5 Mon Sep 17 00:00:00 2001 From: Giovanni Marques de Castro Date: Fri, 22 Mar 2019 19:13:31 -0300 Subject: [PATCH 5/8] Update datatables.js --- inst/htmlwidgets/datatables.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/htmlwidgets/datatables.js b/inst/htmlwidgets/datatables.js index ebe0ba9c..4a2a0b26 100644 --- a/inst/htmlwidgets/datatables.js +++ b/inst/htmlwidgets/datatables.js @@ -9,8 +9,8 @@ var DTWidget = {}; // 123456666.7890 -> 123,456,666.7890 var markInterval = function(d, digits, interval, mark, decMark, precision, exponential) { + if(exponential) x = exponential ? d.toExponential(digits) : d.toFixed(digits); x = precision ? d.toPrecision(digits) : d.toFixed(digits); - x = exponential ? d.toExponential(digits) : d.toFixed(digits); if (!/^-?[\d.]+$/.test(x)) return x; var xv = x.split('.'); if (xv.length > 2) return x; // should have at most one decimal point From c0c20bb1ba1fd565ef3fbc9e688f6b73e51fa19d Mon Sep 17 00:00:00 2001 From: Giovanni Marques de Castro Date: Fri, 22 Mar 2019 19:19:25 -0300 Subject: [PATCH 6/8] Update datatables.js fix regression in formatSignif --- inst/htmlwidgets/datatables.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/htmlwidgets/datatables.js b/inst/htmlwidgets/datatables.js index 4a2a0b26..7faed17e 100644 --- a/inst/htmlwidgets/datatables.js +++ b/inst/htmlwidgets/datatables.js @@ -9,8 +9,8 @@ var DTWidget = {}; // 123456666.7890 -> 123,456,666.7890 var markInterval = function(d, digits, interval, mark, decMark, precision, exponential) { - if(exponential) x = exponential ? d.toExponential(digits) : d.toFixed(digits); x = precision ? d.toPrecision(digits) : d.toFixed(digits); + if(exponential) {x = exponential ? d.toExponential(digits) : d.toFixed(digits);}; if (!/^-?[\d.]+$/.test(x)) return x; var xv = x.split('.'); if (xv.length > 2) return x; // should have at most one decimal point From fc432e4e40d723832ee796e6d60665ad1755974b Mon Sep 17 00:00:00 2001 From: Giovanni Marques de Castro Date: Thu, 28 Mar 2019 13:13:07 -0300 Subject: [PATCH 7/8] Update NAMESPACE --- NAMESPACE | 1 - 1 file changed, 1 deletion(-) diff --git a/NAMESPACE b/NAMESPACE index 539465fc..b0d1f015 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,7 +16,6 @@ export(formatDate) export(formatPercentage) export(formatRound) export(formatSignif) -export(formatExp) export(formatString) export(formatStyle) export(hideCols) From 08c72d7d681f4efc797aa47c0d9ba2f7bfd6a591 Mon Sep 17 00:00:00 2001 From: Giovanni Marques de Castro Date: Thu, 28 Mar 2019 13:25:10 -0300 Subject: [PATCH 8/8] Update format.R --- R/format.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/R/format.R b/R/format.R index 984ad9fc..c35c5354 100644 --- a/R/format.R +++ b/R/format.R @@ -44,6 +44,9 @@ formatColumns = function(table, columns, template, ...) { #' # render vapor pressure with only two significant figures. #' datatable(pressure) %>% formatSignif('pressure',2) #' +#' # render vapor pressure with only two 2-decimal digits in exponential format. +#' datatable(pressure) %>% formatExp('pressure',2) +#' #' # apply CSS styles to columns #' datatable(iris) %>% #' formatStyle('Sepal.Length', fontWeight = styleInterval(5, c('bold', 'weight'))) %>% @@ -93,6 +96,9 @@ formatSignif = function( formatColumns(table, columns, tplSignif, digits, interval, mark, dec.mark) } + +#' @export +#' @rdname formatCurrency formatExp = function( table, columns, digits = 2, interval = 3, mark = ',', dec.mark = getOption('OutDec') ) {