R combining data tables DT package and sparkline package box plot with target value

北慕城南 提交于 2019-12-08 04:41:14

问题


I'm using sparkline plots in a DT table as outlayed here: https://leonawicz.github.io/HtmlWidgetExamples/ex_dt_sparkline.html

Boiled down dummy example as follows

            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

I want to use the target value function in the boxplots http://omnipotent.net/jquery.sparkline/#boxplot

As the sparkline call is defined on column level I can not pass on individual target levels as argument.

So what I would need is a wrapper function for the sparkline call that takes as an argument the 6 values defined in the column boxWithTarget (the last value being the target value) and calls on the sparkline function. In Dummy code:

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

To my (limited) understanding this wrapper function would need to be defined on the java level. As I have no idea how to achieve this any help would be much appreciated. Many thanks!

来源:https://stackoverflow.com/questions/42251594/r-combining-data-tables-dt-package-and-sparkline-package-box-plot-with-target-va

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!