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.
# !formatR
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,] -1721817.661 146531.380 -2512334.33 0.72694885 98.88029
#> [2,] -147325.302 677709.069 -55693.78 0.63768555 100.51460
#> [3,] -144199.607 218194.874 533889.79 0.39640996 98.49090
#> [4,] -182705.449 -1811720.491 -281951.11 0.95948261 101.53274
#> [5,] -453699.384 962086.482 524187.57 0.29865803 100.42915
#> [6,] 728982.042 -143236.740 1163102.00 0.05020117 100.12210
#> [7,] 2165024.895 -106087.195 1148712.62 0.57618742 98.86199
#> [8,] -1530989.402 119177.592 61897.11 0.21790581 99.44198
#> [9,] 612426.950 129560.754 586148.92 0.12585627 101.05254
#> [10,] -1763011.492 649827.542 1772882.61 0.93815269 100.67768
#> [11,] -422012.515 -2174114.857 -254361.16 0.80127513 100.03850
#> [12,] 47398.090 2782557.184 1046347.89 0.75805362 99.64362
#> [13,] 642996.343 -261221.255 1416826.36 0.53256516 100.78284
#> [14,] -814074.827 313355.750 -196640.02 0.54680477 100.80441
#> [15,] 568154.420 1174345.882 -287213.58 0.09592650 98.09994
#> [16,] 462951.256 -565088.249 -685432.66 0.38834975 100.93578
#> [17,] -1204543.545 1213952.419 -956736.87 0.17235189 99.69095
#> [18,] 837776.321 -145896.412 -695541.43 0.69072585 100.26307
#> [19,] 1988504.929 -1077563.309 -1656275.43 0.67520850 98.20941
#> [20,] 2554.896 -875850.616 -590537.90 0.94629485 99.21174
#> [21,] -835847.354 1165057.320 -458541.99 0.19621952 98.86698
#> [22,] 84049.689 231670.635 -436663.33 0.96863750 100.36365
#> [23,] -726788.954 588628.809 327127.13 0.38709628 99.71411
#> [24,] -1412399.651 -1599450.568 1078454.92 0.65034390 100.51767
#> [25,] 1035363.190 -1370736.306 -108882.65 0.81459620 99.89709
#> [26,] 276488.611 384150.344 -1299410.46 0.07096477 99.02593
#> [27,] 343685.465 1437320.413 358537.29 0.52683032 101.27067
#> [28,] 1723548.883 336696.283 -341799.45 0.76347483 100.96086
#> [29,] 212038.083 1418293.384 668599.86 0.43538664 100.76872
#> [30,] -33997.013 623909.788 2226850.46 0.55247234 101.03593
#> [31,] -1810087.468 706748.047 524858.44 0.20403065 99.52611
#> [32,] -179237.242 -9935.672 -1584281.53 0.03102602 98.72467
#> [33,] -213445.978 272181.715 349401.78 0.96970706 99.69438
#> [34,] 1167307.879 9672.713 1172838.25 0.17861309 102.21177
#> [35,] 170034.850 2024343.341 2139369.26 0.77829279 98.95833
#> [36,] -539123.324 1398392.759 549453.78 0.88571080 98.85348
#> [37,] 50035.101 848791.268 1491814.05 0.83644625 98.32467
#> [38,] -151483.443 656224.329 526566.55 0.60536844 101.52594
#> [39,] 544797.116 -448257.264 207583.99 0.90687946 100.55419
#> [40,] 2855417.575 1210534.893 122294.73 0.03590981 101.99311
# 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'))
)