1

I've recently updated my SunburstR package in R to the 2.0 Version and suddenly the sorting of the slices (by size, descending, clockwise) does not happen automatically as it used to. Any R & JS/d3.js expert around to help me implement a custom JS sortFunction or say how to solve the problem? For reproduction purposes:

library(jsonlite)
library(sunburstR)
b = read.csv(
   file = paste0(
   "https://gist.githubusercontent.com/rileycrane/",
   "92a2c36eb932b4f99e51/raw/",
   "a0212b4ca8043af47ec82369aa5f023530279aa3/visit-sequences.csv"
 ),header=FALSE
,stringsAsFactors = FALSE
)

sequence_json <- jsonlite::fromJSON(
  system.file("examples/visit-sequences.json",package="sunburstR"),
  simplifyDataFrame = FALSE
)
sunburst(sequence_json)

sunburst function provides a sortFunction parameeter where one can write custom JS, like in:

sunburst(df,
     # create a trivial sort function
     sortFunction = htmlwidgets::JS('function(x) {return x;}'))

Thanks!

PS result: Not sorted

How it used to look like (notice the order of slices):

Sunburst in ealier versions

Centar 15
  • 127
  • 1
  • 13

1 Answers1

1

Perhaps this example sorting alphabetically will help.

As another example, here is how we could sort each node from largest to smallest count.

library(sunburstR)

sequence_json <- jsonlite::fromJSON(
  system.file("examples/visit-sequences.json",package="sunburstR"),
  simplifyDataFrame = FALSE
)

sunburst(
  sequence_json,
  sortFunction = htmlwidgets::JS(
    "
function(a,b) {
  // sort by count descending
  //   unlike the other example using data.name, value is at the top level of the object
  return b.value - a.value
}
"    
  )
)

Most of the attributes will become prepended by .data.

screenshot of json data

screenshot of node data

It might be helpful to set a debugger in the sortFunction and open the widget with developer tools in Chrome and Firefox to see what you are working with.

timelyportfolio
  • 6,479
  • 30
  • 33
  • Wow from the creator himself :) Thank you so much! I've actually been using rend2b which looks amazing, but the valueField doesn't seem to pick up when I use "size" as a param (which is also the default param). – Centar 15 Jun 28 '18 at 12:29
  • 1
    Correction - I've now noticed that the valueField does something else. What I lack is sorting of the slices in sund2b. – Centar 15 Jun 28 '18 at 14:17
  • sorting on the `sund2b` will be a little trickier, but https://github.com/d2bjs/d2b/issues/17 shows that it can be done. – timelyportfolio Jul 10 '18 at 11:33