R/config.r

Defines functions check_config get_config create_config

Documented in check_config create_config get_config

#' Configuration
#'
#' Creates a configuration file based on the default
#' shipped with helseatlas package.
#'
#' @param dir Folder to put config file
#' @param force Overwrite current config file
#'
#' @export
create_config <- function(dir = ".", force = FALSE) {
  ref_file <- system.file("helseatlas.yml", package = "helseatlas")
  new_file <- paste(dir, "_helseatlas.yml", sep = "/")
  if (!file.exists(new_file) | force) {
    file.copy(ref_file, to = new_file)
    return(paste0(new_file, " file created: fill it in"))
  } else {
    return(paste0("Cannot create ", new_file, " config file: already exists. ",
    "(run with force = TRUE if you want to overwrite file)"))
  }
}

#' Retrieve Config
#'
#' Retrieves config file.
#'
#' @param dir Folder location of _helseatlas.yml file
#'
#' @export
get_config <- function(dir = ".") {
  config_file <- paste(dir, "_helseatlas.yml", sep = "/")
  if (!file.exists(config_file)) {
    # Use the default if _helseatlas.yml does not exist
    config_file <- system.file("helseatlas.yml", package = "helseatlas")
  }
  config <- yaml::read_yaml(config_file)
  check_config(config)
  return(config)
}

#' Check config file
#'
#' @param config Config file to check
#'
check_config <- function(config) {
  if ((class(config) != "list") |
  (!("title" %in% attributes(config)$names)) |
  (!("menus" %in% attributes(config)$names)) |
  (!("plot" %in% attributes(config)$names)) |
  (!("num" %in% attributes(config)$names)) |
  (!("info" %in% attributes(config)$names))
  ) {
    stop("Complete the config file: _qmongr.yml")
  }
  invisible()
}
Helseatlas/shinymap documentation built on June 11, 2020, 3:38 a.m.