Format numeric columns in a table as currency (formatCurrency()) or
percentages (formatPercentage()), or round numbers to a specified
number of decimal places (formatRound()). The function
formatStyle() applies CSS styles to table cells by column.
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'))
currency
the currency symbol
interval
put a marker after how many digits of the numbers
mark
the marker after every interval decimals in the numbers
digits
the number of decimal places to round to
method
the method(s) to convert a date to string in JavaScript; see
DT:::DateMethods for a list of possible methods, and
http://mzl.la/1xGe99W for a full reference
fontWeight
the font weight, e.g. 'bold' and 'normal'
color
the font color, e.g. 'red' and '#ee00aa'
backgroundColor
the background color of table cells
background
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)
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)
# 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'))
)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(DT)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/DT/formatCurrency.Rd_%03d_medium.png", width=480, height=480)
> ### Name: formatCurrency
> ### Title: Format table columns
> ### Aliases: formatCurrency formatDate formatPercentage formatRound
> ### formatStyle
>
> ### ** 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,] -1093831.00 853252.44 -1925330.191 0.80439846 100.52227
[2,] -1982540.05 1301473.89 1887772.051 0.74008880 100.68155
[3,] -281484.70 -236125.53 -876791.348 0.95132688 98.67726
[4,] -428608.50 -850891.07 1567892.918 0.47476141 100.07202
[5,] 40359.75 -74520.69 430381.512 0.29023219 100.87685
[6,] 621297.60 -499580.85 757673.395 0.28083906 99.47628
[7,] 1271478.21 184156.81 344086.221 0.68227680 101.26409
[8,] -375198.76 705702.81 117758.895 0.80736988 101.20450
[9,] 1532348.58 109109.61 798579.200 0.61524106 98.41048
[10,] 712910.91 485599.99 -793701.586 0.39158484 100.00739
[11,] -908177.40 -1280613.05 927506.348 0.80574382 102.02712
[12,] 956707.90 606309.34 248807.965 0.37655358 96.23975
[13,] -1146457.65 -222109.48 633913.050 0.31808406 99.39136
[14,] 1385282.22 -15563.81 295655.792 0.61998199 100.45360
[15,] 1005339.88 791216.02 -1137365.692 0.22043571 99.96572
[16,] -832624.54 -1141109.08 30245.477 0.19258902 99.43779
[17,] -1271699.88 -1002025.28 1141361.454 0.44134879 100.72929
[18,] 417082.46 -361476.68 -568779.212 0.18080952 98.93679
[19,] 134630.37 122395.15 1119638.573 0.68652452 99.15067
[20,] 779677.02 1038038.27 1486523.071 0.74981298 98.53189
[21,] -306500.37 223121.82 2069763.817 0.41455959 100.42294
[22,] 299358.61 -19466.08 829410.627 0.31852353 100.04402
[23,] 2927670.08 -1424415.66 -432526.530 0.16056296 98.01674
[24,] 910026.20 -598148.33 6554.748 0.80812359 100.44334
[25,] -690402.64 392327.37 2681151.992 0.90789800 99.71378
[26,] -1154584.88 967574.02 703519.195 0.92260486 98.83532
[27,] 2865758.33 25821.12 -475857.145 0.13126501 100.61138
[28,] 898495.22 29004.88 648321.788 0.17549980 99.71295
[29,] -1213948.35 -178922.77 2445686.399 0.66198360 98.85744
[30,] 2709915.57 1748028.03 -1400794.329 0.49612524 99.33129
[31,] -834963.77 753009.85 -22748.867 0.46769625 100.40200
[32,] 324436.49 -86829.60 2454076.485 0.73384205 100.30471
[33,] 1494725.36 -759234.66 365919.646 0.82537554 101.31943
[34,] 1016307.05 -295800.49 722862.314 0.56486877 99.92432
[35,] -97192.76 -794981.29 2077575.812 0.03144262 100.16996
[36,] -716252.43 176921.67 -1866860.424 0.61023209 99.84397
[37,] -692992.26 -831998.94 -468050.148 0.76786030 100.09162
[38,] 295595.27 170673.99 641752.590 0.88166743 99.73095
[39,] -216107.45 561608.14 -1463363.368 0.35890562 100.71737
[40,] 716131.04 1234019.47 -53293.451 0.91195229 100.37777
>
> # 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)
>
> # 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'))
+ )
>
>
>
>
>
> dev.off()
null device
1
>