R/runModuleExample.R

#' Run HRI Shiny Module Example Applications
#'
#' Launch Shiny example applications, and optionally, your system's web browser.
#'
#' @param example The name of the example to run, or `NA` (the default) to
#'   list the available examples.
#' @param launch.browser If true, the system's default web browser will be
#'   launched automatically after the app is started. Defaults to true in
#'   interactive sessions only.
#' @param port shiny port option assigned in the environment
#' @param host The IPv4 address that the application should listen on. Defaults
#'   to the `shiny.host` option, if set, or `"127.0.0.1"` if not.
#' @param display.mode The mode in which to display the example. Defaults to
#'   `showcase`, but may be set to `normal` to see the example without
#'   code or commentary.
#'@param pkg parameter for the package.  Default is set to hrimodules
#'
#'
#'
#' @export

runModuleExample <-
  function (example = NA,
            port = getOption("shiny.port"),
            launch.browser = getOption("shiny.launch.browser",
                                       interactive()),
            host = getOption("shiny.host",
                             "127.0.0.1"),
            display.mode = c("auto", "normal",
                             "showcase"),
            pkg = "hrimodules")
  {
    examplesDir <- system.file("examples", package = pkg)

    resolve<-function (dir, relpath)
    {
      abs.path <- file.path(dir, relpath)
      if (!file.exists(abs.path))
        return(NULL)
      abs.path <- normalizePath(abs.path, winslash = "/",
                                mustWork = TRUE)
      dir <- normalizePath(dir, winslash = "/", mustWork = TRUE)
      if (.Platform$OS.type == "windows")
        dir <- sub("/$", "", dir)
      if (nchar(abs.path) <= nchar(dir) + 1)
        return(NULL)
      if (substr(abs.path, 1, nchar(dir)) != dir || substr(abs.path,
                                                           nchar(dir) + 1, nchar(dir) + 1) != "/") {
        return(NULL)
      }
      return(abs.path)
    }

    dir <- resolve(examplesDir, example)
    if (is.null(dir)) {
      if (is.na(example)) {
        errFun <- message
        errMsg <- ""
      }
      else {
        errFun <- stop
        errMsg <- paste("Example", example, "does not exist. ")
      }
      errFun(errMsg,
             "Valid examples are \"",
             paste(list.files(examplesDir),
                   collapse = "\", \""),
             "\"")
    }
    else {
      runApp(
        dir,
        port = port,
        host = host,
        launch.browser = launch.browser,
        display.mode = display.mode
      )
    }
  }
HarryRosen/hrimodules documentation built on Jan. 11, 2022, 12:36 a.m.