Skip to content

Commit

Permalink
populate selectise from server side
Browse files Browse the repository at this point in the history
  • Loading branch information
catalamarti committed Oct 2, 2024
1 parent 68b1931 commit c987766
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 281 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Imports:
dplyr,
DT,
glue,
here,
omopgenerics (>= 0.2.3),
purrr,
rlang,
Expand All @@ -45,6 +44,7 @@ Suggests:
duckdb,
ggplot2,
gt,
here,
htmlwidgets,
knitr,
omock,
Expand All @@ -56,8 +56,8 @@ Suggests:
sortable,
testthat (>= 3.1.5),
utils,
zip,
webshot2
webshot2,
zip
VignetteBuilder: knitr
Config/testthat/edition: 3
Config/testthat/parallel: true
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
export(exportStaticApp)
export(exportSummarisedResult)
export(filterData)
export(getChoices)
export(importSummarisedResult)
export(launchDynamicApp)
export(tidyData)
export(visTable)
importFrom(omopgenerics,emptySummarisedResult)
importFrom(omopgenerics,exportSummarisedResult)
importFrom(omopgenerics,importSummarisedResult)
importFrom(rlang,.data)
Expand Down
8 changes: 0 additions & 8 deletions R/appStatic.R
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,3 @@ subs <- function(x, pat, subst) {
}
return(x)
}
emptySummarisedResult <- function() {
omopgenerics::emptySummarisedResult(settings = dplyr::tibble(
result_id = integer(),
result_type = character(),
package_name = character(),
package_version = character()
))
}
14 changes: 0 additions & 14 deletions R/buttons.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@

