Skip to content

Commit

Permalink
Impute document.id instead of bypass it
Browse files Browse the repository at this point in the history
Close #86
  • Loading branch information
colearendt committed Jun 14, 2017
1 parent 4ffec9d commit 34624aa
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions R/json_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ json_structure_level <- function(s) {

json_structure_objects <- function(s) {

v <- c('document.id',parent.id='child.id','seq',level ='level + 1L')
v <- v[v != 'document.id' | 'document.id' %in% names(s)]

expand_s <- s %>%
dplyr::filter(type == "object") %>%
dplyr::transmute_(
.dots=v
dplyr::transmute(
document.id
, parent.id=child.id
, seq
, level=level + 1L
) %>%
gather_object %>%
json_types %>%
Expand All @@ -180,12 +180,17 @@ json_structure_objects <- function(s) {
dplyr::mutate(
child.id = paste(parent.id, index, sep = "."),
seq = purrr::map2(seq, name, c)
)

v <- c('document.id','parent.id','level','index','child.id','seq','name','type','length')
v <- v[v != 'document.id' | 'document.id' %in% names(df_s)]
df_s <- df_s %>% dplyr::select_(
.dots=v
) %>%
dplyr::select(
document.id
, parent.id
, level
, index
, child.id
, seq
, name
, type
, length
)

# Reconstruct tbl_json object
Expand All @@ -195,29 +200,31 @@ json_structure_objects <- function(s) {

json_structure_arrays <- function(s) {

v <- c('document.id',parent.id='child.id','seq',level ='level + 1L')
v <- v[v != 'document.id' | 'document.id' %in% names(s)]

s <- s %>%
dplyr::filter(type == "array") %>%
dplyr::transmute_(
.dots=v
dplyr::transmute(
document.id
, parent.id=child.id
, seq
, level=level + 1L
) %>%
gather_array("index") %>%
json_types %>%
json_lengths %>%
dplyr::mutate(
child.id = paste(parent.id, index, sep = "."),
seq = purrr::map2(seq, index, c)
)

v <- c('document.id', 'parent.id', 'level'
, 'index', 'child.id', 'seq'
, name='NA_character_', 'type', 'length')
v <- v[v != 'document.id' | 'document.id' %in% names(s)]

s %>% dplyr::transmute_(
.dots=v
) %>%
dplyr::transmute(
document.id
, parent.id
, level
, index
, child.id
, seq
, name=NA_character_
, type
, length
)

}

0 comments on commit 34624aa

Please sign in to comment.