Title: | A Wrapper of the JavaScript Library 'DataTables' |
---|---|
Description: | Data objects in R can be rendered as HTML tables using the JavaScript library 'DataTables' (typically via R Markdown or Shiny). The 'DataTables' library has been included in this R package. The package name 'DT' is an abbreviation of 'DataTables'. |
Authors: | Yihui Xie [aut], Joe Cheng [aut, cre], Xianying Tan [aut], JJ Allaire [ctb], Maximilian Girlich [ctb], Greg Freedman Ellis [ctb], Johannes Rauh [ctb], SpryMedia Limited [ctb, cph] (DataTables in htmlwidgets/lib), Brian Reavis [ctb, cph] (selectize.js in htmlwidgets/lib), Leon Gersen [ctb, cph] (noUiSlider in htmlwidgets/lib), Bartek Szopka [ctb, cph] (jquery.highlight.js in htmlwidgets/lib), Alex Pickering [ctb], William Holmes [ctb], Mikko Marttila [ctb], Andres Quintero [ctb], Stéphane Laurent [ctb], Posit Software, PBC [cph, fnd] |
Maintainer: | Joe Cheng <[email protected]> |
License: | GPL-3 | file LICENSE |
Version: | 0.33.2 |
Built: | 2024-11-25 23:21:36 UTC |
Source: | https://github.com/rstudio/DT |
Create a new value from a character string based on an old value, e.g., if
the old value is an integer, call as.integer()
to coerce the string to
an integer.
coerceValue(val, old)
coerceValue(val, old)
val |
A character string. |
old |
An old value, whose type is the target type of |
This function only works with integer, double, date, time (POSIXlt
or
POSIXct
), and factor values. The date must be of the format
%Y-%m-%dT%H:%M:%SZ
. The factor value must be in the levels of
old
, otherwise it will be coerced to NA
.
A value of the same data type as old
if possible.
library(DT) coerceValue("100", 1L) coerceValue("1.23", 3.1416) coerceValue("2018-02-14", Sys.Date()) coerceValue("2018-02-14T22:18:52Z", Sys.time()) coerceValue("setosa", iris$Species) coerceValue("setosa2", iris$Species) # NA coerceValue("FALSE", TRUE) # not supported
library(DT) coerceValue("100", 1L) coerceValue("1.23", 3.1416) coerceValue("2018-02-14", Sys.Date()) coerceValue("2018-02-14T22:18:52Z", Sys.time()) coerceValue("setosa", iris$Species) coerceValue("setosa2", iris$Species) # NA coerceValue("FALSE", TRUE) # not supported
This function creates an HTML widget to display rectangular data (a matrix or data frame) using the JavaScript library DataTables.
datatable( data, options = list(), class = "display", callback = JS("return table;"), rownames, colnames, container, caption = NULL, filter = c("none", "bottom", "top"), escape = TRUE, style = "auto", width = NULL, height = NULL, elementId = NULL, fillContainer = getOption("DT.fillContainer", NULL), autoHideNavigation = getOption("DT.autoHideNavigation", NULL), lazyRender = NULL, selection = c("multiple", "single", "none"), extensions = list(), plugins = NULL, editable = FALSE )
datatable( data, options = list(), class = "display", callback = JS("return table;"), rownames, colnames, container, caption = NULL, filter = c("none", "bottom", "top"), escape = TRUE, style = "auto", width = NULL, height = NULL, elementId = NULL, fillContainer = getOption("DT.fillContainer", NULL), autoHideNavigation = getOption("DT.autoHideNavigation", NULL), lazyRender = NULL, selection = c("multiple", "single", "none"), extensions = list(), plugins = NULL, editable = FALSE )
data |
a data object (either a matrix or a data frame) |
options |
a list of initialization options (see
https://datatables.net/reference/option/); the character options
wrapped in |
class |
the CSS class(es) of the table; see https://datatables.net/manual/styling/classes |
callback |
the body of a JavaScript callback function with the argument
|
rownames |
|
colnames |
if missing, the column names of the data; otherwise it can be
an unnamed character vector of names you want to show in the table header
instead of the default data column names; alternatively, you can provide a
named numeric or character vector of the form |
container |
a sketch of the HTML table to be filled with data cells; by
default, it is generated from |
caption |
the table caption; a character vector or a tag object
generated from |
filter |
whether/where to use column filters; |
escape |
whether to escape HTML entities in the table: |
style |
either |
width , height
|
Width/Height in pixels (optional, defaults to automatic sizing) |
elementId |
An id for the widget (a random string by default). |
fillContainer |
|
autoHideNavigation |
|
lazyRender |
|
selection |
the row/column selection mode (single or multiple selection
or disable selection) when a table widget is rendered in a Shiny app;
alternatively, you can use a list of the form |
extensions |
a character vector of the names of the DataTables extensions (https://datatables.net/extensions/index) |
plugins |
a character vector of the names of DataTables plug-ins
(https://rstudio.github.io/DT/plugins.html). Note that only those
plugins supported by the |
editable |
|
selection
:
The argument could be a scalar string, which means the selection
mode
, whose value could be one of 'multiple'
(the default),
'single'
and 'none'
(disable selection).
When a list form is provided for this argument, only parts of the
"full" list are allowed. The default values for non-matched elements are
list(mode = 'multiple', selected = NULL, target = 'row',
selectable = NULL)
.
target
must be one of 'row'
, 'column'
,
'row+column'
and 'cell'
.
selected
could be NULL
or "indices".
selectable
could be NULL
, TRUE
, FALSE
or "indices", where NULL
and TRUE
mean all the table is
selectable. When FALSE
, it means users can't select the table
by the cursor (but they could still be able to select the table via
dataTableProxy
, specifying ignore.selectable = TRUE
).
If "indices", they must be all positive or non-positive values. All
positive "indices" mean only the specified ranges are selectable while all
non-positive "indices" mean those ranges are not selectable.
The "indices"' format is specified below.
The "indices"' format of selected
and selectable
:
when target
is 'row'
or 'column'
, it should be a plain
numeric vector; when target
is 'row+column'
, it should be a
list, specifying rows
and cols
respectively, e.g.,
list(rows = 1, cols = 2)
; when target
is 'cell'
,
it should be a 2-col matrix
, where the two values of each row
stand for the row and column index.
Note that DT has its own selection implementation and doesn't
use the Select extension because the latter doesn't support the
server-side processing mode well. Please set this argument to
'none'
if you really want to use the Select extension.
options$columnDefs
:
columnDefs
is an option that provided by the DataTables library
itself, where the user can set various attributes for columns. It must be
provided as a list of list, where each sub-list must contain a vector named 'targets',
specifying the applied columns, i.e.,
list(list(..., targets = '_all'), list(..., targets = c(1, 2)))
columnDefs$targets
is a vector and should be one of:
0 or a positive integer: column index counting from the left.
A negative integer: column index counting from the right.
A string: the column name. Note, it must be the names of the
original data, not the ones that (could) be changed via param colnames
.
The string "_all": all columns (i.e. assign a default).
When conflicts happen, e.g., a single column is defined for some property
twice but with different values, the value that defined earlier takes the priority.
For example, list(list(visible=FALSE, target=1), list(visible=TRUE, target=1))
results in a table whose first column is invisible.
See https://datatables.net/reference/option/columnDefs for more.
filter
:
filter
can be used to position and customize column filters.
A scalar string value defines the position, and must be one of 'none'
(the default), 'bottom'
and 'top'
. A named list can be used
for further control. In the named list form:
$position
is a string as described above. It defaults to 'none'
.
$clear
is a logical value indicating if clear
buttons should appear in input boxes. It defaults to TRUE
.
$plain
is a logical value indicating if plain styling
should be used for input boxes instead of Bootstrap styling. It
defaults to FALSE
.
$vertical
is a logical value indicating if slider
widgets should be oriented vertically rather than horizontally.
It defaults to FALSE
.
$opacity
is a numeric value between 0 and 1 used to set
the level of transparency of slider widgets. It defaults to 1
.
$settings
is a named list used to directly pass configuration
for initializing filter widgets in JavaScript.
The $select
element is passed to the select widget, and
$slider
is passed to the slider widget.
Valid values depend on the settings accepted by the underlying JavaScript libraries, Selectize and noUiSlider. Please note that the versions bundled with DT are currently quite old, so accepted settings may not match their most recent documentation.
These settings can override values set by DT, so specifying a setting already in use may break something. Use with care.
You are recommended to escape the table content for security reasons (e.g. XSS attacks) when using this function in Shiny or any other dynamic web applications.
See https://rstudio.github.io/DT/ for the full documentation.
library(DT) # see the package vignette for examples and the link to website vignette('DT', package = 'DT') # some boring edge cases for testing purposes m = matrix(nrow = 0, ncol = 5, dimnames = list(NULL, letters[1:5])) datatable(m) # zero rows datatable(as.data.frame(m)) m = matrix(1, dimnames = list(NULL, 'a')) datatable(m) # one row and one column datatable(as.data.frame(m)) m = data.frame(a = 1, b = 2, c = 3) datatable(m) datatable(as.matrix(m)) # dates datatable(data.frame( date = seq(as.Date("2015-01-01"), by = "day", length.out = 5), x = 1:5 )) datatable(data.frame(x = Sys.Date())) datatable(data.frame(x = Sys.time()))
library(DT) # see the package vignette for examples and the link to website vignette('DT', package = 'DT') # some boring edge cases for testing purposes m = matrix(nrow = 0, ncol = 5, dimnames = list(NULL, letters[1:5])) datatable(m) # zero rows datatable(as.data.frame(m)) m = matrix(1, dimnames = list(NULL, 'a')) datatable(m) # one row and one column datatable(as.data.frame(m)) m = data.frame(a = 1, b = 2, c = 3) datatable(m) datatable(as.matrix(m)) # dates datatable(data.frame( date = seq(as.Date("2015-01-01"), by = "day", length.out = 5), x = 1:5 )) datatable(data.frame(x = Sys.Date())) datatable(data.frame(x = Sys.time()))
This function stores a data object in a shiny session and returns a URL that
returns JSON data based on DataTables Ajax requests. The URL can be used as
the url
option inside the ajax
option of the table. It is
basically an implementation of server-side processing of DataTables in R.
Filtering, sorting, and pagination are processed through R instead of
JavaScript (client-side processing).
dataTableAjax( session, data, rownames, filter = dataTablesFilter, outputId, future = FALSE )
dataTableAjax( session, data, rownames, filter = dataTablesFilter, outputId, future = FALSE )
session |
the |
data |
a data object (will be coerced to a data frame internally) |
rownames |
see |
filter |
(for expert use only) a function with two arguments |
outputId |
the output ID of the table (the same ID passed to
|
future |
whether the server-side filter function should be executed as a future or as a standard synchronous function. If true, the future will be evaluated according to the session's plan. |
Normally you should not need to call this function directly. It is called
internally when a table widget is rendered in a Shiny app to configure the
table option ajax
automatically. If you are familiar with
DataTables' server-side processing, and want to use a custom filter
function, you may call this function to get an Ajax URL.
A character string (an Ajax URL that can be queried by DataTables).
https://rstudio.github.io/DT/server.html
DTApp = function(data, ..., options = list()) { library(shiny) library(DT) shinyApp( ui = fluidPage( title = 'Server-side processing of DataTables', fluidRow( DT::dataTableOutput('tbl') ) ), server = function(input, output, session) { options$serverSide = TRUE options$ajax = list(url = dataTableAjax(session, data, outputId = 'tbl')) # create a widget using an Ajax URL created above widget = datatable(data, ..., options = options) output$tbl = DT::renderDataTable(widget) } ) } if (interactive()) DTApp(iris) if (interactive()) DTApp(iris, filter = 'top')
DTApp = function(data, ..., options = list()) { library(shiny) library(DT) shinyApp( ui = fluidPage( title = 'Server-side processing of DataTables', fluidRow( DT::dataTableOutput('tbl') ) ), server = function(input, output, session) { options$serverSide = TRUE options$ajax = list(url = dataTableAjax(session, data, outputId = 'tbl')) # create a widget using an Ajax URL created above widget = datatable(data, ..., options = options) output$tbl = DT::renderDataTable(widget) } ) } if (interactive()) DTApp(iris) if (interactive()) DTApp(iris, filter = 'top')
These two functions are like most fooOutput()
and renderFoo()
functions in the shiny package. The former is used to create a
container for table, and the latter is used in the server logic to render the
table.
dataTableOutput(outputId, width = "100%", height = "auto", fill = TRUE) DTOutput(outputId, width = "100%", height = "auto", fill = TRUE) renderDataTable( expr, server = TRUE, env = parent.frame(), quoted = FALSE, funcFilter = dataTablesFilter, future = FALSE, outputArgs = list(), ... ) renderDT( expr, server = TRUE, env = parent.frame(), quoted = FALSE, funcFilter = dataTablesFilter, future = FALSE, outputArgs = list(), ... )
dataTableOutput(outputId, width = "100%", height = "auto", fill = TRUE) DTOutput(outputId, width = "100%", height = "auto", fill = TRUE) renderDataTable( expr, server = TRUE, env = parent.frame(), quoted = FALSE, funcFilter = dataTablesFilter, future = FALSE, outputArgs = list(), ... ) renderDT( expr, server = TRUE, env = parent.frame(), quoted = FALSE, funcFilter = dataTablesFilter, future = FALSE, outputArgs = list(), ... )
outputId |
output variable to read the table from |
width |
the width of the table container |
height |
the height of the table container |
fill |
passed to |
expr |
an expression to create a table widget (normally via
|
server |
whether to use server-side processing. If |
env |
The parent environment for the reactive expression. By default,
this is the calling environment, the same as when defining an ordinary
non-reactive expression. If |
quoted |
If it is |
funcFilter |
(for expert use only) passed to the |
future |
whether the server-side filter function should be executed as a future or as a standard synchronous function. If true, the future will be evaluated according to the session's plan. |
outputArgs |
A list of arguments to be passed through to the implicit
call to |
... |
ignored when |
https://rstudio.github.io/DT/shiny.html
if (interactive()) { library(shiny) library(DT) shinyApp( ui = fluidPage(fluidRow(column(12, DTOutput('tbl')))), server = function(input, output) { output$tbl = renderDT( iris, options = list(lengthChange = FALSE) ) } ) }
if (interactive()) { library(shiny) library(DT) shinyApp( ui = fluidPage(fluidRow(column(12, DTOutput('tbl')))), server = function(input, output) { output$tbl = renderDT( iris, options = list(lengthChange = FALSE) ) } ) }
The function dataTableProxy()
creates a proxy object that can be used
to manipulate an existing DataTables instance in a Shiny app, e.g. select
rows/columns, or add rows.
dataTableProxy( outputId, session = shiny::getDefaultReactiveDomain(), deferUntilFlush = TRUE ) selectRows(proxy, selected, ignore.selectable = FALSE) selectColumns(proxy, selected, ignore.selectable = FALSE) selectCells(proxy, selected, ignore.selectable = FALSE) addRow(proxy, data, resetPaging = TRUE) clearSearch(proxy) selectPage(proxy, page) updateCaption(proxy, caption) updateSearch(proxy, keywords = list(global = NULL, columns = NULL)) showCols(proxy, show, reset = FALSE) hideCols(proxy, hide, reset = FALSE) colReorder(proxy, order, origOrder = FALSE) reloadData( proxy, resetPaging = TRUE, clearSelection = c("all", "none", "row", "column", "cell") )
dataTableProxy( outputId, session = shiny::getDefaultReactiveDomain(), deferUntilFlush = TRUE ) selectRows(proxy, selected, ignore.selectable = FALSE) selectColumns(proxy, selected, ignore.selectable = FALSE) selectCells(proxy, selected, ignore.selectable = FALSE) addRow(proxy, data, resetPaging = TRUE) clearSearch(proxy) selectPage(proxy, page) updateCaption(proxy, caption) updateSearch(proxy, keywords = list(global = NULL, columns = NULL)) showCols(proxy, show, reset = FALSE) hideCols(proxy, hide, reset = FALSE) colReorder(proxy, order, origOrder = FALSE) reloadData( proxy, resetPaging = TRUE, clearSelection = c("all", "none", "row", "column", "cell") )
outputId |
the id of the table to be manipulated (the same id as the one
you used in |
session |
the Shiny session object (from the server function of the Shiny app) |
deferUntilFlush |
whether an action should be carried out right away, or should be held until after the next time all of the outputs are updated |
proxy |
a proxy object returned by |
selected |
an integer vector of row/column indices, or a matrix of two
columns (row and column indices, respectively) for cell indices; you may
use |
ignore.selectable |
when |
data |
a single row of data to be added to the table; it can be a matrix
or data frame of one row, or a vector or list of row data (in the latter
case, please be cautious about the row name: if your table contains row
names, here |
resetPaging |
whether to reset the paging position |
page |
a number indicating the page to select |
caption |
a new table caption (see the |
keywords |
a list of two components: |
show |
a vector of column positions to show (the indexing starts at 0, but if row.names are visible, they are the first column). |
reset |
if |
hide |
a vector of column positions to hide |
order |
A numeric vector of column positions, starting from 0, and including the row.names as a column, if they are include. Must contain a value for all columns, regardless of whether they are visible or not. Also for column reordering to work, the datatable must have extension 'ColReorder' set as well as option 'colReordoer' set to TRUE). |
origOrder |
Whether column reordering should be relative to the original order (the default is to compare to current order) |
clearSelection |
which existing selections to clear: it can be any
combinations of |
addRow()
only works for client-side tables. If you want to use
it in a Shiny app, make sure to use renderDataTable(..., server =
FALSE)
. Also note that the column filters (if used) of the table will not
be automatically updated when a new row is added, e.g., the range of the
slider of a column will stay the same even if you have added a value
outside the range of the original data column.
reloadData()
only works for tables in the server-side processing
mode, e.g. tables rendered with renderDataTable(server = TRUE)
. The
data to be reloaded (i.e. the one you pass to dataTableAjax()
) must
have exactly the same number of columns as the previous data object in the
table.
https://rstudio.github.io/DT/shiny.html
doGlobalSearch()
can be used to search a data frame given the search
string typed by the user into the global search box of a
datatable
. doColumnSearch()
does the same for a vector
given the search string typed into a column filter. These functions are used
internally by the default filter
function passed to
dataTableAjax()
, allowing you to replicate the search results
that server-side processing returns.
doColumnSearch(x, search_string, options = list()) doGlobalSearch(data, search_string, options = list())
doColumnSearch(x, search_string, options = list()) doGlobalSearch(data, search_string, options = list())
x |
a vector, the type of which determines the expected
|
search_string |
a string that determines what to search for. The format depends on the type of input, matching what a user would type in the associated filter control. |
options |
a list of options used to control how searching character
values works. Supported options are |
data |
a data frame |
An integer vector of filtered row indices
The column filters section online for search string formats: https://rstudio.github.io/DT/
Accessing the search strings typed by a user in a Shiny app: https://rstudio.github.io/DT/shiny.html
doGlobalSearch(iris, "versi") doGlobalSearch(iris, "v.r.i", options = list(regex = TRUE)) doColumnSearch(iris$Species, "[\"versicolor\"]") doColumnSearch(iris$Sepal.Length, "4 ... 5")
doGlobalSearch(iris, "versi") doGlobalSearch(iris, "v.r.i", options = list(regex = TRUE)) doColumnSearch(iris$Species, "[\"versicolor\"]") doColumnSearch(iris$Sepal.Length, "4 ... 5")
These objects are imported from other packages. Follow the links to their documentation.
When editing cells in a DataTable in a Shiny app, we know the row/column indices and values of the cells that were edited. With these information, we can update the data object behind the DataTable accordingly.
editData(data, info, proxy = NULL, rownames = TRUE, resetPaging = FALSE, ...)
editData(data, info, proxy = NULL, rownames = TRUE, resetPaging = FALSE, ...)
data |
The original data object used in the DataTable. |
info |
The information about the edited cells. It should be obtained
from |
proxy , resetPaging , ...
|
(Optional) If |
rownames |
Whether row names are displayed in the table. |
The updated data object.
For factor columns, new levels would be automatically added when necessary
to avoid NA
coercing.
Format numeric columns in a table as currency (formatCurrency()
) or
percentages (formatPercentage()
), or round numbers to a specified
number of decimal places (formatRound()
), or a specified number
of significant figures (formatSignif()
). The function
formatStyle()
applies CSS styles to table cells by column.
formatCurrency( table, columns, currency = "$", interval = 3, mark = ",", digits = 2, dec.mark = getOption("OutDec"), before = TRUE, zero.print = NULL, rows = NULL ) formatString(table, columns, prefix = "", suffix = "", rows = NULL) formatPercentage( table, columns, digits = 0, interval = 3, mark = ",", dec.mark = getOption("OutDec"), zero.print = NULL, rows = NULL ) formatRound( table, columns, digits = 2, interval = 3, mark = ",", dec.mark = getOption("OutDec"), zero.print = NULL, rows = NULL ) formatSignif( table, columns, digits = 2, interval = 3, mark = ",", dec.mark = getOption("OutDec"), zero.print = NULL, rows = NULL ) formatDate(table, columns, method = "toDateString", params = NULL, rows = NULL) formatStyle( table, columns, valueColumns = columns, target = c("cell", "row"), fontWeight = NULL, color = NULL, backgroundColor = NULL, background = NULL, ... )
formatCurrency( table, columns, currency = "$", interval = 3, mark = ",", digits = 2, dec.mark = getOption("OutDec"), before = TRUE, zero.print = NULL, rows = NULL ) formatString(table, columns, prefix = "", suffix = "", rows = NULL) formatPercentage( table, columns, digits = 0, interval = 3, mark = ",", dec.mark = getOption("OutDec"), zero.print = NULL, rows = NULL ) formatRound( table, columns, digits = 2, interval = 3, mark = ",", dec.mark = getOption("OutDec"), zero.print = NULL, rows = NULL ) formatSignif( table, columns, digits = 2, interval = 3, mark = ",", dec.mark = getOption("OutDec"), zero.print = NULL, rows = NULL ) formatDate(table, columns, method = "toDateString", params = NULL, rows = NULL) formatStyle( table, columns, valueColumns = columns, target = c("cell", "row"), fontWeight = NULL, color = NULL, backgroundColor = NULL, background = NULL, ... )
table |
a table object created from |
columns |
the indices of the columns to be formatted (can be character,
numeric, logical, or a formula of the form |
currency |
the currency symbol |
interval |
put a marker after how many digits of the numbers |
mark |
the marker after every |
digits |
the number of decimal places to round to |
dec.mark |
a character to indicate the decimal point |
before |
whether to place the currency symbol before or after the values |
zero.print |
a string to specify how zeros should be formatted.
Useful for when many zero values exist. If |
rows |
an integer vector (starting from 1) to specify the only rows
that the style applies to.
By default, it's |
prefix |
string to put in front of the column values |
suffix |
string to put after the column values |
method |
the method(s) to convert a date to string in JavaScript; see
|
params |
a list parameters for the specific date conversion method,
e.g., for the |
valueColumns |
indices of the columns from which the cell values are
obtained; this can be different with the |
target |
the target to apply the CSS styles to (the current cell or the full row) |
fontWeight |
the font weight, e.g. |
color |
the font color, e.g. |
backgroundColor |
the background color of table cells |
background |
the background of table cells |
... |
other CSS properties, e.g. |
The length of arguments other than table
should be 1 or the same as
the length of columns
.
See https://rstudio.github.io/DT/functions.html for detailed documentation and examples.
library(DT) m = cbind(matrix(rnorm(120, 1e5, 1e6), 40), runif(40), rnorm(40, 100)) colnames(m) = head(LETTERS, ncol(m)) m # format the columns A and C as currency, and D as percentages datatable(m) %>% formatCurrency(c('A', 'C')) %>% formatPercentage('D', 2) # the first two columns are Euro currency, and round column E to 3 decimal places datatable(m) %>% formatCurrency(1:2, '\U20AC') %>% formatRound('E', 3) # render vapor pressure with only two significant figures. datatable(pressure) %>% formatSignif('pressure',2) # apply CSS styles to columns datatable(iris) %>% formatStyle('Sepal.Length', fontWeight = styleInterval(5, c('bold', 'weight'))) %>% formatStyle('Sepal.Width', color = styleInterval(3.4, c('red', 'white')), backgroundColor = styleInterval(3.4, c('yellow', 'gray')) )
library(DT) m = cbind(matrix(rnorm(120, 1e5, 1e6), 40), runif(40), rnorm(40, 100)) colnames(m) = head(LETTERS, ncol(m)) m # format the columns A and C as currency, and D as percentages datatable(m) %>% formatCurrency(c('A', 'C')) %>% formatPercentage('D', 2) # the first two columns are Euro currency, and round column E to 3 decimal places datatable(m) %>% formatCurrency(1:2, '\U20AC') %>% formatRound('E', 3) # render vapor pressure with only two significant figures. datatable(pressure) %>% formatSignif('pressure',2) # apply CSS styles to columns datatable(iris) %>% formatStyle('Sepal.Length', fontWeight = styleInterval(5, c('bold', 'weight'))) %>% formatStyle('Sepal.Width', color = styleInterval(3.4, c('red', 'white')), backgroundColor = styleInterval(3.4, c('yellow', 'gray')) )
Replace the data object of a table output and avoid regenerating the full table, in which case the state of the current table will be preserved (sorting, filtering, and pagination) and applied to the table with new data.
replaceData(proxy, data, ..., resetPaging = TRUE, clearSelection = "all") updateFilters(proxy, data)
replaceData(proxy, data, ..., resetPaging = TRUE, clearSelection = "all") updateFilters(proxy, data)
proxy |
a proxy object created by |
data |
the new data object to be loaded in the table |
... |
other arguments to be passed to |
resetPaging , clearSelection
|
passed to |
When you replace the data in an existing table, please make sure the
new data has the same number of columns as the current data. When you have
enabled column filters, you should also make sure the attributes of every
column remain the same, e.g. factor columns should have the same or fewer
levels, and numeric columns should have the same or smaller range,
otherwise the filters may never be able to reach certain rows in the data,
unless you explicitly update the filters with updateFilters()
.
If the ColReorder
extension is used, the new data
must have
column names that match the original data column names exactly.
A few helper functions for the formatStyle()
function to
calculate CSS styles for table cells based on the cell values. Under the
hood, they just generate JavaScript and CSS code from the values specified in
R.
styleInterval(cuts, values) styleEqual(levels, values, default = NULL) styleValue() styleColorBar(data, color, angle = 90) styleRow(rows, values, default = NULL)
styleInterval(cuts, values) styleEqual(levels, values, default = NULL) styleValue() styleColorBar(data, color, angle = 90) styleRow(rows, values, default = NULL)
cuts |
a vector of cut points (sorted increasingly) |
values |
a vector of CSS values |
levels |
a character vector of data values to be mapped (one-to-one) to CSS values |
default |
a string or |
data |
a numeric vector whose range will be used for scaling the table data from 0-100 before being represented as color bars. A vector of length 2 is acceptable here for specifying a range possibly wider or narrower than the range of the table data itself. |
color |
the color of the bars |
angle |
a number of degrees representing the direction to fill the gradient relative to a horizontal line and the gradient line, going counter-clockwise. For example, 90 fills right to left and -90 fills left to right. |
rows |
the Row Indexes (starting from 1) that applies the CSS style. It could
be an integer vector or a list of integer vectors, whose length must be equal to
the length of |
The function styleInterval()
maps intervals to CSS values. Its
argument values
must be of length n + 1
where n =
length(cuts)
. The right-closed interval ‘(cuts[i - 1], cuts[i]]’ is
mapped to ‘values[i]’ for ‘i = 2, 3, ..., n’; ‘values[1]’ is
for the interval ‘(-Inf, cuts[1]]’, and ‘values[n + 1]’ is for
‘(cuts[n], +Inf)’. You can think of the order of cuts
and
values
using this diagram: ‘-Inf -> values[1] -> cuts[1] ->
values[2] -> cuts[2] -> ... -> values[n] -> cuts[n] -> values[n + 1] ->
+Inf’.
The function styleEqual()
maps data values to CSS values in the
one-to-one manner, i.e. values[i]
is used when the table cell value is
levels[i]
.
The function styleColorBar()
can be used to draw background color bars
behind table cells in a column, and the width of bars is proportional to the
column values.
The function styleValue()
uses the column value as the CSS values.
The function styleRow()
applies the CSS values based on Row Indexes.
This only works expected in the client-side processing mode, i.e., server = FALSE
.
Convenience functions to generate a table header (‘<thead></thead>’) or
footer (‘<tfoot></tfoot>’) given the column names. They are basically
wrappers of htmltools::tags$th
applied to the column names.
tableHeader(names, escape = TRUE) tableFooter(names, escape = TRUE)
tableHeader(names, escape = TRUE) tableFooter(names, escape = TRUE)
names |
a character vector of the column names of the table (if it is an object with column names, its column names will be used instead) |
escape |
whether to escape the names (see |
A tag object generated by htmltools::tags
.
library(DT) tableHeader(iris) # or equivalently, tableHeader(colnames(iris)) tableFooter(iris) # footer library(htmltools) tags$table(tableHeader(iris), tableFooter(iris))
library(DT) tableHeader(iris) # or equivalently, tableHeader(colnames(iris)) tableFooter(iris) # footer library(htmltools) tags$table(tableHeader(iris), tableFooter(iris))