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,
...
)a table object created from datatable()
the indices of the columns to be formatted (can be character,
numeric, logical, or a formula of the form ~ V1 + V2, which is
equivalent to c('V1', 'V2'))
the currency symbol
put a marker after how many digits of the numbers
the marker after every interval decimals in the numbers
the number of decimal places to round to
a character to indicate the decimal point
whether to place the currency symbol before or after the values
a string to specify how zeros should be formatted.
Useful for when many zero values exist. If NULL, keeps zero as it is.
an integer vector (starting from 1) to specify the only rows
that the style applies to.
By default, it's NULL, meaning all rows should be formatted. Note,
formatStyle() doesn't support this argument and you should use
styleRow() instead. In addition, this only works expected in the
client-side processing mode, i.e., server = FALSE.
string to put in front of the column values
string to put after the column values
the method(s) to convert a date to string in JavaScript; see
DT:::DateMethods for a list of possible methods, and
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
for a full reference
a list parameters for the specific date conversion method,
e.g., for the toLocaleDateString() method, your browser may support
params = list('ko-KR', list(year = 'numeric', month = 'long', day =
'numeric'))
indices of the columns from which the cell values are
obtained; this can be different with the columns argument, e.g. you
may style one column based on the values of a different column
the target to apply the CSS styles to (the current cell or the full row)
the font weight, e.g. 'bold' and 'normal'
the font color, e.g. 'red' and '#ee00aa'
the background color of table cells
the background of table cells
other CSS properties, e.g. 'border', 'font-size',
'text-align', and so on; if you want to condition CSS styles on the
cell values, you may use the helper functions such as
styleInterval(); note the actual CSS property names are
dash-separated, but you can use camelCase names in this function (otherwise
you will have to use backticks to quote the names, e.g. `font-size` =
'12px'), and this function will automatically convert camelCase names to
dash-separated names (e.g. 'fontWeight' will be converted to
'font-weight' internally)
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
#> A B C D E
#> [1,] 368959.05 -683173.11 -57428.23 0.64787446 100.72578
#> [2,] 1029525.36 -155994.58 -96081.86 0.39488555 100.53505
#> [3,] 1192447.07 619282.01 983182.40 0.41637979 99.62463
#> [4,] -395829.33 71761.72 2777548.85 0.09311957 99.77677
#> [5,] -2016530.42 1671173.81 -457619.39 0.76997796 101.03371
#> [6,] 2154462.92 501504.77 -2910768.81 0.57676003 100.39276
#> [7,] 1113549.30 -326721.32 1370879.23 0.49185984 101.32750
#> [8,] 1227673.67 -871210.70 1014035.98 0.12828847 100.27647
#> [9,] -247446.52 -205869.95 858050.75 0.88551558 102.07952
#> [10,] -813657.80 -806784.48 -767739.04 0.38562083 101.13613
#> [11,] 1130069.30 550790.38 -86465.30 0.86747678 99.19595
#> [12,] -1002158.96 776048.52 447847.30 0.43275009 99.72996
#> [13,] 176648.32 777404.88 -225451.85 0.90660980 100.33746
#> [14,] 1700360.64 482383.37 -39796.67 0.02104626 101.49409
#> [15,] 1432550.39 -664084.75 147969.26 0.66837035 100.79890
#> [16,] -326771.28 1127561.56 -1218844.31 0.41564122 99.16778
#> [17,] 227181.44 -318266.76 -600118.28 0.02215304 99.83031
#> [18,] -305623.65 588580.50 61074.84 0.53456861 99.15995
#> [19,] -1718234.85 -1366632.19 -1060915.84 0.92024065 99.72687
#> [20,] -617902.94 817519.53 -1203350.66 0.25976364 98.84349
#> [21,] -768389.50 1060801.24 -208718.89 0.49276730 99.12248
#> [22,] 443586.12 -2124454.77 445836.63 0.67073629 98.11426
#> [23,] -86014.60 -870877.84 -981236.12 0.20069721 100.44262
#> [24,] 403529.28 152635.72 499569.89 0.99149699 98.10191
#> [25,] 40215.04 -829149.50 -578597.98 0.60639090 100.69620
#> [26,] 404342.19 84246.26 1115731.52 0.96226928 100.36207
#> [27,] -1709672.25 69021.99 -742688.70 0.50100037 99.48675
#> [28,] -362188.04 -350684.83 274047.32 0.39506522 99.44758
#> [29,] 1141120.76 -72988.54 -713895.55 0.95479972 99.02520
#> [30,] 784539.85 -1514263.40 503769.84 0.68177829 101.16137
#> [31,] -876957.07 -2976992.88 -132803.47 0.06676765 100.44079
#> [32,] -903873.75 2143116.86 -1899700.78 0.55681634 99.34081
#> [33,] 36755.17 898602.88 691657.54 0.64667606 101.27313
#> [34,] -124563.63 106029.96 47873.84 0.83449950 101.29160
#> [35,] 220986.21 776525.96 -243485.95 0.79215356 101.32413
#> [36,] -100571.95 -1142104.98 279354.74 0.44566592 100.00121
#> [37,] -981336.40 1156205.01 -827230.56 0.14931552 100.20328
#> [38,] 308556.32 -459635.81 333495.29 0.42804573 99.61193
#> [39,] 740654.43 -1489668.75 24904.11 0.68501294 98.99919
#> [40,] -556594.05 1429250.63 -1236874.01 0.81642249 98.77237
# 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'))
)