r - Transform sequence of data into JSON for D3.js visualization -
i have data shows series of actions (column actions
) performed several users (column id
). order of data frame important - order actions performed in. each id, first action performed start
. consecutive identical actions possible (for example, sequence start -> d -> d -> d
valid ). code generate data:
set.seed(10) <- 0 all_id <- null all_vals <- null while (i < 5) { <- + 1 print(i) size <- sample(3:5, size = 1) tmp_id <- rep(i, times = size + 1) tmp_vals <- c("start",sample(letters, size = size) ) all_id <- c(all_id, tmp_id) all_vals <- c(all_vals, tmp_vals) } df <- data.frame(id = all_id, action = all_vals)
goal - transform data in json nested on multiple levels used in d3.js visualization (like this). see counter how many times each child appears respective parent (an maybe percentage out of total appearances of parent) - hope can myself.
expected output below - generic, not data generated above, , real data have quite lot of nested values ( count
, percentage
optional @ point in time):
{ "action": "start", "parent": "null", "count": "10", "percentage": "100", "children": [ { "action": "h", "parent": "start", "count": "6", "percentage": "60", "children": [ { "action": "d", "parent": "h", "count": "5", "percentage": "83.3" }, { "action": "b", "parent": "h", "count": "3", "percentage": "50" } ] }, { "action": "r", "parent": "start", "count": "4", "percentage": "40" } ] }
i know supposed post i've tried, don't have remotely worth of being shown.
i have started writing r -> d3.js converters in https://github.com/timelyportfolio/d3r should work in these type situations. work example later today data.
the internal hierarchy builder in https://github.com/timelyportfolio/sunburstr might work here.
i'll add answer explore both of these paths.
example 1
set.seed(10) <- 0 all_id <- null all_vals <- null while (i < 5) { <- + 1 print(i) size <- sample(3:5, size = 1) tmp_id <- rep(i, times = size + 1) tmp_vals <- c("start",sample(letters, size = size) ) all_id <- c(all_id, tmp_id) all_vals <- c(all_vals, tmp_vals) } df <- data.frame(id = all_id, action = all_vals) # not sure understand # supposed become here first try # find position of start start_pos <- which(df$action=="start") # sequences # surely there better way sequences <- paste( start_pos+1, c(start_pos[-1],nrow(df))-1, sep=":" ) paths <- lapply( sequences, function(x){ data.frame( t(as.character(df[eval(parse(text=x)),]$action)), stringsasfactors=false ) } ) paths_df <- dplyr::bind_rows(paths) # use d3r # devtools::install_github("timelyportfolio/d3r") library(d3r) d3_nest(paths_df) # if want list, json=false # visualize listviewer # devtools::install_github("timelyportfolio/listviewer") listviewer::jsonedit(d3_nest(paths_df))
Comments
Post a Comment