R/helper_functions.R

Defines functions run_API_demo_App set_hs_API_key get_hs_API_key

Documented in get_hs_API_key run_API_demo_App set_hs_API_key

#' Get the locally stored healthsites API_key
#'
#' @param env_var_name The name of the environment variable storing your
#' heatlhsites API key.
#'
#' @return The API key stored as an environment variable under the name
#' Healthsites_API_key.
#' @export
#'
#' @examples
#' \dontrun{
#' # To save the API_key into the object \code{key}
#' # this only works if an API key was saved as environment variable with
#' # the name \code{Healthsites_API_key}. To check environment variables
#' # names and their corresponding values.
#' key <- get_hs_API_key(env_var_name = "Healthsites_API_key")
#'}
get_hs_API_key <- function(env_var_name = "Healthsites_API_key") {

  Healthsites_API_key <- Sys.getenv(env_var_name)
  if (Healthsites_API_key == "") {
    stop(
      "You do not currently have an API key set. Please follow the following steps to get an API key:
        1. Establish an OpenStreetMap account at https://www.openstreetmap.org/user/new
        2. Sign into Healthsites with your OSM account: https://healthsites.io/map
        3. Get an API token from your profile page:
          - Click on your user profile to open your profile page.
          - There you will find an option to generate/view the API key/token.
        4. Set your API key using the set_hs_API_key function within bolsteR"
    )

  }
  return(Healthsites_API_key)
}

#' Set healthsites API_key as an environment variable
#'
#' @param API_key The healthsites.io API key to save as an environment
#' variable.
#'
#' @return Set the value given as an environment, but returns nothing
#' @import assertthat stringr
#' @export
#'
#' @examples
#' \dontrun{
#' # Using 123abcdefg as an API key:
#' set_hs_API_key(API_key = "123abcdefg")
#' }
set_hs_API_key <- function(API_key) {

  assertthat::assert_that(is.character(API_key),
                            length(API_key) == 1,
                            !stringr::str_detect(string = API_key,
                                                pattern = " "),
                            msg =

     "This does not match our expectation for the health state API key.
     Please follow the following steps to get an API key:
      1. Establish an OpenStreetMap account:
        - https://www.openstreetmap.org/user/new
      2. Sign into Healthsites with your OSM account:
        - https://healthsites.io/map
      3. Get an API token off your profile page:
        - Click on your user profile to open your profile page.
        - There you will find an option to generate/view the API key/token.
      4. Set your API key using the 'XXXXX' function")

  Sys.setenv('Healthsites_API_key' = API_key)
}

#' Run the example Shiny app that quires the healthsites api.
#'
#' @param example_app The example shiny app to run. Current options are hs
#' for the "Healthsites" API functions demo shiny app, or wb_dhs for
#' healthsites, World Bank, and DHS APIs' functions demo shiny app.
#'
#' @return Runs the example shiny app
#' @export
#'
#' @examples
#' \dontrun{
#' run_API_demo_App()
#' }
run_API_demo_App <- function(example_app = "demo") {
  library(bolsteR)
  appFolder <- switch(example_app, demo = "demoApp")
  appDir <- system.file("shiny-examples", appFolder,
                        package = "bolsteR")
  if (appDir == "") {
    stop("Could not find example directory. Try re-installing `bolsteR`.",
         call. = FALSE)
  }

  shiny::runApp(appDir, display.mode = "normal")
}
dark-peak-analytics/who_decide_AP documentation built on May 25, 2022, 8:31 p.m.