Json wrangling (non-hardcoded)
#test
dirX="/Users/gerhard/msc-thesis-data/gridtest"
wrangle <- function(dirX){
require(jsonlite)
files <- list.files(path=paste0(dirX),
pattern="*json",
full.names=T,
recursive=TRUE)
j <- fromJSON(files[1])
print("---------------------------------------------------------------------------------")
print(paste0("Processing jsons to list:"))
for(i in 2:length(files)){
print(100*(i/length(files)))
f <- fromJSON(files[i])
j <- c(j,f)
}
j
}
dat <- wrangle(dirX)
## Loading required package: jsonlite
## [1] "---------------------------------------------------------------------------------"
## [1] "Processing jsons to list:"
## [1] 50
## [1] 75
## [1] 100
print("---------------------------------------------------------------------------------")
## [1] "---------------------------------------------------------------------------------"
print("DONE")
## [1] "DONE"
print("---------------------------------------------------------------------------------")
## [1] "---------------------------------------------------------------------------------"
print("Extracting layers and momenta...")
## [1] "Extracting layers and momenta..."
p <- sapply(dat,`[[`,"P")
pdg <- sapply(dat, `[[`,"pdgCode")
layer_0 <- sapply(dat, `[[`,"layer 0")
layer_1 <- sapply(dat, `[[`,"layer 1")
layer_2 <- sapply(dat, `[[`,"layer 2")
layer_3 <- sapply(dat, `[[`,"layer 3")
layer_4 <- sapply(dat, `[[`,"layer 4")
layer_5 <- sapply(dat, `[[`,"layer 5")
print("---------------------------------------------------------------------------------")
## [1] "---------------------------------------------------------------------------------"
print("Removing NULLS")
## [1] "Removing NULLS"
n <- unique(
c(
which(sapply(layer_0, is.null)),
which(sapply(layer_1, is.null)),
which(sapply(layer_2, is.null)),
which(sapply(layer_3, is.null)),
which(sapply(layer_4, is.null)),
which(sapply(layer_5, is.null))
)
)
pdg <- pdg[-n]
layer_0 <- layer_0[-n]
layer_1 <- layer_1[-n]
layer_2 <- layer_2[-n]
layer_3 <- layer_3[-n]
layer_4 <- layer_4[-n]
layer_5 <- layer_5[-n]
p <- p[-n]
print("---------------------------------------------------------------------------------")
## [1] "---------------------------------------------------------------------------------"
print("Removing empties:")
## [1] "Removing empties:"
e <- unique(
as.numeric(which(sapply(layer_0, "typeof")=="list")),
as.numeric(which(sapply(layer_1, "typeof")=="list")),
as.numeric(which(sapply(layer_2, "typeof")=="list")),
as.numeric(which(sapply(layer_3, "typeof")=="list")),
as.numeric(which(sapply(layer_4, "typeof")=="list")),
as.numeric(which(sapply(layer_5, "typeof")=="list"))
)
pdg <- pdg[-e]
layer_0 <- layer_0[-e]
layer_1 <- layer_1[-e]
layer_2 <- layer_2[-e]
layer_3 <- layer_3[-e]
layer_4 <- layer_4[-e]
layer_5 <- layer_5[-e]
p <- p[-e]