- 已编辑
如题。
第一段代码
library(sparkline)
library(dplyr)
library(DT)
table2 <- data.frame(
column1 = c(rep(c('坂田银时', '神乐', '志村新八', '定春' ), 10)),
column2 = sample(1:4, 40, replace = T),
column3 = sample(10:20, 40, replace = T))
table2.group <- table2 %>%
group_by(column1) %>%
summarise(column2_mean = mean(column2))
table2.group$sparkline1 <- table2$column2 %>%
split(table2$column1) %>%
map(~ sparkline(.x, type = "line")) %>%
map(htmltools::as.tags) %>%
map_chr(as.character)
table2.group$sparkline2 <- table2$column3 %>%
split(table2$column1) %>%
map(~ sparkline(.x, type = "bar")) %>%
map(htmltools::as.tags) %>%
map_chr(as.character)
DT::datatable(table2.group, escape = FALSE) %>% spk_add_deps()
第二段代码
library(DT)
library(tibble) #使用tibble类型的数据
library(purrr) #使用 map_chr 函数
table1 <-
tibble(
column1 = c('坂田银时', '神乐', '志村新八', '定春'), # 第一列
column2 = c(100, 10000, 10, 100), # 第二列
column3 = c(1:4), # 第三列
sparkline = list(
v1 = x, # 第四列第一行
v2 = abs(x), # 第四列第二行
v3 = x, # 第四列第三行
v4 = abs(x) # 第四列第四行
)
)
table1$sparkline <- table1$sparkline %>%
map( ~ sparkline(.x, type = "box")) %>%
map(htmltools::as.tags) %>%
map_chr(as.character)
DT::datatable(table1, escape = FALSE) %>% spk_add_deps()
环境信息如下:
> sessionInfo()
R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.utf8 LC_CTYPE=Chinese (Simplified)_China.utf8 LC_MONETARY=Chinese (Simplified)_China.utf8
[4] LC_NUMERIC=C LC_TIME=Chinese (Simplified)_China.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_1.0.8 purrr_0.3.4 tibble_3.1.6 DT_0.23 sparkline_2.0 formattable_0.2.1
loaded via a namespace (and not attached):
[1] bslib_0.3.1 compiler_4.2.0 pillar_1.7.0 jquerylib_0.1.4 tools_4.2.0 digest_0.6.28 jsonlite_1.8.0
[8] evaluate_0.14 lifecycle_1.0.1 pkgconfig_2.0.3 rlang_1.0.2 DBI_1.1.2 cli_3.2.0 rstudioapi_0.13
[15] crosstalk_1.2.0 yaml_2.2.1 xfun_0.26 fastmap_1.1.0 knitr_1.36 generics_0.1.2 htmlwidgets_1.5.4
[22] sass_0.4.0 vctrs_0.3.8 tidyselect_1.1.2 glue_1.6.2 R6_2.5.1 fansi_1.0.2 rmarkdown_2.11
[29] magrittr_2.0.2 scales_1.1.1 ellipsis_0.3.2 htmltools_0.5.2 assertthat_0.2.1 colorspace_2.0-2 utf8_1.2.2
[36] munsell_0.5.0 crayon_1.5.0