0

I found package sparkline:https://github.com/htmlwidgets/sparkline

but I have no idea how to create in markdown/html data.frame with sparkcharts.

I know there is an example in link above, but I don't know how to create that data frame in html automatically.

Taz
  • 5,755
  • 6
  • 26
  • 63

2 Answers2

1

I'm not really sure what your problem is or which data frame you refer to. The example on the link you provide is working perfectly. Try the following steps

  1. Start R Studio
  2. Install the sparkline package if you haven't

    library(devtools)
    install_github('htmlwidgets/sparkline')
    
  3. Use File -> New File -> R markdown

  4. Copy in the example from the htmlwidgets in the editor and hit the knitr button.

and you will get an html file with several examples.

ekstroem
  • 5,957
  • 3
  • 22
  • 48
1

I think this is what you are looking for: https://leonawicz.github.io/HtmlWidgetExamples/ex_dt_sparkline.html

you can also find a dummy example here: R combining data tables DT package and sparkline package box plot with target value

As an example: Boiled down dummy example as follows (disregard the last column in the data/table)

            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
Community
  • 1
  • 1
CapChaos
  • 21
  • 4
  • Although your answer is 100% correct, it might also become 100% useless if those links are moved, changed, merged into another one or the main site just disappear... **:-(** Therefore, please [edit] your answer, and copy the relevant steps from the link into your answer, thereby guaranteeing your answer for 100% of the lifetime of this site! **;-)** You can always leave the links in at the bottom of your answer as a source for your material... – Donald Duck Feb 15 '17 at 14:59