R/run_portal_lite.R

Defines functions get_indicator_definitions run_portal_lite

Documented in get_indicator_definitions run_portal_lite

#' @title Run the portal.
#' @param json_options JsonOptions (or ReadableJsonOptions) object.
#' @param ui Shiny UI object
#' @param server Shiny server function
#' @param ... Additional arguments passed to shiny::runApp() function
#'
#' include JsonOptions.R shiny_default_ui.R shiny_default_server.R utils.R
#' @export
#'
run_portal_lite <- function(json_options = default_json_options,
                            ui = NULL,
                            server = NULL,
                            ...) {
  # TODO: add types checks? at least for [Readable]JsonOptions
  print(getwd())
  CONFIG <<- read_config_file()
  JSON_OPTIONS <<- json_options
  indicator_definitions <- get_indicator_definitions(CONFIG$indicator_definitions)
  if (is.null(ui)) {
    ui <- get_default_ui(CONFIG$theme_css, CONFIG$primary_color, indicator_definitions)
  }
  if (is.null(server)) {
    server <- get_default_server(CONFIG, indicator_definitions)
  }
  runApp(
    list(ui = ui, server = server),
    ...
  )
}


#' @title Read viz JSON.
#'
get_indicator_definitions <- function(filepath) {
  indicator_definitions_raw <- jsonlite::read_json(filepath)
  indicator_definitions <- list()
  for (item in indicator_definitions_raw) {
    check_indicator_definition(item)
    if (is.null(item$disabled) || !item$disabled) {
      key <- paste(item$class, item$type, item$indicator_name, sep = "_")
      if (key %in% names(indicator_definitions)) {
        stop(
          paste(
            "Two indicators with the same class, type and name:\n",
            item$class,
            item$type,
            item$indicator_name
          )
        )
      } else {
        indicator_definitions[[key]] <- item
      }
    }
  }
  return(indicator_definitions)
}


# TRYING TO REPLICATE global.R (for development)
# library(shiny)
# library(highcharter)
# source("R/consts.R")
# source("R/type_checks.R")
# for (f in list.files("R/", pattern = "utils*")) source(paste0("R/", f))
# for (f in list.files("R/", pattern = "shiny*")) {
#   if (f == "shiny_default_ui.R") next
#   source(paste0("R/", f))
# }
# source("R/shiny_default_ui.R")
#
# for (f in list.files("R/", pattern = "default_*")) source(paste0("R/", f))
# source("R/JsonOptions.R")
# options(shiny.fullstacktrace = TRUE)
# runApp(get_portal_lite())
xaviermiles/portalLite documentation built on Jan. 28, 2022, 9:10 a.m.