使用格式化的样式问题
Style questions using formattable
我希望使用 R 包格式 table 在 HTML 中显示数据框。在下面的 table 中,如何为值 6100 和 7500 添加逗号?请注意,我已将这些值四舍五入到最接近的百位。
此外,如何增加字体大小并加粗整个加拿大行?
library(tidyverse)
library(formattable)
target_year = 1980
current_year = 2019
can_target = 20
can2019 = 37
world_target = 6123
world2019 = 7456
can_change <- (can2019 - can_target) / can_target
world_change <- (world2019 - world_target) / world_target
can_world_table <- tibble(
region = c("Canada", "World"),
ty = c(can_target, round(world_target, digits = -2)),
cy = c(can2019, round(world2019, digits = -2)),
change = percent(c(can_change, world_change), 0)
) %>%
set_names(c("", target_year, current_year, "Change"))
can_world_table
我可以回答你问题的 "add commas to the values" 部分。
我过去使用过的一个特定库 numform。
最终结果是字符类型的,所以如果您打算显示该值,它很有用,但如果您打算在计算中使用它,它就没有用了。
> library('numform')
> v <- f_comma(c(100000,1000))
> v
[1] "100,000" "1,000"
Doco 在这里:https://cran.r-project.org/web/packages/numform/numform.pdf
您可以使用 formattable
以不同方式添加逗号。
一种方法是根据需要添加到列中,使用 format
和 big.mark
,例如:
ty = c(can_target, format(round(world_target, digits = -2), big.mark = ",")),
cy = c(can2019, format(round(world2019, digits = -2), big.mark = ",")),
这会给你这个:
# A tibble: 2 x 4
` ` `1980` `2019` Change
<chr> <chr> <chr> <formttbl>
1 Canada 20 37 85%
2 World 6,100 7,500 22%
然后,要增加字体大小并加粗一行,您可以创建一个格式化程序并将其应用于该行:
lg_bold <- formatter("span", style = ~style("font.weight" = "bold", "font.size" = "16px"))
formattable(can_world_table, list(area(row = 2) ~ lg_bold))
另一个注意事项是我在第 1 行第 1 列添加了 space 而不是空字符串,因为 formattable 没有 link 那。
can_world_table <- tibble(
region = c("Canada", "World"),
ty = c(can_target, format(round(world_target, digits = -2), big.mark = ",")),
cy = c(can2019, format(round(world2019, digits = -2), big.mark = ",")),
change = percent(c(can_change, world_change), 0)
) %>%
set_names(c(" ", target_year, current_year, "Change")) # Added space for formattable
lg_bold <- formatter("span", style = ~style("font.weight" = "bold", "font.size" = "16px"))
formattable(can_world_table, list(area(row = 2) ~ lg_bold))
我希望使用 R 包格式 table 在 HTML 中显示数据框。在下面的 table 中,如何为值 6100 和 7500 添加逗号?请注意,我已将这些值四舍五入到最接近的百位。
此外,如何增加字体大小并加粗整个加拿大行?
library(tidyverse)
library(formattable)
target_year = 1980
current_year = 2019
can_target = 20
can2019 = 37
world_target = 6123
world2019 = 7456
can_change <- (can2019 - can_target) / can_target
world_change <- (world2019 - world_target) / world_target
can_world_table <- tibble(
region = c("Canada", "World"),
ty = c(can_target, round(world_target, digits = -2)),
cy = c(can2019, round(world2019, digits = -2)),
change = percent(c(can_change, world_change), 0)
) %>%
set_names(c("", target_year, current_year, "Change"))
can_world_table
我可以回答你问题的 "add commas to the values" 部分。
我过去使用过的一个特定库 numform。
最终结果是字符类型的,所以如果您打算显示该值,它很有用,但如果您打算在计算中使用它,它就没有用了。
> library('numform')
> v <- f_comma(c(100000,1000))
> v
[1] "100,000" "1,000"
Doco 在这里:https://cran.r-project.org/web/packages/numform/numform.pdf
您可以使用 formattable
以不同方式添加逗号。
一种方法是根据需要添加到列中,使用 format
和 big.mark
,例如:
ty = c(can_target, format(round(world_target, digits = -2), big.mark = ",")),
cy = c(can2019, format(round(world2019, digits = -2), big.mark = ",")),
这会给你这个:
# A tibble: 2 x 4
` ` `1980` `2019` Change
<chr> <chr> <chr> <formttbl>
1 Canada 20 37 85%
2 World 6,100 7,500 22%
然后,要增加字体大小并加粗一行,您可以创建一个格式化程序并将其应用于该行:
lg_bold <- formatter("span", style = ~style("font.weight" = "bold", "font.size" = "16px"))
formattable(can_world_table, list(area(row = 2) ~ lg_bold))
另一个注意事项是我在第 1 行第 1 列添加了 space 而不是空字符串,因为 formattable 没有 link 那。
can_world_table <- tibble(
region = c("Canada", "World"),
ty = c(can_target, format(round(world_target, digits = -2), big.mark = ",")),
cy = c(can2019, format(round(world2019, digits = -2), big.mark = ",")),
change = percent(c(can_change, world_change), 0)
) %>%
set_names(c(" ", target_year, current_year, "Change")) # Added space for formattable
lg_bold <- formatter("span", style = ~style("font.weight" = "bold", "font.size" = "16px"))
formattable(can_world_table, list(area(row = 2) ~ lg_bold))