R/wizard.R

Defines functions wizard

Documented in wizard

#' Interactive Configuration Wizard
#'
#' Walks through setup questions and generates a _shinyelectron.yml
#' configuration file for your Shiny app.
#'
#' @param appdir Character string. Path to the app directory.
#' @return Invisible path to the generated config file.
#'
#' @seealso [init_config()] to create a template config file;
#'   [show_config()] to display the merged effective configuration.
#'
#' @examplesIf interactive()
#' wizard(tempdir())
#'
#' @export
wizard <- function(appdir) {
  if (!interactive()) {
    cli::cli_abort("wizard() must be run interactively")
  }

  cli::cli_h1("shinyelectron Configuration Wizard")
  cli::cli_alert_info("This will create a {.file _shinyelectron.yml} in {.path {appdir}}")
  cat("\n")

  # App name
  default_name <- app_dir_name(appdir)
  app_name <- readline(paste0("App name [", default_name, "]: "))
  if (!nzchar(app_name)) app_name <- default_name

  # App version
  app_version <- readline("App version [1.0.0]: ")
  if (!nzchar(app_version)) app_version <- "1.0.0"

  # Language
  cat("\nLanguage:\n")
  cat("  1. r-shiny  -- R Shiny\n")
  cat("  2. py-shiny -- Python Shiny\n")
  type_choice <- readline("Choose language [1]: ")
  app_type <- switch(type_choice,
    "2" = "py-shiny",
    "r-shiny"
  )

  # Runtime strategy (always ask; default shinylive)
  cat("\nRuntime strategies:\n")
  cat("  1. shinylive     -- Browser-WebAssembly (no runtime on disk, recommended)\n")
  cat("  2. auto-download -- Download R/Python on first launch\n")
  cat("  3. system        -- Use R/Python already installed on user's machine\n")
  cat("  4. bundled       -- Embed runtime in app (large but self-contained)\n")
  cat("  5. container     -- Run in Docker/Podman container\n")
  strategy_choice <- readline("Choose runtime strategy [1]: ")
  runtime_strategy <- switch(strategy_choice,
    "2" = "auto-download", "3" = "system", "4" = "bundled", "5" = "container",
    "shinylive"
  )

  # Platforms. export() builds for these. An empty answer leaves them out
  # of the config, so each build targets the machine it runs on. A bundled
  # or auto-download build embeds a runtime for one platform, so for those
  # strategies ask again until the answer names at most one.
  one_platform <- runtime_strategy %in% c("bundled", "auto-download")
  cat("\nTarget platforms (comma-separated):\n")
  cat("  mac, win, linux\n")
  repeat {
    platform_input <- trimws(readline("Platforms [this machine]: "))
    platforms <- if (nzchar(platform_input)) {
      unique(trimws(strsplit(platform_input, ",")[[1]]))
    }
    if (!one_platform || length(platforms) <= 1) break
    cat(sprintf("  The %s strategy builds one platform at a time. Enter one platform.\n",
                runtime_strategy))
  }

  valid_platforms <- SHINYELECTRON_DEFAULTS$valid_platforms
  invalid_platforms <- platforms[!platforms %in% valid_platforms]
  if (length(invalid_platforms) > 0) {
    cli::cli_abort(c(
      "Invalid platform token(s): {.val {invalid_platforms}}",
      "i" = "Valid platforms: {.val {valid_platforms}}"
    ))
  }

  # Numeric prompt helper: re-prompt on invalid input and fall back to the
  # default on an empty answer (also covers non-interactive sessions, where
  # readline() returns ""). Returns an integer so the config never stores NA.
  read_numeric <- function(prompt, default, min_value) {
    repeat {
      input <- trimws(readline(prompt))
      if (!nzchar(input)) return(default)
      val <- suppressWarnings(as.integer(input))
      if (!is.na(val) && val >= min_value) return(val)
      cat(sprintf("  Please enter a whole number >= %d.\n", min_value))
    }
  }

  # Window dimensions
  width <- read_numeric("Window width [1200]: ", 1200L, 100L)
  height <- read_numeric("Window height [800]: ", 800L, 100L)

  # Port
  port <- read_numeric("Server port [3838]: ", 3838L, 1L)

  # --- Advanced options ---
  cat("\n")
  advanced <- readline("Configure advanced options (signing, tray, updates)? [y/N]: ")
  do_advanced <- tolower(advanced) %in% c("y", "yes")

  # Code signing
  sign_enabled <- FALSE
  signing_config <- NULL
  if (do_advanced) {
    cat("\n")
    cli::cli_h2("Code Signing")
    sign_input <- readline("Enable code signing for distribution? [y/N]: ")
    sign_enabled <- tolower(sign_input) %in% c("y", "yes")
    if (sign_enabled) {
      signing_config <- list(sign = TRUE)
      if ("mac" %in% (platforms %||% detect_current_platform())) {
        notarize <- readline("  Enable macOS notarization? [y/N]: ")
        signing_config$mac <- list(notarize = tolower(notarize) %in% c("y", "yes"))
      }
    }
  }

  # System tray
  tray_config <- NULL
  if (do_advanced) {
    cat("\n")
    cli::cli_h2("System Tray")
    tray_input <- readline("Enable system tray icon? [y/N]: ")
    if (tolower(tray_input) %in% c("y", "yes")) {
      close_to_tray <- readline("  Close to tray instead of quitting? [y/N]: ")
      tray_config <- list(
        enabled = TRUE,
        close_to_tray = tolower(close_to_tray) %in% c("y", "yes")
      )
    }
  }

  # Auto-updates
  updates_config <- NULL
  if (do_advanced) {
    cat("\n")
    cli::cli_h2("Auto-Updates")
    updates_input <- readline("Enable auto-updates? [y/N]: ")
    if (tolower(updates_input) %in% c("y", "yes")) {
      # The wizard sets up GitHub Releases; enable_auto_updates() also takes
      # the generic HTTP provider.
      provider <- "github"
      updates_config <- list(enabled = TRUE, provider = provider)
      owner <- readline("  GitHub owner/org: ")
      repo <- readline("  GitHub repo name: ")
      if (nzchar(owner) && nzchar(repo)) {
        updates_config$github <- list(owner = owner, repo = repo)
      }
    }
  }

  # Dependency management (only when a real runtime is in play)
  deps_config <- NULL
  if (runtime_strategy != "shinylive" && do_advanced) {
    cat("\n")
    cli::cli_h2("Dependencies")
    prompt_install <- readline("Prompt user before installing packages? [y/N]: ")
    prompt_runtime <- readline("Let user pick runtime version? [y/N]: ")
    if (tolower(prompt_install) %in% c("y", "yes") ||
        tolower(prompt_runtime) %in% c("y", "yes")) {
      deps_config <- list(
        prompt_before_install = tolower(prompt_install) %in% c("y", "yes"),
        prompt_runtime_version = tolower(prompt_runtime) %in% c("y", "yes")
      )
    }
  }

  # Build the YAML content. The slug is written out so the app keeps its
  # identity when the name changes later. As in export(), it comes from the
  # directory name; the app name only sets the display name.
  slug <- resolve_app_slug(list(), NULL, appdir)
  config <- list(
    app = Filter(Negate(is.null), list(name = app_name, slug = slug, version = app_version)),
    build = Filter(Negate(is.null), list(type = app_type, platforms = platforms))
  )

  config$build$runtime_strategy <- runtime_strategy

  config$window <- list(width = width, height = height)
  config$server <- list(port = port)

  if (!is.null(signing_config)) config$signing <- signing_config
  if (!is.null(tray_config)) config$tray <- tray_config
  if (!is.null(updates_config)) config$updates <- updates_config
  if (!is.null(deps_config)) config$lifecycle <- deps_config

  # Write the config
  config_path <- file.path(appdir, "_shinyelectron.yml")

  if (file.exists(config_path)) {
    overwrite <- readline("Config file already exists. Overwrite? [y/N]: ")
    if (!tolower(overwrite) %in% c("y", "yes")) {
      cli::cli_alert_info("Aborted. Existing config unchanged.")
      return(invisible(NULL))
    }
  }

  yaml::write_yaml(config, config_path)

  # Confirm the generated file round-trips through the validator.
  validate_config_file(config_path)

  cli::cli_alert_success("Created {.file {config_path}}")
  if (is.null(slug)) alert_missing_slug()
  cat("\n")
  cli::cli_alert_info("Next steps:")
  cli::cli_alert_info("  1. Review and edit {.file _shinyelectron.yml}")
  cli::cli_alert_info("  2. Run {.code app_check(\"{appdir}\")} to validate")
  cli::cli_alert_info("  3. Run {.code export(\"{appdir}\", \"output\")} to build")

  invisible(config_path)
}

Try the shinyelectron package in your browser

Any scripts or data that you put into this service are public.

shinyelectron documentation built on Dec. 3, 2026, 9:07 a.m.