Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用formattable设置问题的样式_R_Formattable - Fatal编程技术网

使用formattable设置问题的样式

使用formattable设置问题的样式,r,formattable,R,Formattable,我希望使用R包formattable在HTML中显示数据帧。在下表中,如何将逗号添加到值6100和7500?注意,我已将这些值四舍五入到最接近的一百 另外,如何增加字体大小并加粗整个加拿大行 library(tidyverse) library(formattable) target_year = 1980 current_year = 2019 can_target = 20 can2019 = 37 world_target = 6123 world2019 = 7456 can_c

我希望使用R包formattable在HTML中显示数据帧。在下表中,如何将逗号添加到值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
库(tidyverse)
库(格式化表)
目标年=1980年
本年度=2019年
can_目标=20
can2019=37
世界_目标=6123
世界2019=7456
can_change我可以回答你问题的“在值中添加逗号”部分

我在过去使用过的一个特定库numform

最终结果的类型为character,因此,如果您计划显示该值,它将很有用,但如果您计划在计算中使用它,它将不有用

> library('numform')
> v <- f_comma(c(100000,1000))
> v
[1] "100,000" "1,000"
>库('numform')
>v v
[1] "100,000" "1,000"

Doco Here:

您可以使用
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%
#一个tible:2 x 4
``1980``2019`变化
1加拿大20 37 85%
2世界6100 7500 22%
然后,要增加行的字体大小并加粗,可以创建格式设置程序并应用于该行:

lg_bold <- formatter("span", style = ~style("font.weight" = "bold", "font.size" = "16px"))
formattable(can_world_table, list(area(row = 2) ~ lg_bold))
lg\u粗体
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))