r2d3 (version 1.0)

dfToJSON: Converts data to json form

Description

Creates a json file from an input

Usage

dfToJSON(df, mode = "vector")

Arguments

df
a data.frame to be converted
mode
there are four modes "vector", "coords" , "rowToObject", "hierarchy"

References

I got the code for the recursive function that makes the json hierarchy from someone on stack overflow. I'm sorry I was going to credit them but can no loner find the post. Let me know if it's you!

Examples

Run this code
data(iris)
jsonOut<-dfToJSON(iris)
fileConn<-file("iris.json")
writeLines(jsonOut, fileConn)
close(fileConn)

jsonOut<-dfToJSON(iris, mode="rowToObject")
fileConn<-file("irisRows.json")
writeLines(jsonOut, fileConn)
close(fileConn)

jsonOut<-dfToJSON(iris, mode="coords")
fileConn<-file("irisCoords.json")
writeLines(jsonOut, fileConn)
close(fileConn)

tree<-dcast(data=iris, Species+Petal.Width ~ .)
jsonOut<-dfToJSON(iris, mode="hierarchy")
fileConn<-file("irisTree.json")
writeLines(jsonOut, fileConn)
close(fileConn)

Run the code above in your browser using DataCamp Workspace