Skip to content

Commit 09e8ea8

Browse files
committed
#401 work on hierarchy
1 parent 115c09f commit 09e8ea8

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

R/geoflow_utils.R

+17-6
Original file line numberDiff line numberDiff line change
@@ -926,18 +926,29 @@ create_object_identification_id = function(prefix, str){
926926
#'
927927
#'@param data data
928928
#'@param parent parent
929+
#'@param parent_key column that identifies the parent
930+
#'@param child_key column that identifies the child
931+
#'@param recursive if the function is called recursively. Default is \code{TRUE}
932+
#'to build the full hierarchy. Can be set to \code{FALSE} to allow lazy loading
933+
#'in a Shiny context.
929934
#'@return a hierarchical list
930935
#'@export
931-
build_hierarchical_list <- function(data, parent, parent_key, child_key) {
936+
build_hierarchical_list <- function(data, parent, parent_key, child_key, recursive = TRUE) {
932937
children <- data[data[,parent_key] == parent, ]
933938
children <- children[order(children[,child_key]),]
939+
out <- list(text = parent)
934940
if (nrow(children) == 0) {
935-
return(NULL)
941+
return(out)
936942
} else {
937-
nested_list <- lapply(1:nrow(children), function(i) {
938-
build_hierarchical_list(data, children[i, child_key], parent_key, child_key)
943+
out$children <- lapply(1:nrow(children), function(i) {
944+
if(recursive){
945+
build_hierarchical_list(data, children[i, child_key], parent_key, child_key, recursive)
946+
}else{
947+
list(
948+
text = children[i, child_key]
949+
)
950+
}
939951
})
940-
names(nested_list) <- children[,child_key]
941-
return(nested_list)
942952
}
953+
return(out)
943954
}

R/geoflow_vocabulary.R

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary",
167167
ORDER BY ?concept
168168
")
169169
out = self$query(str = str, mimetype = mimetype)
170+
out[is.na(out$broaderPrefLabel),]$broaderPrefLabel = "root"
170171
if(out_format == "list"){
171172
out = build_hierarchical_list(
172173
as.data.frame(out),

0 commit comments

Comments
 (0)