Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to format as exponential #652

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions R/format.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'))) %>%
Expand Down Expand Up @@ -93,6 +96,15 @@ formatSignif = function(
formatColumns(table, columns, tplSignif, digits, interval, mark, dec.mark)
}


#' @export
#' @rdname formatCurrency
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a brief description of this function on line 18 above.

formatExp = function(
table, columns, digits = 2, interval = 3, mark = ',', dec.mark = getOption('OutDec')
) {
formatColumns(table, columns, tplExp, digits, interval, mark, dec.mark)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the arguments interval, mark, or dec.mark will work for formatExp(). You will have to either drop them or work harder on the markInterval function in JS.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know either, I would sure need to work harder on them, but most likely I will drop them (as I don't know what they do yet). I just copied the code and made a few changes to got it working.

}

#' @export
#' @rdname formatCurrency
#' @param method the method(s) to convert a date to string in JavaScript; see
Expand Down Expand Up @@ -210,6 +222,12 @@ 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
)
}
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)
Expand Down
10 changes: 9 additions & 1 deletion inst/htmlwidgets/datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
if(exponential) {x = exponential ? d.toExponential(digits) : d.toFixed(digits);};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here d.toFixed(digits) will never be executed no matter if exponential is true or false.

if (!/^-?[\d.]+$/.test(x)) return x;
var xv = x.split('.');
if (xv.length > 2) return x; // should have at most one decimal point
Expand Down Expand Up @@ -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;
Expand Down