Skip to content

Commit

Permalink
c2 updated
Browse files Browse the repository at this point in the history
  • Loading branch information
renkun-ken committed Mar 28, 2014
1 parent 26a0269 commit 979e943
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions data/test1.user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"sample": 200,
"functions": {
"runif": {
"max": 12
},
"rbinom": {
"size": 12
}
},
"aggregate": ["mean","sd","median"]
}
27 changes: 27 additions & 0 deletions src/c2.jsonlite.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ library(jsonlite)

### Exampel 1
get.df <- function(json) {
require(jsonlite)
profile <- fromJSON(json,simplifyDataFrame = FALSE)
message(sprintf("Profile '%s' loaded", profile$title))
rows <- data.frame(lapply(names(profile$functions),function(fun) {
Expand All @@ -23,3 +24,29 @@ get.df <- function(json) {
}

df <- get.df("data/test1.json")

### Example 2: modifyList()

get.df2 <- function(name,format="data/%s.json",update=".user") {
require(jsonlite)
file.default <- sprintf(format,name)
file.user <- sprintf(format,paste0(name,update))
profile <- fromJSON(file.default,simplifyDataFrame = FALSE)
if(file.exists(file.user)) {
profile.user <- fromJSON(file.user,simplifyDataFrame = FALSE)
profile <- modifyList(profile,profile.user)
}
str(profile)
message(sprintf("Profile '%s' loaded", profile$title))
rows <- data.frame(lapply(names(profile$functions),function(fun) {
do.call(fun,c(list(n=profile$sample),profile$functions[[fun]]))
}))
colnames(rows) <- names(profile$functions)
aggs <- data.frame(lapply(profile$aggregate, function(fun) {
apply(rows,MARGIN = 1,FUN = get(fun))
}))
colnames(aggs) <- profile$aggregate
cbind(rows,aggs)
}

df <- get.df2("test1")

0 comments on commit 979e943

Please sign in to comment.