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