Skip to content

Commit

Permalink
add ability to specify either csv or json data
Browse files Browse the repository at this point in the history
  • Loading branch information
timelyportfolio committed Jul 2, 2015
1 parent 27fa2ac commit 4cc93b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
9 changes: 7 additions & 2 deletions R/sunburst.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
#'
#' @export
sunburst <- function(
data = NULL
csvdata = NULL
, jsondata = NULL
, width = NULL
, height = NULL
) {

if(is.null(csvdata) && is.null(jsondata)) stop("please provide either csvdata or jsondata",call.=FALSE)
if(!is.null(csvdata) && !is.null(jsondata)) warning("both csv and json provided; will use csvdata",call.=FALSE)

# forward options using x
x = list(
data = data
csvdata = csvdata
,jsondata = jsondata
)

# create widget
Expand Down
31 changes: 31 additions & 0 deletions inst/examples/example_replicate.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,34 @@ browsable(
,grViz('digraph {A->B;}')
)
)

# try with json data
sequence_json <- rjson::fromJSON(file="./inst/examples/visit-sequences.json")
sunburst(jsondata = sequence_json)



# try with sire json data from
# https://twitter.com/UTVilla/status/616600742967816193
# but does not work
# guessing JS needs to be changed
sunburst( jsondata = rjson::fromJSON( file = "https://rawgit.com/durtal/durtal.github.io/master/data/prominent_sires.json") )


# try with csv data from this fork
# https://gist.github.com/mkajava/7515402
# works technically but not cosmetically
sunburst( csvdata = read.csv(
file = "https://gist.githubusercontent.com/mkajava/7515402/raw/9f80d28094dc9dfed7090f8fb3376ef1539f4fd2/comment-sequences.csv"
,header = FALSE
,stringsAsFactors = FALSE
))


# try with csv data from this fork
# https://gist.github.com/rileycrane/92a2c36eb932b4f99e51/
sunburst( csvdata = read.csv(
file = "https://gist.githubusercontent.com/rileycrane/92a2c36eb932b4f99e51/raw/a0212b4ca8043af47ec82369aa5f023530279aa3/visit-sequences.csv"
,header=FALSE
,stringsAsFactors = FALSE
))
1 change: 1 addition & 0 deletions inst/examples/visit-sequences.json

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions inst/htmlwidgets/sunburst.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ HTMLWidgets.widget({
// x.data should be a data.frame in R so an Javascript Object of Objects
// but buildHierarchy expects an Array of Arrays
// so use d3.zip and apply to do this
var json = buildHierarchy(
d3.zip.apply(
null,
Object.keys(x.data).map(function(ky){return x.data[ky]})
)
);
var json = [];
if(x.csvdata !== null){
json = buildHierarchy(
d3.zip.apply(
null,
Object.keys(x.csvdata).map(function(ky){return x.csvdata[ky]})
)
);
} else {
json = x.jsondata
}
createVisualization(json);

// Main function to draw and set up the visualization, once we have the data.
Expand Down
2 changes: 1 addition & 1 deletion man/sunburst.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
\alias{sunburst}
\title{htmlwidget for d3.js sequence sunburst diagrams}
\usage{
sunburst(data = NULL, width = NULL, height = NULL)
sunburst(csvdata = NULL, jsondata = NULL, width = NULL, height = NULL)
}
\description{
\href{https://gist.github.com/kerryrodden/7090426}{Sequences sunburst} diagrams provide
Expand Down

0 comments on commit 4cc93b8

Please sign in to comment.