selector <- function(id, lab, cho, sel, mult) {
if(stringr::str_detect(id, "variable_name") ||
stringr::str_detect(id, "summarise_large_scale_characteristics_grouping_concept_id")){
'shiny::selectizeInput(
inputId = "{id}",
label = "{lab}",
choices = NULL,
selected = NULL,
multiple = {mult},
options = list(plugins = "remove_button")
)' |>
glue::glue() |>
as.character()
} else {
'shiny::selectizeInput(
inputId = "{id}",
label = "{lab}",
Expand All @@ -23,7 +10,6 @@ selector <- function(id, lab, cho, sel, mult) {
)' |>
glue::glue() |>
as.character()
}
}
downloadTable <- function(id, lab, type = NULL) {

Expand Down
41 changes: 31 additions & 10 deletions R/choices.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#' Get the different options that a summarised_result have.
#'
#' @param result A `<summarised_result>` object.
#' @param flatten Whether to flatten to a single list or not.
#'
#' @return A named list with the options
#' @export
Expand All @@ -16,22 +17,32 @@
#'
#' getChoices(result)
#'
getChoices <- function(result) {
getChoices <- function(result, flatten = FALSE) {
# initial checks
result <- omopgenerics::validateResultArguemnt(result)
result <- omopgenerics::validateResultArgument(result)
omopgenerics::assertLogical(flatten, length = 1)

# get choices
settings <- getPossibleSettings(result)
grouping <-getPossibleGrouping(result)
variables <- getPossibleVariables(result)
choices <- names(variables) |>
purrr::set_names() |>
purrr::map(\(x) list(
settings = settings[[x]],
grouping = grouping[[x]],
variable_name = variables[[x]]$variable_name,
estimate_name = variables[[x]]$estimate_name
))

if (flatten) {
choices <- c(
correctNames(settings, "settings"),
correctNames(grouping, "grouping"),
correctNames(variables)
)

Check warning on line 35 in R/choices.R

View check run for this annotation

Codecov / codecov/patch

R/choices.R#L31-L35

Added lines #L31 - L35 were not covered by tests
} else {
choices <- names(variables) |>
purrr::set_names() |>
purrr::map(\(x) list(
settings = settings[[x]],
grouping = grouping[[x]],
variable_name = variables[[x]]$variable_name,
estimate_name = variables[[x]]$estimate_name
))
}

return(choices)
}
Expand Down Expand Up @@ -82,3 +93,13 @@ getPossibilities <- function(x, split = FALSE) {
purrr::map(getPos, split = split)
return(x)
}
correctNames <- function(x, prefix = "") {
if (prefix == "") {
sub <- "_"

Check warning on line 98 in R/choices.R

View check run for this annotation

Codecov / codecov/patch

R/choices.R#L97-L98

Added lines #L97 - L98 were not covered by tests
} else {
sub <- paste0("_", prefix, "_")

Check warning on line 100 in R/choices.R

View check run for this annotation

Codecov / codecov/patch

R/choices.R#L100

Added line #L100 was not covered by tests
}
x <- unlist(x, recursive = FALSE)
names(x) <- gsub(".", sub, names(x), fixed = TRUE)
return(x)

Check warning on line 104 in R/choices.R

View check run for this annotation

Codecov / codecov/patch

R/choices.R#L102-L104

Added lines #L102 - L104 were not covered by tests
}
72 changes: 34 additions & 38 deletions R/createServer.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@

createServer <- function(resultTypes, data) {
selectiseServer <- c(
'# fill selectise variables ----')

# shiny::observe({
# choices <- omopViewer::getChoices([data])
# })
# "updateSelectizeInput(session, ",
# '"',
# rt, "_variable_name", '"',
# ", ",
# "choices = unique(visOmopResults::filterSettings(data, result_type ==",
# '"',
# rt,
# '"',
# ")$variable_name)", ", ",
# "selected = unique(visOmopResults::filterSettings(data, result_type ==",
# '"',
# rt,
# '"',
# ")$variable_name)",
# ", ",
# "server = TRUE)"
# )

purrr::map_chr(resultTypes, \(x) {
c(glue::glue("# {x} -----"),
glue::glue("## raw {x} -----"),
rawServer(x, data),
glue::glue("## tidy {x} -----"),
tidyServer(x, data),
glue::glue("## formatted {x} -----"),
formattedServer(x, data),
glue::glue("## plot {x} -----"),
plotsServer(x, data),
"\n"
) |>
paste0(collapse = "\n")
})
c(
selectiseServer(resultTypes, data),
purrr::map_chr(resultTypes, \(x) {
c(glue::glue("# {x} -----"),
glue::glue("## raw {x} -----"),
rawServer(x, data),
glue::glue("## tidy {x} -----"),
tidyServer(x, data),
glue::glue("## formatted {x} -----"),
formattedServer(x, data),
glue::glue("## plot {x} -----"),
plotsServer(x, data),
"\n"
) |>
paste0(collapse = "\n")
})
)
}
selectiseServer <- function(resultTypes, data) {
if (length(resultTypes) == 0) return(character())
c(
'# fill selectise variables ----',
paste0('shiny::observe({
choices <- omopViewer::getChoices(', data, ', flatten = TRUE)
for (k in seq_along(choices)) {
shiny::updateSelectizeInput(
session,
inputId = names(choices)[k],
choices = choices[[k]],
selected = choices[[k]]
)
}
})')
)
}
9 changes: 9 additions & 0 deletions R/filterData.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
filterData <- function(result,
resultType,
input) {
# initial check
result <- omopgenerics::validateResultArguemnt(result)
omopgenerics::assertCharacter(resultType)

# filter result type
result <- result |>
visOmopResults::filterSettings(.data$result_type == .env$resultType)
if (nrow(result) == 0) return(emptySummarisedResult())

Check warning on line 22 in R/filterData.R

View check run for this annotation

Codecov / codecov/patch

R/filterData.R#L22

Added line #L22 was not covered by tests

if (length(input) == 0) {
inputs <- character()
} else {
Expand All @@ -37,6 +44,8 @@ filterData <- function(result,
result <- result |>
dplyr::filter(.data$result_id %in% set$result_id)

if (nrow(result) == 0) return(emptySummarisedResult())

Check warning on line 47 in R/filterData.R

View check run for this annotation

Codecov / codecov/patch

R/filterData.R#L47

Added line #L47 was not covered by tests

# filter grouping
cols <- c(
"cdm_name", "group_name", "group_level", "strata_name", "strata_level",
Expand Down
4 changes: 4 additions & 0 deletions R/reexports.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ omopgenerics::importSummarisedResult
#' @importFrom omopgenerics exportSummarisedResult
#' @export
omopgenerics::exportSummarisedResult

#' @importFrom omopgenerics emptySummarisedResult
#' @export
omopgenerics::emptySummarisedResult()
2 changes: 1 addition & 1 deletion R/sidebar.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ getSidebarInformation <- function(tab) {
getSidebarChoices <- function(choi, tit, prefix) {
if (length(choi) == 0) return(NULL)
selectors <- purrr::map_chr(names(choi), \(x) selector(
paste0(prefix, "_", x), formatTit(x), cast(choi[[x]]), cast(choi[[x]]), TRUE
paste0(prefix, "_", x), formatTit(x), 'NULL', 'NULL', TRUE
)) |>
paste0(collapse = ",\n")
'bslib::accordion_panel(
Expand Down
30 changes: 30 additions & 0 deletions man/getChoices.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c987766

Please sign in to comment.