Last active
October 4, 2015 11:36
-
-
Save gluc/bbd9b9d38baf2475ecf2 to your computer and use it in GitHub Desktop.
data.tree YAML function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This demonstrates how to add functions to a YAML. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Remainder <- function(self) { | |
1 - sum( Get(self$siblings, "p")) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Jenny Lind | |
type: decision | |
Sign with Movie Company: | |
type: chance | |
Small Box Office: | |
type: terminal | |
p: 0.3 | |
payoff: 200000 | |
Medium Box Office: | |
type: terminal | |
p: 0.6 | |
payoff: 1000000 | |
Large Box Office: | |
type: terminal | |
p: "function:Remainder" | |
payoff: 3000000 | |
Sign with TV Network: | |
type: chance | |
Small Box Office: | |
type: terminal | |
p: 0.3 | |
payoff: 900000 | |
Medium Box Office: | |
type: terminal | |
p: 0.6 | |
payoff: 900000 | |
Large Box Office: | |
type: terminal | |
p: "function:Remainder" | |
payoff: 900000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
source("functions.R") | |
#function to replace the function tags with their actual functions | |
ReplaceFunction <- function(x) { | |
for( field in x$fields) { | |
if (str_detect(x[[field]], "function[:][:alpha:]+")) { | |
functionName <- str_sub(x[[field]], str_locate(x[[field]], ":")[1, 1] + 1) | |
x[[field]] <- eval(parse(text = functionName)) | |
} | |
} | |
} | |
#actually do the replacement | |
jl$Do(ReplaceFunction) | |
print(jl, "p") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment