大致就是如果用 DT 包引入 迷你图(sparkline)的话,只有第一页是有图形的,翻页以后本该显示图形的地方都是空的。我原以为是 sparkline 这个包和所有动态表格包的分页功能都冲突了,后来用 reactable 试了试,却是好使的。
初步怀(瞎)疑(猜),DT 在加载数据时可能是一页一页加载的,而 reactable 是一口气加载全部,数据量大的情况下 DT 的效率会更高,但是遇到 sparkline 就出问题了。
举个栗子,编的数据是这样的:
```{r}
library(data.table)
library(sparkline)
data <- data.frame(type1 = sort(rep(LETTERS[1:20], 2)),
value = sample(2000:5000, 40))
dt <- as.data.table(data)
```
DT 包子的代码如下:
```{r}
library(DT)
dt.DT <-
dt[, .(
sparkline1 = as.character(htmltools::as.tags(
sparkline(
value,
type = "pie",
width = 50,
height = 50
)
))
), keyby = .(type1)]
datatable(dt.DT, escape = FALSE) |> spk_add_deps()
```
reactable 包子的代码如下:
```{r}
library(reactable)
dt.react <-
dt[, .(sparkline1 = list(value)), keyby = .(type1)]
reactable(dt.react,
columns = list(sparkline1 = colDef(
cell = function(values) {
sparkline(values,
type = "pie")
}
)))
```
环境信息如下:
> 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] sparkline_2.0 data.table_1.14.2 reactable_0.3.0 DT_0.23
loaded via a namespace (and not attached):
[1] digest_0.6.28 reactR_0.4.4 R6_2.5.1 jsonlite_1.8.0 magrittr_2.0.2 evaluate_0.14 rlang_1.0.2
[8] cli_3.2.0 jquerylib_0.1.4 bslib_0.3.1 rmarkdown_2.11 tools_4.2.0 htmlwidgets_1.5.4 crosstalk_1.2.0
[15] xfun_0.26 yaml_2.2.1 fastmap_1.1.0 compiler_4.2.0 htmltools_0.5.2 knitr_1.36 sass_0.4.0