1

我在 DT 表中使用迷你图,如下所示: https ://leonawicz.github.io/HtmlWidgetExamples/ex_dt_sparkline.html

归结为虚拟示例如下

            library(data.table)
            library(DT)
            library(sparkline)



            hist.A<-rnorm(100)
            hist.B<-rnorm(100)
            hist.C<-rnorm(100)

            current.A<-rnorm(1)
            current.B<-rnorm(1)
            current.C<-rnorm(1)

            #whisker should show full range of data
            boxval.A<-paste(quantile(hist.A,probs=c(0,0.25,0.5,0.75,1)),collapse = ",")
            boxval.B<-paste(quantile(hist.B,probs=c(0,0.25,0.5,0.75,1)),collapse = ",")
            boxval.C<-paste(quantile(hist.C,probs=c(0,0.25,0.5,0.75,1)),collapse = ",")

            data<-data.frame(Variable=c("A","B","C"),Current=c(current.A,current.B,current.C),boxplot=c(boxval.A,boxval.B,boxval.C))
            data$boxWithTarget<-paste(data$boxplot,data$Current,Sep=",")

            cd <- list(list(targets = 2, render = JS("function(data, type, full){ return '<span class=sparkSamples>' + data + '</span>' }")))
            line_string <- "type: 'line', lineColor: 'black', fillColor: '#ccc', highlightLineColor: 'orange', highlightSpotColor: 'orange'"
            box_string <- "type: 'box', raw:true, showOutliers:false,lineColor: 'black', whiskerColor: 'black', outlierFillColor: 'black', outlierLineColor: 'black', medianColor: 'black', boxFillColor: 'orange', boxLineColor: 'black'"
            cb = JS("function (oSettings, json) {\n  $('.sparkSeries:not(:has(canvas))').sparkline('html', { ", 
                    line_string, " });\n  $('.sparkSamples:not(:has(canvas))').sparkline('html', { ", 
                    box_string, " });\n}")


            d <- datatable(data.table(data), rownames = FALSE, options = list(columnDefs = cd, 
                                                                                 fnDrawCallback = cb))
            d$dependencies <- append(d$dependencies, htmlwidgets:::getDependency("sparkline"))
            d

我想在箱线图中使用目标值函数 http://omnipotent.net/jquery.sparkline/#boxplot

由于迷你图调用是在列级别定义的,我不能将单个目标级别作为参数传递。

所以我需要一个用于迷你图调用的包装函数,它将 boxWithTarget 列中定义的 6 个值(最后一个值是目标值)作为参数,并调用迷你图函数。在虚拟代码中:

            sparkline_target(input,...) {
                v=strsplit(input, ",")
                tar=v[6]
                val<-paste(v[1:5],collapse=",")
                sparkline(val,target=tar,..)
                }

据我(有限)理解,这个包装函数需要在 java 级别上定义。因为我不知道如何实现这一点,任何帮助将不胜感激。非常感谢!

4

0 回答 0