The excellent package inspectdf
produces tibble output where one column contains either another tibble (or list). Sample data, which is in the dput
at the bottom of this question, looks like this:
> test
# A tibble: 4 x 10
col_name min q1 median mean q3 max sd pcnt_na hist
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <list>
1 contacts 0 1 1 4.14 2 480 14.9 1.10 <tibble [26 x 2]>
2 pop 0.02 735. 2225. 4651. 4722. 289825. 10590. 0.082 <tibble [17 x 2]>
3 limit 0 750 1000 5994. 3500 500000 33355. 0.055 <tibble [27 x 2]>
4 code_count 1 1 1 1.72 2 43 1.50 0 <tibble [24 x 2]>
where the content of the column in question contains:
> test$hist[1]
[[1]]
# A tibble: 26 x 2
value prop
<chr> <dbl>
1 [-Inf, 0) 0
2 [0, 20) 0.961
3 [20, 40) 0.0200
4 [40, 60) 0.00696
5 [60, 80) 0.00352
6 [80, 100) 0.00200
7 [100, 120) 0.00144
8 [120, 140) 0.000837
9 [140, 160) 0.000696
10 [160, 180) 0.000459
# ... with 16 more rows
The question is, how to make this last column accessible or able to visualize from within a display mechanism? For example, when I use DT
it puts [object: Object]
in the rightmost column, where instead I would like some sort of useful thing, like maybe a link to view or popup the embedded data:
library(DT)
test %>% DT::datatable()
Is there a way to use sparklines, or make a link, or any other way to display the contents of that last column? All approaches are welcome (and it would be ideal if there was a way to do this within DT
). Thank you for your help.
test <- structure(list(col_name = c("contacts", "pop", "limit", "code_count"
), min = c(0, 0.02, 0, 1), q1 = c(1, 735.1, 750, 1), median = c(1,
2224.73, 1000, 1), mean = c(4.142, 4651.211, 5994.002, 1.717),
q3 = c(2, 4721.56, 3500, 2), max = c(480, 289825.16, 5e+05,
43), sd = c(14.926, 10590.427, 33354.671, 1.501), pcnt_na = c(1.103,
0.082, 0.055, 0), hist = list(structure(list(value = c("[-Inf, 0)",
"[0, 20)", "[20, 40)", "[40, 60)", "[60, 80)", "[80, 100)",
"[100, 120)", "[120, 140)", "[140, 160)", "[160, 180)", "[180, 200)",
"[200, 220)", "[220, 240)", "[240, 260)", "[260, 280)", "[280, 300)",
"[300, 320)", "[320, 340)", "[340, 360)", "[360, 380)", "[380, 400)",
"[400, 420)", "[420, 440)", "[440, 460)", "[460, 480)", "[480, Inf)"
), prop = c(0, 0.961394177780924, 0.0200167851400953, 0.00696279968047565,
0.00352285711396706, 0.00199601605710992, 0.00143785959129195,
0.000837234698726958, 0.000695673276236893, 0.000459063470074926,
0.00138527963436707, 0.00129023125069517, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 2.02230603557236e-06, 0)), row.names = c(NA,
-26L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
value = c("[-Inf, 0)", "[0, 20000)", "[20000, 40000)",
"[40000, 60000)", "[60000, 80000)", "[80000, 1e+05)",
"[1e+05, 120000)", "[120000, 140000)", "[140000, 160000)",
"[160000, 180000)", "[180000, 2e+05)", "[2e+05, 220000)",
"[220000, 240000)", "[240000, 260000)", "[260000, 280000)",
"[280000, 3e+05)", "[3e+05, Inf)"), prop = c(0, 0.965121399547629,
0.0219419924337957, 0.00583678616465502, 0.00279829460157329,
0.00160731800076062, 0.00102283872775676, 0.000630517023959647,
0.000388318421105306, 0.000296242919193739, 0.000178146079785424,
6.40525230689165e-05, 6.20508817230129e-05, 4.60377509557837e-05,
4.00328269180728e-06, 2.00164134590364e-06, 0)), row.names = c(NA,
-17L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
value = c("[-Inf, 0)", "[0, 20000)", "[20000, 40000)",
"[40000, 60000)", "[60000, 80000)", "[80000, 1e+05)",
"[1e+05, 120000)", "[120000, 140000)", "[140000, 160000)",
"[160000, 180000)", "[180000, 2e+05)", "[2e+05, 220000)",
"[220000, 240000)", "[240000, 260000)", "[260000, 280000)",
"[280000, 3e+05)", "[3e+05, 320000)", "[320000, 340000)",
"[340000, 360000)", "[360000, 380000)", "[380000, 4e+05)",
"[4e+05, 420000)", "[420000, 440000)", "[440000, 460000)",
"[460000, 480000)", "[480000, 5e+05)", "[5e+05, Inf)"
), prop = c(0, 0.962615288870034, 0.00958330915327091,
0.0104617958348925, 0.00314974495870712, 0, 0.00967936236675118,
0, 5.00277153543063e-05, 0, 0, 8.80487790235791e-05,
0, 0.000580321498109953, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0.00379210082385642, 0)), row.names = c(NA, -27L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(value = c("[-Inf, 0)",
"[0, 2)", "[2, 4)", "[4, 6)", "[6, 8)", "[8, 10)", "[10, 12)",
"[12, 14)", "[14, 16)", "[16, 18)", "[18, 20)", "[20, 22)",
"[22, 24)", "[24, 26)", "[26, 28)", "[28, 30)", "[30, 32)",
"[32, 34)", "[34, 36)", "[36, 38)", "[38, 40)", "[40, 42)",
"[42, 44)", "[44, Inf)"), prop = c(0, 0.663122, 0.251104,
0.054242, 0.01933, 0.007962, 0.002444, 0.000946, 0.00032,
0.00016, 8.4e-05, 4e-05, 1.4e-05, 1.2e-05, 8.4e-05, 0.000102,
1.6e-05, 0, 4e-06, 2e-06, 2e-06, 4e-06, 6e-06, 0)), row.names = c(NA,
-24L), class = c("tbl_df", "tbl", "data.frame")))), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
You can visualize the nested tibble with the sparkline
package :
library(dplyr)
library(htmltools)
library(sparkline)
library(purrr)
library(DT)
# Create the HTML tag for each tibble
hist_spark <- test$hist %>% purrr::map( function(x) {
as.character(htmltools::as.tags(sparkline(x$prop)))
})
test$hist_spark <- hist_spark
# Render the tags
datatable(test,
options = list(
columnDefs = list(list(visible=FALSE, targets=c(c(10)))),
fnDrawCallback = htmlwidgets::JS('function(){HTMLWidgets.staticRender();}')
)
) %>% sparkline::spk_add_deps()
fnDrawCallback
datatable option and spk_add_deps
are necessary for this to work :