在 R 中使用 formattable() 时,逗号后的数字不会出现
Numbers after the comma do not appear when using formattable() in R
我正在使用格式table 包生成我的 data.frame 的 table 视图。但是我注意到有些列不显示逗号(十进制)后的数字。
例如:列“v”、“p”和“t”的格式table省略了逗号(小数)后的字符。为什么会这样?
aa2<-read.table(text="Ano v u a p ur h e t
2005 1782135.22 113711.81 98964.84 2207446.25 3876.68 7085.74 3265.89 59030602.87
2006 1719687.83 167937.4 97068.3 2218090.61 3936.55 6811.86 2952.21 59030602.87
2007 1755637.78 122799.6 94299.72 2229590.5 3978.23 6858.66 3171.66 59030602.87
2008 1779051.85 97385.73 101739.73 2225127.88 3996.84 6929.01 2254.58 59030602.87
2009 1805123.7 74061.79 109175.68 2215819.96 4126.57 6771.5 1406.21 59030602.87
2010 1716896.85 168013.92 108014.05 2210652.42 4210.9 7162.69 1535.68 59030602.87
2011 1736892.8 151980.31 113991.8 2200158.22 4259.77 7759.18 1442.06 59030602.87
2012 1757330.63 133273.24 125825.1 2185550.21 4419.45 8129.58 1958.48 59030602.87
2013 1639912.63 248584.77 140183.71 2171799.74 4531.06 8687.48 2777.32 59030602.87
2014 1657021.54 227375.14 180036.19 2136407.51 4631.85 8724.39 2287.94 59030602.87
2015 1720644.41 151089.44 190536.46 2138270.92 4733.71 8911.75 2298.34 59030602.87
2016 1662281.39 202916.33 210776.21 2124964.42 4803.06 8575.97 2165.52 59030602.87
2017 1716427.7 136156.44 230587.13 2117936.68 4809.71 8386.94 2170.25 59030602.87
2018 1638715.79 204483.2 255703.3 2101912.82 4931.96 8366.64 2349.4 59030602.87
", sep="", header = TRUE)
aa2
#Formatar a tabela
formattable(aa2, list(
'v' = color_tile("#00cccc","#0066cc"),
'u' = color_tile("#00cccc","#0066cc"),
'a' = color_tile("#00cccc","#0066cc"),
'p' = color_tile("#00cccc","#0066cc"),
'ur' = color_tile("#00cccc","#0066cc"),
'h' = color_tile("#00cccc","#0066cc"),
'e'= color_tile("#00cccc","#0066cc")
))
它可能已经四舍五入了。我们可以通过应用 comma
在 numeric
列
之上创建一个 formattable
属性来避免
library(dplyr)
library(formattable)
aa2 <- aa2 %>%
mutate(across(v:t, ~ formattable::comma(., digits = 2, big.mark = "")))
formattable::formattable(aa2, list(
'v' = color_tile("#00cccc","#0066cc"),
'u' = color_tile("#00cccc","#0066cc"),
'a' = color_tile("#00cccc","#0066cc"),
'p' = color_tile("#00cccc","#0066cc"),
'ur' = color_tile("#00cccc","#0066cc"),
'h' = color_tile("#00cccc","#0066cc"),
'e'= color_tile("#00cccc","#0066cc")
))
-输出
此设置由options
控制,它决定要显示的位数。如果您将 options(digits = n)
中的 n
设置为较大的数字,则默认情况下所有数字都将可见。
library(formattable)
options(digits = 10)
formattable(aa2, list(
'v' = color_tile("#00cccc","#0066cc"),
'u' = color_tile("#00cccc","#0066cc"),
'a' = color_tile("#00cccc","#0066cc"),
'p' = color_tile("#00cccc","#0066cc"),
'ur' = color_tile("#00cccc","#0066cc"),
'h' = color_tile("#00cccc","#0066cc"),
'e'= color_tile("#00cccc","#0066cc")
))
默认值为7,不允许显示所有数字。如果进一步减少数字,您会发现它对其他列也有影响。
options(digits = 4)
formattable(aa2, list(
'v' = color_tile("#00cccc","#0066cc"),
'u' = color_tile("#00cccc","#0066cc"),
'a' = color_tile("#00cccc","#0066cc"),
'p' = color_tile("#00cccc","#0066cc"),
'ur' = color_tile("#00cccc","#0066cc"),
'h' = color_tile("#00cccc","#0066cc"),
'e'= color_tile("#00cccc","#0066cc")
))
我正在使用格式table 包生成我的 data.frame 的 table 视图。但是我注意到有些列不显示逗号(十进制)后的数字。
例如:列“v”、“p”和“t”的格式table省略了逗号(小数)后的字符。为什么会这样?
aa2<-read.table(text="Ano v u a p ur h e t
2005 1782135.22 113711.81 98964.84 2207446.25 3876.68 7085.74 3265.89 59030602.87
2006 1719687.83 167937.4 97068.3 2218090.61 3936.55 6811.86 2952.21 59030602.87
2007 1755637.78 122799.6 94299.72 2229590.5 3978.23 6858.66 3171.66 59030602.87
2008 1779051.85 97385.73 101739.73 2225127.88 3996.84 6929.01 2254.58 59030602.87
2009 1805123.7 74061.79 109175.68 2215819.96 4126.57 6771.5 1406.21 59030602.87
2010 1716896.85 168013.92 108014.05 2210652.42 4210.9 7162.69 1535.68 59030602.87
2011 1736892.8 151980.31 113991.8 2200158.22 4259.77 7759.18 1442.06 59030602.87
2012 1757330.63 133273.24 125825.1 2185550.21 4419.45 8129.58 1958.48 59030602.87
2013 1639912.63 248584.77 140183.71 2171799.74 4531.06 8687.48 2777.32 59030602.87
2014 1657021.54 227375.14 180036.19 2136407.51 4631.85 8724.39 2287.94 59030602.87
2015 1720644.41 151089.44 190536.46 2138270.92 4733.71 8911.75 2298.34 59030602.87
2016 1662281.39 202916.33 210776.21 2124964.42 4803.06 8575.97 2165.52 59030602.87
2017 1716427.7 136156.44 230587.13 2117936.68 4809.71 8386.94 2170.25 59030602.87
2018 1638715.79 204483.2 255703.3 2101912.82 4931.96 8366.64 2349.4 59030602.87
", sep="", header = TRUE)
aa2
#Formatar a tabela
formattable(aa2, list(
'v' = color_tile("#00cccc","#0066cc"),
'u' = color_tile("#00cccc","#0066cc"),
'a' = color_tile("#00cccc","#0066cc"),
'p' = color_tile("#00cccc","#0066cc"),
'ur' = color_tile("#00cccc","#0066cc"),
'h' = color_tile("#00cccc","#0066cc"),
'e'= color_tile("#00cccc","#0066cc")
))
它可能已经四舍五入了。我们可以通过应用 comma
在 numeric
列
formattable
属性来避免
library(dplyr)
library(formattable)
aa2 <- aa2 %>%
mutate(across(v:t, ~ formattable::comma(., digits = 2, big.mark = "")))
formattable::formattable(aa2, list(
'v' = color_tile("#00cccc","#0066cc"),
'u' = color_tile("#00cccc","#0066cc"),
'a' = color_tile("#00cccc","#0066cc"),
'p' = color_tile("#00cccc","#0066cc"),
'ur' = color_tile("#00cccc","#0066cc"),
'h' = color_tile("#00cccc","#0066cc"),
'e'= color_tile("#00cccc","#0066cc")
))
-输出
此设置由options
控制,它决定要显示的位数。如果您将 options(digits = n)
中的 n
设置为较大的数字,则默认情况下所有数字都将可见。
library(formattable)
options(digits = 10)
formattable(aa2, list(
'v' = color_tile("#00cccc","#0066cc"),
'u' = color_tile("#00cccc","#0066cc"),
'a' = color_tile("#00cccc","#0066cc"),
'p' = color_tile("#00cccc","#0066cc"),
'ur' = color_tile("#00cccc","#0066cc"),
'h' = color_tile("#00cccc","#0066cc"),
'e'= color_tile("#00cccc","#0066cc")
))
默认值为7,不允许显示所有数字。如果进一步减少数字,您会发现它对其他列也有影响。
options(digits = 4)
formattable(aa2, list(
'v' = color_tile("#00cccc","#0066cc"),
'u' = color_tile("#00cccc","#0066cc"),
'a' = color_tile("#00cccc","#0066cc"),
'p' = color_tile("#00cccc","#0066cc"),
'ur' = color_tile("#00cccc","#0066cc"),
'h' = color_tile("#00cccc","#0066cc"),
'e'= color_tile("#00cccc","#0066cc")
))