Skip to content

Commit

Permalink
setting numeric columns alignment to dt-right should respect users' o…
Browse files Browse the repository at this point in the history
…ption (#478)

fixes #476
  • Loading branch information
shrektan authored and yihui committed Jan 15, 2018
1 parent f27ce32 commit 713ca43
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions R/datatables.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,15 @@ datatable = function(
}

# align numeric columns to the right
if (length(numc)) options = appendColumnDefs(
options, list(className = 'dt-right', targets = numc - 1)
)
if (length(numc)) {
# if the `className` of the column has already been defined by the user,
# we should not touch it.
defined_cols <- classNameDefinedColumns(options)
undefined_numc <- (numc - 1)[!(numc - 1) %in% defined_cols]
if (length(undefined_numc)) options = appendColumnDefs(
options, list(className = 'dt-right', targets = undefined_numc)
)
}

# make sure the table is _not_ ordered by default (change the DataTables default)
if (is.null(options[['order']])) options$order = list()
Expand Down Expand Up @@ -275,6 +281,17 @@ appendColumnDefs = function(options, def) {
options
}

classNameDefinedColumns = function(options) {
defs = options[['columnDefs']]
if (is.null(defs) || length(defs) == 0L) return(integer())
colDefinedTgts <- function(init, x) {
if (!is.null(x[['className']])) return(c(init, x[['targets']]))
init
}
unique(Reduce(colDefinedTgts, defs, integer()))
}


# convert character indices to numeric
convertIdx = function(i, names, n = length(names), invert = FALSE) {
if (!is.character(i)) return({
Expand Down

0 comments on commit 713ca43

Please sign in to comment.