Skip to content

Instantly share code, notes, and snippets.

View gluc's full-sized avatar

Christoph Glur gluc

View GitHub Profile
@gluc
gluc / gist:05e7c2aef5becd14382b
Last active August 29, 2015 14:19
Converting data.tree to JSON
library(data.tree)
#create an example tree
contacts <- Node$new("contacts")
contacts$type <- "root"
jack <- contacts$AddChild("c1")
jack$fullName <- "Jack Miller"
jack$isGoodCustomer <- FALSE
jack$type <- "customer"
jill <- contacts$AddChild("c2")
@gluc
gluc / tree conversion
Created May 13, 2015 14:17
Converting data.tree to JSON, simpler
#this has now become much simpler:
data(acme)
l <- acme$ToList()
#alternatively, you might prefer the "traditional" generic method:
l <- as.list(acme)
library(rjson)
j <- toJSON(l)
cat(j)
@gluc
gluc / Desc_JSON_to_df.md
Last active June 19, 2023 02:32
Convert a complex JSON to an R data.frame

This gist shows how to convert a nested JSON file to an R data.frame. To do this, it uses jsonlite and data.tree.

The gist contains two examples: one is a bit simpler, the second one a bit more advanced.

Example 1

In the first example, we download all the repos from Hadley Wickham's Github account from https://api.github.com/users/hadley/repos . This JSON contains a nested owner object. The code shows how to convert that in a flat data.frame in three statements:

  1. line 5: download
  2. line 8: convert to data.tree
@gluc
gluc / convert_taxonomy.R
Last active August 29, 2015 14:24
Convert igraph-style taxonomy
library(data.tree)
library(igraph)
taxonomy <- data.frame(
children = LETTERS[2:13],
parents = c("A", "A", "B", "B", "C", "C", "C", "E", "E", "G", "H",
"H"),
child.level = c(2, 2, 3, 3, 3, 3, 3,4, 4, 4, 4, 4),
description = c("w", "i", "z", "v", "j", "u", "q", "g", "b", "c", "t",
"o"),
stringsAsFactors = FALSE
@gluc
gluc / decisionMermaid.R
Last active August 29, 2015 14:26
calculate a decision tree with data.tree, and plot it with DiagrammeR / mermaid
### This demo calculates and plots a simple decision tree
### It requires the yaml and the ape packages to be installed
library(data.tree)
library(yaml)
#load from file
fileName <- 'jennylind.yaml'
@gluc
gluc / .gitignore
Last active September 9, 2015 20:29
responses
.Rproj.user
.Rhistory
.RData
csv <- "
parent,child,share,aDirectShare
A,B,50,50
A,C,69,69
A,D,56,56
B,E,80,50
B,F,72,50
C,G,45,45
D,H,67,56
D,I,35,35
@gluc
gluc / app.R
Last active November 23, 2022 10:14
Shiny CRUD
library(shiny)
library(shinyjs)
# Get table metadata. For now, just the fields
# Further development: also define field types
# and create inputs generically
GetTableMetadata <- function() {
fields <- c(id = "Id",
@gluc
gluc / crossover.R
Last active September 29, 2015 03:06
Genetics Crossover
library(data.tree)
CreateCrossoverRandomTree <- function(n, treeId) {
tree <- CreateRandomTree(n)
tree$Set(treeId = treeId)
tree$Set(id = 1:tree$totalCount)
return (tree)
}
@gluc
gluc / LoadFunctionYaml.R
Last active October 4, 2015 11:36
data.tree YAML function
#requires data.tree 0.2.2
library(data.tree)
library(yaml)
library(stringr)
fileName <- 'jennylind.yaml'
l <- yaml.load_file(fileName)
jl <- as.Node(l)
#this file contains all the functions referred to from the yaml