Skip to content

Commit 1f10aae

Browse files
committed
#401 faster get_concepts_hierarchy
1 parent 09e8ea8 commit 1f10aae

5 files changed

+51
-28
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export(list_vocabularies)
8888
export(loadMetadataHandler)
8989
export(load_workflow_environment)
9090
export(posix_to_str)
91+
export(precompute_relationships)
9192
export(register_actions)
9293
export(register_contact_handlers)
9394
export(register_data_accessors)

R/geoflow_utils.R

+25-20
Original file line numberDiff line numberDiff line change
@@ -918,37 +918,42 @@ create_object_identification_id = function(prefix, str){
918918
paste(prefix, digest::digest(object = str, algo = "crc32", serialize = FALSE), sep = "_")
919919
}
920920

921+
922+
#'@description precompute_relationships
923+
#'@aliases precompute_relationships
924+
#'@title precompute_relationships
925+
#'
926+
#'@usage precompute_relationships(data, parent_key, child_key)
927+
#'
928+
#'@param data data
929+
#'@param parent_key parent_key
930+
#'@param child_key child_key
931+
#'@return a list of relationships
932+
#'@export
933+
precompute_relationships <- function(data, parent_key, child_key) {
934+
ordered_data <- data[order(data[[parent_key]], data[[child_key]]), ]
935+
relationships <- split(ordered_data[[child_key]], ordered_data[[parent_key]])
936+
return(relationships)
937+
}
938+
939+
921940
#'@name build_hierarchical_list
922941
#'@aliases build_hierarchical_list
923942
#'@title build_hierarchical_list
924943
#'
925944
#'@usage build_hierarchical_list(data, parent)
926945
#'
927-
#'@param data data
928946
#'@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.
947+
#'@param relationships relationships
934948
#'@return a hierarchical list
935949
#'@export
936-
build_hierarchical_list <- function(data, parent, parent_key, child_key, recursive = TRUE) {
937-
children <- data[data[,parent_key] == parent, ]
938-
children <- children[order(children[,child_key]),]
950+
build_hierarchical_list <- function(parent, relationships) {
951+
children <- relationships[[parent]]
939952
out <- list(text = parent)
940-
if (nrow(children) == 0) {
941-
return(out)
953+
if (is.null(children)) {
954+
out$icon = "fa-regular fa-note-sticky"
942955
} else {
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-
}
951-
})
956+
out$children <- lapply(children, build_hierarchical_list, relationships)
952957
}
953958
return(out)
954959
}

R/geoflow_vocabulary.R

+2-6
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,8 @@ geoflow_skos_vocabulary <- R6Class("geoflow_skos_vocabulary",
169169
out = self$query(str = str, mimetype = mimetype)
170170
out[is.na(out$broaderPrefLabel),]$broaderPrefLabel = "root"
171171
if(out_format == "list"){
172-
out = build_hierarchical_list(
173-
as.data.frame(out),
174-
parent = "root",
175-
parent_key = "broaderPrefLabel",
176-
child_key = "prefLabel"
177-
)
172+
relationships <- precompute_relationships(as.data.frame(out), "broaderPrefLabel", "prefLabel");
173+
out <- build_hierarchical_list("root", relationships)
178174
}
179175
return(out)
180176
},

man/build_hierarchical_list.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/precompute_relationships.Rd

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)