Nothing
#' Resolve the dependencies of every app in a suite
#'
#' The Python apps of a suite share one runtime, so they share the
#' requirements at the suite root (`requirements.txt` or `pyproject.toml`)
#' when it lists any. An R app, or a Python app in a suite without root
#' requirements, is read on its own. Shinylive apps manage their own packages
#' and are skipped, and the suite's requirements are not read when every
#' Python app is one.
#'
#' Stops when the bundled Python apps pin one package to different versions,
#' as [check_py_pin_conflicts()] describes.
#'
#' @param appdir Character. The suite root.
#' @param app_type Character. The suite's default app type.
#' @param config List. The suite configuration, with its paths resolved.
#' @param verbose Logical. Whether to report the suite-level packages.
#' @return A list named by app id, each entry `NULL` or as
#' [resolve_app_dependencies()] returns.
#' @keywords internal
resolve_suite_dependencies <- function(appdir, app_type, config, verbose = TRUE) {
types <- vapply(config$apps, function(a) resolve_app_type(a, config), character(1))
strategies <- vapply(config$apps, function(a) resolve_app_strategy(a, config),
character(1))
is_python <- grepl("^py-", types)
suite_py_deps <- NULL
if (grepl("^py-", app_type) && any(is_python & strategies != "shinylive")) {
config_deps <- config$dependencies %||% SHINYELECTRON_DEFAULTS$dependencies
suite_reqs <- read_py_requirements(appdir)
if (length(suite_reqs$packages) > 0 || length(suite_reqs$direct) > 0) {
merged <- resolve_py_dependencies(appdir, config_deps, reqs = suite_reqs)
suite_py_deps <- list(
language = "python",
packages = merged$packages,
requirements = merged$requirements,
index_urls = merged$index_urls
)
if (verbose) {
cli::cli_alert_info("Detected {length(suite_py_deps$packages)} Python package dependencies (suite-level)")
cli::cli_alert_info("Packages: {paste(suite_py_deps$packages, collapse = ', ')}")
}
} else if (isTRUE(config_deps$auto_detect %||% TRUE)) {
# The file lists no packages to share, so each app is read on its own,
# but its pip options are still reported.
warn_py_requirements_ignored(suite_reqs$ignored, appdir)
}
}
deps <- lapply(seq_along(config$apps), function(i) {
if (strategies[[i]] == "shinylive") return(NULL)
if (is_python[[i]] && !is.null(suite_py_deps)) return(suite_py_deps)
resolve_app_dependencies(
resolve_config_path(config$apps[[i]]$path, appdir),
types[[i]], strategies[[i]], config
)
})
names(deps) <- vapply(config$apps, function(a) a$id, character(1))
bundled_py <- is_python & strategies == "bundled"
specs <- lapply(deps[bundled_py], function(d) d$requirements %||% d$packages)
names(specs) <- vapply(config$apps[bundled_py], function(a) a$name %||% a$id,
character(1))
check_py_pin_conflicts(specs)
deps
}
#' Export multi-app Shiny suite as Electron application
#'
#' `export()` settles the suite's slug before it calls this function. When
#' called directly without `app.slug`, the slug comes from the resolved
#' suite name, as it did in earlier releases.
#'
#' @param slug_pinned Logical. Whether the configuration sets `app.slug`, for
#' the progress output; `export()` passes it because it fills in the slug
#' before the call.
#' @keywords internal
export_multi_app <- function(appdir, destdir, config,
app_name = NULL,
runtime_strategy = NULL, sign = FALSE,
platform = NULL, arch = NULL, icon = NULL,
overwrite = FALSE, build = TRUE,
run_after = FALSE, open_after = FALSE,
verbose = TRUE,
slug_pinned = !is.null(config$app$slug)) {
force(slug_pinned)
name_from_config <- is.null(app_name) && !is.null(config$app$name)
app_name <- app_name %||% config$app$name %||% app_dir_name(appdir)
validate_app_name(app_name, field = if (name_from_config) "app.name" else "app_name")
config$app$slug <- resolve_app_slug(config, app_name, appdir)
if (build) check_app_slug(config$app$slug)
# Paths in the suite config are relative to the suite root. export() has
# already resolved them; resolving again is a no-op for absolute paths and
# covers a config passed in directly.
config <- resolve_config_paths(config, appdir)
config <- drop_missing_config_files(config, base_dir = appdir)
# Target platforms and architectures: argument > build.platforms and
# build.architectures > the build machine. export() passes its own
# arguments and has already checked the same targets against the build
# machine.
targets <- resolve_build_targets(platform, arch, config)
# Validate the icon up front, matching the single-app path.
if (!is.null(icon)) {
validate_icon(icon, targets$platform)
}
# Normalize suite-level build.type (may be legacy) and resolve strategy
raw_type <- config$build$type %||% "r-shiny"
suite_normalized <- normalize_app_type_arg(raw_type, runtime_strategy %||% config$build$runtime_strategy)
app_type <- suite_normalized$app_type %||% "r-shiny"
runtime_strategy <- runtime_strategy %||% suite_normalized$runtime_strategy %||% config$build$runtime_strategy %||% "shinylive"
# The runtime_strategy argument must win over the config file's suite default,
# exactly as it does for single apps. Write the resolved value back so that
# resolve_app_strategy() and validate_suite_strategies() use the caller's
# choice for every app that does not set its own per-app runtime_strategy.
config$build$runtime_strategy <- runtime_strategy
if (verbose) {
cli::cli_h1("Exporting multi-app Shiny suite to Electron")
alert_app_identity("Suite", app_name, config$app$slug, slug_pinned)
cli::cli_alert_info("Apps: {length(config$apps)}")
cli::cli_alert_info("Default type: {.val {app_type}}")
}
# Validate multi-app config
validate_multi_app_config(config, appdir)
# Reject conflicting native runtime strategies within a language before
# staging (e.g. one bundled and one auto-download R app in the suite).
validate_suite_strategies(config$apps, config)
# A bundled or auto-download app takes one target. build_multi_app() stops
# on more, but only after every app is staged, so check here first.
if (build) {
strategies <- vapply(config$apps, function(a) resolve_app_strategy(a, config),
character(1))
check_single_target(strategies, targets$platform, targets$arch,
apps = config$apps, from_config = targets$from_config)
warn_if_electron_unsupported(config)
}
# Local R packages go into the shared bundled R library, so the suite needs
# a bundled R app. Paths resolve against the suite root, whose config is the
# only one read; check and read them before anything is copied or
# downloaded. The config then holds what was read, for the build to use.
suite_bundled_r <- any(vapply(config$apps, function(a) {
grepl("^r-", resolve_app_type(a, config)) &&
identical(resolve_app_strategy(a, config), "bundled")
}, logical(1)))
local_packages <- resolve_local_packages(
config$dependencies$r$local_packages,
base_dir = appdir,
bundled_r = suite_bundled_r
)
if (length(local_packages) > 0) {
config$dependencies$r$local_packages <- local_packages
}
local_names <- local_r_package_names(local_packages)
# Without dependencies.r.version, the R apps that embed, download or run R in
# a container use the newest portable R for the first target platform. Look
# it up once here; the build reads it from the config.
suite_needs_r <- any(vapply(config$apps, function(a) {
grepl("^r-", resolve_app_type(a, config)) &&
resolve_app_strategy(a, config) %in% c("bundled", "auto-download", "container")
}, logical(1)))
if (suite_needs_r) {
config$dependencies$r$version <- resolve_runtime_version(
"r", config, platform = targets$platform[1]
)
}
# Resolve every app's dependencies before the destination is touched, so a
# package that cannot be installed stops the export with any earlier build
# still in place.
suite_deps <- resolve_suite_dependencies(appdir, app_type, config,
verbose = verbose)
# Create destination
if (fs::dir_exists(destdir)) {
if (!overwrite) {
cli::cli_abort("Destination directory already exists: {.path {destdir}}")
}
assert_safe_to_overwrite(destdir)
unlink(destdir, recursive = TRUE)
}
fs::dir_create(destdir, recurse = TRUE)
created_destdir <- TRUE
result <- list()
result <- tryCatch({
# Step 1: Process each app
apps_dir <- fs::path(destdir, "apps")
fs::dir_create(apps_dir)
# Shinylive apps share ONE static site (one WebR/Pyodide asset tree) under
# destdir/shinylive-site, each app exported into its own <id> subdir.
shinylive_site_dir <- fs::path(destdir, "shinylive-site")
apps_manifest <- list()
# Accumulate the per-language UNION of DIRECT package sets. embed_*_runtime
# resolves the recursive tree internally, so the union of direct sets is the
# right input (R: each app's detected deps; Python: the shared suite list).
r_union_packages <- character(0)
r_union_repos <- NULL
py_union_packages <- character(0)
py_union_index_urls <- NULL
for (app_entry in config$apps) {
app_id <- app_entry$id
app_src <- resolve_config_path(app_entry$path, appdir)
app_dest <- fs::path(apps_dir, app_id)
this_type <- resolve_app_type(app_entry, config)
this_strategy <- resolve_app_strategy(app_entry, config)
if (verbose) cli::cli_alert_info("Processing app: {.val {app_entry$name}} ({this_type}, {this_strategy})")
# Convert or copy based on strategy
if (this_strategy == "shinylive") {
# Additive shared-site export: each app lands at shinylive-site/<id>,
# all sharing shinylive-site/shinylive/ (one runtime copy).
if (this_type == "r-shiny") {
convert_shiny_to_shinylive(appdir = app_src, output_dir = shinylive_site_dir,
subdir = app_id, verbose = verbose)
} else {
convert_py_to_shinylive(appdir = app_src, output_dir = shinylive_site_dir,
subdir = app_id, verbose = verbose)
}
} else {
copy_dir_contents(app_src, app_dest)
# Write the dependencies resolved before the destination was created.
dep_info <- suite_deps[[app_id]]
if (!is.null(dep_info) && length(dep_info$packages) > 0) {
manifest <- generate_dependency_manifest(
packages = dep_info$packages,
language = dep_info$language,
repos = dep_info$repos,
index_urls = dep_info$index_urls,
local_packages = local_names,
requirements = dep_info$requirements
)
writeLines(manifest, fs::path(app_dest, "dependencies.json"))
if (this_strategy == "bundled") {
if (grepl("^r-", this_type)) {
r_union_packages <- c(r_union_packages, unlist(dep_info$packages))
if (is.null(r_union_repos)) r_union_repos <- dep_info$repos
} else {
py_union_packages <- c(py_union_packages, unlist(dep_info$requirements %||% dep_info$packages))
if (is.null(py_union_index_urls)) py_union_index_urls <- dep_info$index_urls
}
}
}
}
# Build manifest entry. The launcher shows the copy of the icon that
# copy_brand_assets() puts in the build (use NA for no icon so jsonlite
# writes null, not {}).
app_icon <- app_icon_asset(app_entry) %||% NA
serve <- if (this_strategy == "shinylive") {
list(kind = "shinylive", site = "src/shinylive-site", subdir = app_id)
} else if (this_strategy == "container") {
list(kind = "container", path = paste0("src/apps/", app_id))
} else {
list(kind = "native", path = paste0("src/apps/", app_id),
runtime_strategy = this_strategy)
}
apps_manifest <- c(apps_manifest, list(list(
id = app_id,
name = app_entry$name,
description = app_entry$description %||% "",
type = this_type,
runtime_strategy = this_strategy,
icon = app_icon,
serve = serve
)))
}
result$apps <- apps_manifest
# Write the apps manifest into the staging root so it is emitted even when
# build = FALSE (tests and tooling can read the serve descriptors without a
# full Electron build). build_multi_app persists the same manifest into the
# Electron output directory.
manifest_data <- list(
schema_version = MANIFEST_SCHEMA_VERSION,
apps = apps_manifest,
default_type = app_type,
runtime_strategy = runtime_strategy
)
writeLines(
jsonlite::toJSON(manifest_data, pretty = TRUE, auto_unbox = TRUE),
fs::path(destdir, "apps-manifest.json")
)
# Step 2: Build Electron application
if (build) {
if (verbose) cli::cli_alert_info("Building Electron application...")
electron_dir <- fs::path(destdir, "electron-app")
# Write apps manifest for the Electron app
fs::dir_create(electron_dir, recurse = TRUE)
built_app_dir <- without_repeated_update_url_warning(build_multi_app(
apps_dir = apps_dir,
shinylive_site_dir = shinylive_site_dir,
output_dir = electron_dir,
app_name = app_name,
apps_manifest = apps_manifest,
default_type = app_type,
runtime_strategy = runtime_strategy,
sign = sign,
platform = targets$platform,
arch = targets$arch,
icon = icon,
config = config,
overwrite = TRUE,
verbose = verbose,
r_packages = sort(unique(r_union_packages)),
r_repos = r_union_repos,
py_packages = sort(unique(py_union_packages)),
py_index_urls = py_union_index_urls
))
result$electron_app <- built_app_dir
}
result
}, error = function(e) {
if (isTRUE(created_destdir) && fs::dir_exists(destdir)) {
unlink(destdir, recursive = TRUE)
}
cli::cli_abort(c(
"Failed to export multi-app suite",
"x" = "Error: {e$message}"
), parent = e)
})
# The build output is committed past this point. Post-build actions must
# never delete it, so they run OUTSIDE the cleanup tryCatch above.
if (verbose) {
cli::cli_alert_success("Successfully exported multi-app suite!")
cli::cli_alert_info("Output: {.path {destdir}}")
}
# Run in dev mode if requested. A non-zero Electron exit must not destroy the
# successful build, so failures warn rather than abort.
if (run_after && build) {
if (verbose) cli::cli_alert_info("Starting application in development mode...")
tryCatch(
run_electron_app(app_dir = result$electron_app, verbose = verbose),
error = function(e) cli::cli_warn(c(
"The application exited with an error (the build output was kept).",
"i" = conditionMessage(e)
))
)
}
if (open_after) {
if (verbose) cli::cli_alert_info("Opening output directory...")
tryCatch(
utils::browseURL(destdir),
error = function(e) cli::cli_warn(c(
"Could not open the output directory (the build output was kept).",
"i" = conditionMessage(e)
))
)
}
result
}
#' Build multi-app Electron application
#' @keywords internal
build_multi_app <- function(apps_dir, output_dir, app_name,
apps_manifest, default_type,
runtime_strategy, sign, platform, arch,
icon, config, overwrite, verbose,
r_packages = NULL, r_repos = NULL,
py_packages = NULL, py_index_urls = NULL,
shinylive_site_dir = NULL) {
if (is.null(platform)) platform <- detect_current_platform()
if (is.null(arch)) arch <- detect_current_arch()
validate_platform(platform)
validate_arch(arch)
# Resolve the R pruning flag before anything is created or downloaded.
prune <- resolve_r_prune(config)
# Check the update settings too, since a bad one would reach the publish block.
check_update_settings(config)
# Resolve each app's type and runtime strategy once; the single-platform
# guard, runtime embedding, and auto-download manifest writing all key off the
# per-app resolved strategy rather than the suite-level scalar.
app_types <- vapply(config$apps, function(a) resolve_app_type(a, config), character(1))
app_strategies <- vapply(config$apps, function(a) resolve_app_strategy(a, config), character(1))
# Bundled / auto-download native runtimes embed a single platform's runtime,
# so a suite containing ANY such native app cannot target multiple platforms
# or architectures in one build, regardless of the suite-level default.
check_single_target(app_strategies, platform, arch, apps = config$apps)
validate_node_npm()
fs::dir_create(output_dir, recurse = TRUE)
# Setup project structure
setup_electron_project(output_dir, app_name, default_type, verbose = verbose)
# Copy native/container apps to src/apps/. Shinylive apps are NOT staged in
# apps_dir (they live in shinylive_site_dir); we additionally skip any
# shinylive id defensively so the per-app WASM duplication never reappears.
src_apps_dir <- fs::path(output_dir, "src", "apps")
fs::dir_create(src_apps_dir, recurse = TRUE)
shinylive_ids <- vapply(apps_manifest, function(a) {
if (!is.null(a$serve) && identical(a$serve$kind, "shinylive")) a$id else NA_character_
}, character(1))
shinylive_ids <- shinylive_ids[!is.na(shinylive_ids)]
for (app_id_dir in list.dirs(apps_dir, recursive = FALSE, full.names = TRUE)) {
app_id <- basename(app_id_dir)
if (app_id %in% shinylive_ids) next
copy_dir_contents(app_id_dir, fs::path(src_apps_dir, app_id))
}
# Copy the shared shinylive site once, preserving the single shinylive/ asset
# tree shared by every sub-app.
if (!is.null(shinylive_site_dir) && fs::dir_exists(shinylive_site_dir)) {
copy_dir_contents(shinylive_site_dir, fs::path(output_dir, "src", "shinylive-site"))
}
# Embed native runtimes once per bundled language. Call unconditionally for a
# bundled language even when the union package set is empty: shipping no
# runtime/<lang> would break the suite-wide bundled detection in the backends.
r_bundled <- any(grepl("^r-", app_types) & app_strategies == "bundled")
py_bundled <- any(grepl("^py-", app_types) & app_strategies == "bundled")
if (r_bundled) {
if (verbose) cli::cli_alert_info("Embedding R runtime for bundled apps...")
embed_r_runtime(
output_dir = output_dir,
packages = sort(unique(r_packages)),
repos = r_repos %||% config$dependencies$r$repos %||%
SHINYELECTRON_DEFAULTS$dependencies$r$repos,
version = resolve_runtime_version("r", config),
platform = platform[1],
arch = arch[1],
verbose = verbose,
prune = prune,
# The packages that export_multi_app() read.
local_packages = config$dependencies$r$local_packages %||% character(0)
)
}
if (py_bundled) {
if (verbose) cli::cli_alert_info("Embedding Python runtime for bundled apps...")
embed_python_runtime(
output_dir = output_dir,
packages = sort(unique(py_packages)),
index_urls = py_index_urls %||% SHINYELECTRON_DEFAULTS$dependencies$python$index_urls,
version = resolve_runtime_version("python", config),
platform = platform[1],
arch = arch[1],
verbose = verbose
)
}
# Auto-download native apps read runtime-manifest.json from their OWN app dir
# (src/apps/<id>/runtime-manifest.json), so write one per such app.
for (i in seq_along(config$apps)) {
if (app_strategies[i] == "auto-download" && grepl("^(r|py)-", app_types[i])) {
app_id <- config$apps[[i]]$id
write_runtime_manifest(
fs::path(src_apps_dir, app_id),
app_types[i], platform, arch, config, verbose = verbose
)
}
}
# Write apps-manifest.json
manifest_data <- list(
schema_version = MANIFEST_SCHEMA_VERSION,
apps = apps_manifest,
default_type = default_type,
runtime_strategy = runtime_strategy
)
writeLines(
jsonlite::toJSON(manifest_data, pretty = TRUE, auto_unbox = TRUE),
fs::path(output_dir, "apps-manifest.json")
)
# Process templates (pass multi-app flag)
process_templates(output_dir, app_name, default_type,
runtime_strategy = runtime_strategy,
icon = icon, config = config, sign = sign,
is_multi_app = TRUE,
apps_manifest = apps_manifest,
platform = platform,
verbose = verbose)
# Install npm dependencies
install_npm_dependencies(output_dir, verbose = verbose)
# Build for platforms
build_for_platforms(output_dir, platform, arch, sign = sign,
config = config, verbose = verbose)
# Validate the assembled build output (mirrors the single-app pipeline).
validate_build_output(output_dir, platform)
return(fs::path_abs(output_dir))
}
#' Validate that each language uses a single native runtime strategy
#'
#' Native strategies (`system`, `bundled`, `auto-download`) share one backend
#' module and one suite-wide runtime detection per language, so a suite may
#' declare at most one distinct native strategy per language. `shinylive` and
#' `container` apps use their own backends and are exempt.
#'
#' @param apps List. `config$apps` entries.
#' @param config List. Full suite configuration.
#' @keywords internal
validate_suite_strategies <- function(apps, config) {
native_strategies <- c("system", "bundled", "auto-download")
suite_strategy <- config$build$runtime_strategy
for (lang in c("r", "py")) {
lang_pattern <- paste0("^", lang, "-")
ids <- character(0)
strategies <- character(0)
for (app in apps) {
if (!grepl(lang_pattern, resolve_app_type(app, config))) next
strat <- resolve_app_strategy(app, config)
if (!strat %in% native_strategies) next
ids <- c(ids, app$id)
strategies <- c(strategies, strat)
}
if (length(strategies) == 0) next
distinct <- unique(strategies)
if (length(distinct) > 1) {
conflict_idx <- which(strategies != strategies[1])[1]
cli::cli_abort(c(
"Conflicting native runtime strategies for {.field {lang}-shiny} apps.",
"x" = "App {.val {ids[1]}} uses {.val {strategies[1]}} but app {.val {ids[conflict_idx]}} uses {.val {strategies[conflict_idx]}}.",
"i" = "All native apps of one language must share a single runtime strategy.",
"i" = "Use {.val shinylive} or {.val container} for apps that need a different delivery."
), class = "shinyelectron_suite_strategy_conflict")
}
if (!is.null(suite_strategy) && suite_strategy %in% native_strategies &&
!identical(suite_strategy, distinct)) {
cli::cli_abort(c(
"Suite-level {.field build.runtime_strategy} conflicts with a per-app strategy.",
"x" = "Suite default is {.val {suite_strategy}} but app {.val {ids[1]}} resolves to {.val {strategies[1]}}.",
"i" = "Set {.field build.runtime_strategy} to {.val {strategies[1]}} or remove the per-app override."
), class = "shinyelectron_suite_strategy_conflict")
}
}
invisible(TRUE)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.