简体   繁体   English

在 R 中使用 DT 和 Shiny 合成迷你图

[英]Composited Sparkline in R with DT and Shiny

I have got a reference by leonawicz that can combine sparkline and DT perfectly(Many thanks for him).我得到了 leonawicz 的参考,它可以完美地结合迷你图和 DT(非常感谢他)。 But, could you please give me a help to make a composited sparkline ?但是,你能帮我制作一个合成的迷你图吗? Thanks a lot!.非常感谢!。

Here is the sample code这是示例代码

library(data.table)
library(DT)
library(sparkline)
Data <- data.table(Type = c("A", "B", "C"),
               Value_1 = c("1,1,2,2", "2,2,3,3", "3,3,4,4"), 
               Value_2 = c("0,1,2,3", "2,3,4,5", "4,5,6,7"))
r <- c(0, 8)
line_string <- "type: 'line', lineColor: 'black', fillColor: '#ccc', 
               highlightLineColor: 'orange', highlightSpotColor: 'orange', 
               width: 80,height: 60"
cb_line = JS(paste0("function (oSettings, json) { 
            $('.spark:not(:has(canvas))').sparkline('html', { ", 
                line_string, ", chartRangeMin: ", r[1], ", chartRangeMax: ", 
                r[2], " }); }"), collapse = "")
cd <- list(list(targets = 1:2, render = JS("function(data, type, full){ 
           return '<span class=spark>' + data + '</span>' }")))
d1 <- datatable(Data, rownames = FALSE, options = list(columnDefs = cd,                                                       
                                              fnDrawCallback = cb_line))
d1$dependencies <- append(d1$dependencies, 
                          htmlwidgets:::getDependency("sparkline"))
d1

How could composite Value_1 and Value_2 into 1 sparkline chart?如何将 Value_1 和 Value_2 合成为 1 个迷你图? Thank you again!再次感谢你!

First of all you are making it difficult on yourself.首先,你让自己变得困难。 What you achieved with all that JS code can be easily reproduced in a more R way using the functions sparkline gives us (you are literally not using the sparkline package at all if not for adding the dependency):使用sparkline为我们提供的函数,您使用所有 JS 代码实现的结果可以以更R方式轻松重现(如果不是为了添加依赖项,您实际上根本不使用sparkline包):

Data:数据:

The data you are using doesn't make much sense to me.你使用的数据对我来说没有多大意义。 It should be organized in a tidier way ( one variable per column, one observation per row ).它应该以更整洁的方式组织(每列一个变量,每行一个观察)。

So I converted it:所以我转换了它:

dfO <- data.frame(Type = c("A", "B", "C"),
                   Value_1 = c("1,1,2,2", "2,2,3,3", "3,3,4,4"), 
                   Value_2 = c("0,1,2,3", "2,3,4,5", "4,5,6,7"))
library(tidyr)
library(dplyr)

df <- dfO %>% 
    separate_rows(Value_1, Value_2) %>% 
    mutate_at(vars(starts_with('Value')) ,funs(as.integer))

df
#>    Type Value_1 Value_2
#> 1     A       1       0
#> 2     A       1       1
#> 3     A       2       2
#> 4     A       2       3
#> 5     B       2       2
#> 6     B       2       3
#> 7     B       3       4
#> 8     B       3       5
#> 9     C       3       4
#> 10    C       3       5
#> 11    C       4       6
#> 12    C       4       7

Simple sparklines简单的迷你图

sparkline plays nicely with dplyr , and in particular summarize . sparkline很好地玩弄dplyr ,特别是summarize
The function spk_char convert the htmlwidget to a string that can be used inside another widget, in this case datatable .函数spk_charspk_char转换为可以在另一个小部件中使用的字符串,在本例中为datatable Options can be specified directly, no need to use JS .可以直接指定选项,不需要使用JS

library(dplyr)
library(sparkline)
library(DT)

df %>% 
    group_by(Type) %>% 
    summarize(l1 = spk_chr(Value_1,
                           lineColor = 'black', 
                           fillColor = '#ccc',
                           chartRangeMin = 0,
                           chartRangeMax = 8,
                           width = 80,
                           height = 60,
                           highlightLineColor = 'orange', 
                           highlightSpotColor = 'orange'),
              l2 = spk_chr(Value_2,
                           lineColor = 'black', 
                           fillColor = '#ccc',
                           chartRangeMin = 0,
                           chartRangeMax = 8,
                           width = 80,
                           height = 60,
                           highlightLineColor = 'orange', 
                           highlightSpotColor = 'orange')) %>% 
    datatable(escape = F,
              rownames = F,
              options = list(fnDrawCallback = htmlwidgets::JS('function(){
                                                              HTMLWidgets.staticRender();
                                                              }'))
    ) %>% 
    spk_add_deps()

在此处输入图片说明

Composite sparklines复合迷你图

That said, combining the two sparklines has proved more difficult than I thought it would be.也就是说,事实证明,将两条sparklines结合起来比我想象的要困难得多。 The solution is quite easy, but finding it took a bit.解决方案很简单,但找到它需要一些时间。

What I did is:我所做的是:

  • Split the dataset into groups将数据集拆分为组
  • Create the sparklines for each group为每个组创建迷你图
  • Combine them using spk_composite使用spk_composite组合它们
  • Convert to usable string inside DT using as.character(as.tags(l))使用as.character(as.tags(l))DT转换为可用字符串

The last step is what is done internally by spk_chr .最后一步是spk_chr在内部spk_chr

library(purrr)
df %>% 
    split(.$Type) %>% 
    map_df(~{
        l1 <- sparkline(.x$Value_1,
                        lineColor = 'black', 
                        fillColor = '#ccc',
                        chartRangeMin = 0,
                        chartRangeMax = 8,
                        width = 80,
                        height = 60,
                        highlightLineColor = 'orange', 
                        highlightSpotColor = 'orange')
        l2 <- sparkline(.x$Value_2,
                        lineColor = 'black', 
                        fillColor = '#ccc',
                        chartRangeMin = 0,
                        chartRangeMax = 8,
                        width = 80,
                        height = 60,
                        highlightLineColor = 'orange', 
                        highlightSpotColor = 'orange')
        l <- spk_composite(l2, 
                           l1) 
        data.frame(l1 = as.character(htmltools::as.tags(l1)), 
                   l2 = as.character(htmltools::as.tags(l2)), 
                   l = as.character(htmltools::as.tags(l)))
    }, .id = 'Type') %>% 
    datatable(escape = F,
              rownames = F,
              options = list(fnDrawCallback = htmlwidgets::JS('function(){
                                               HTMLWidgets.staticRender();
                                                                            }'))
    ) %>% 
    spk_add_deps()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM