CatCorrJS with rCharts

来源:互联网 发布:巨人网络回归a股受益股 编辑:程序博客网 时间:2024/04/29 21:51

CatCorrJS with rCharts

CatCorrJS is a library that combines d3.js and crossfilter.js to make it easy to visualize relationships between categorical variables (e.g., results from a survey). This is an attempt to integrate it with rCharts.

The basic idea of the rCharts function integrating this library is to accept two csv files as inputs, one with the questions, and the other with the responses, and return an interactive visualization.

Inputs

The question file consists of three columns, representing type of question, the textcontent of the question and semicolon separated choices. The response file is exported from Google Forms.

Visualization

The catCorrPlot function reads the two csv files with questions and responses, processes the data to create a json payload that is accepted by the CatCorrJS library, and finally uses the rCharts base class to produce an interactive visualization.

  1. catCorrPlot <- function(questions_doc, responses_doc){
  2. require(rCharts)
  3. responses = read.csv(responses_doc)
  4. responses = toJSONArray(setNames(
  5. responses[,-1], 1:(NCOL(responses) - 1)
  6. ), json = F)
  7. questions = read.csv(questions_doc, stringsAsFactors = F)
  8. questions = lapply(1:NROW(questions), function(i){
  9. qi = as.list(questions[i,])
  10. qi$choices = strsplit(qi$choices, ";")[[1]]
  11. qi$number = i
  12. qi
  13. })
  14. questions = toJSONArray(questions, json = F)
  15. r1 <- rCharts$new()
  16. r1$setLib('http://rcharts.github.io/howitworks/catcorrjs/catcorrjs')
  17. r1$set(questions = questions, responses = responses)
  18. r1
  19. }

We can now use this function to create an interactive visualization of the survey

  1. r1 <- catCorrPlot("data/questions.csv", "data/responses.csv")

ToDos.

As an extension, I am working on a prototype, that would simplify workflow considerably by allowing a user to author a survey in RMarkdown, automagically create a google form based on it, and view the results as a dynamic interactive visualization.

Acknowledgements

All the heavy-lifting in creating this visualization is the work of Dean Malmgren and his awesome CatCorrJS. rCharts merely provides a wrapper to integrate this library into an #rstats workflow.

0 0
原创粉丝点击