Package 'livelink'

Title: Create Shareable Links for 'webR' and 'Shinylive' Environments
Description: Creates shareable links for 'R' code in 'WebAssembly' (WASM) Read-Eval-Print Loop (REPL) environments like 'webR' <https://webr.r-wasm.org/> and for 'Shiny' applications using 'Shinylive' <https://shinylive.io/>. Supports single scripts, multi-file projects, exercise and solution pairs, and batch processing. Includes encoding, decoding, and previewing of links for both 'R' and 'Python' environments.
Authors: James Joseph Balamuta [aut, cre, cph], reprex authors [cph] (stringify_expression() and trim_common_leading_ws(), adapted in R/process-input.R; see inst/COPYRIGHTS)
Maintainer: James Joseph Balamuta <[email protected]>
License: AGPL (>= 3)
Version: 0.1.2
Built: 2026-09-30 07:18:35 UTC
Source: https://github.com/coatless-rpkg/livelink

Help Index


Print method for shinylive_decoded objects

Description

Displays the files recovered from a Shinylive link and where they were written.

Usage

## S3 method for class 'shinylive_decoded'
print(x, ...)

Arguments

x

shinylive_decoded object

...

Additional arguments (ignored)

Value

The shinylive_decoded object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the link, the output folder, each file with its size, the total size, and the mode. See decode_shinylive_link() for the entries the object holds.


Print method for shinylive_decoded_batch objects

Description

Displays a summary of the Shinylive links decoded in a batch.

Usage

## S3 method for class 'shinylive_decoded_batch'
print(x, ...)

Arguments

x

shinylive_decoded_batch object

...

Additional arguments (ignored)

Value

The shinylive_decoded_batch object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the base folder, the output folder and file count for each link that decoded, and the totals across all of them. See decode_shinylive_link() for the entries the object holds.


Print method for shinylive_directory objects

Description

Displays every app sharelink generated from a source directory.

Usage

## S3 method for class 'shinylive_directory'
print(x, ...)

Arguments

x

shinylive_directory object

...

Additional arguments (ignored)

Value

The shinylive_directory object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the source folder, one sharelink per app, the engine, and the mode. See shinylive_directory() for the entries the object holds.


Print method for shinylive_preview objects

Description

Displays what a Shinylive link contains without writing anything to disk.

Usage

## S3 method for class 'shinylive_preview'
print(x, show_content = FALSE, max_content_length = 500, ...)

Arguments

x

shinylive_preview object

show_content

Logical. Whether to print the contents of each file. Defaults to FALSE.

max_content_length

Maximum number of characters of content to show per file. Defaults to 500.

...

Additional arguments (ignored)

Value

The shinylive_preview object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the link, the file count and total size, the engine, the mode, and each file with its kind and size. See preview_shinylive_link() for the entries the object holds.


Print method for shinylive_project objects

Description

Displays the project sharelink and the files it carries.

Usage

## S3 method for class 'shinylive_project'
print(x, ...)

Arguments

x

shinylive_project object

...

Additional arguments (ignored)

Value

The shinylive_project object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the URL, the files the project carries, the engine, and the mode. See shinylive_project() for the entries the object holds.


Print method for webr_decoded objects

Description

Displays the files recovered from a webR link and where they were written.

Usage

## S3 method for class 'webr_decoded'
print(x, ...)

Arguments

x

webr_decoded object

...

Additional arguments (ignored)

Value

The webr_decoded object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the link, the output folder, each file with its size, and any file that was skipped along with the reason. See decode_webr_link() for the entries the object holds.


Print method for webr_decoded_batch objects

Description

Displays a summary of the webR links decoded in a batch.

Usage

## S3 method for class 'webr_decoded_batch'
print(x, ...)

Arguments

x

webr_decoded_batch object

...

Additional arguments (ignored)

Value

The webr_decoded_batch object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the base folder, each link that decoded with its file count and output folder, and a count of the links that failed. See decode_webr_link() for the entries the object holds.


Print method for webr_directory objects

Description

Displays every sharelink generated from a source directory.

Usage

## S3 method for class 'webr_directory'
print(x, ...)

Arguments

x

webr_directory object

...

Additional arguments (ignored)

Value

The webr_directory object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the source folder, one sharelink per file, the interface, and the version. See webr_repl_directory() for the entries the object holds.


Print method for webr_exercise objects

Description

Displays the exercise sharelink and the matching solution sharelink.

Usage

## S3 method for class 'webr_exercise'
print(x, ...)

Arguments

x

webr_exercise object

...

Additional arguments (ignored)

Value

The webr_exercise object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers both the exercise sharelink and the solution sharelink. See webr_repl_exercise() for the entries the object holds.


Print method for webr_preview objects

Description

Displays what a webR link contains without writing anything to disk.

Usage

## S3 method for class 'webr_preview'
print(x, show_content = FALSE, max_content_length = 500, ...)

Arguments

x

webr_preview object

show_content

Logical. Whether to print the contents of each file. Defaults to FALSE.

max_content_length

Maximum number of characters of content to show per file. Defaults to 500.

...

Additional arguments (ignored)

Value

The webr_preview object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the link, the file count and total size, the interface, the version, the encoding flags, and each file with its size. See preview_webr_link() for the entries the object holds.


Print method for webr_project objects

Description

Displays the project sharelink and the files it carries.

Usage

## S3 method for class 'webr_project'
print(x, ...)

Arguments

x

webr_project object

...

Additional arguments (ignored)

Value

The webr_project object it was handed, returned invisibly, so it can be passed straight on. Called for the summary it prints, which covers the URL, every file and where it lands, which files run on open, the interface, and the version. See webr_repl_project() for the entries the object holds.


Extract shareable URLs from livelink objects

Description

Extracts the shareable URL(s) from any livelink object, covering both webR REPL and Shinylive results. Provides a clear way to get just the URLs for sharing or further work.

Usage

repl_urls(x, ...)

## Default S3 method:
repl_urls(x, ...)

Arguments

x

A livelink object. Supported classes are webr_link, webr_project, webr_exercise, webr_directory, webr_decoded, webr_decoded_batch, webr_preview, shinylive_link, shinylive_project, shinylive_directory, shinylive_decoded, shinylive_decoded_batch, and shinylive_preview.

...

Additional arguments passed to methods

Value

A character vector of URLs.

  • Most objects give a single URL.

  • An exercise gives two, named exercise and solution.

  • A directory or a batch gives one URL for each file.

See Also

The as.character() methods (for example as.character.webr_link()), which repl_urls() calls to do the work.

Examples

# Single link
link <- webr_repl_link("plot(1:10)")
repl_urls(link)

# Exercise (returns named vector)
exercise <- webr_repl_exercise("# TODO", "plot(1:10)", "test")
repl_urls(exercise)

# Shinylive links work the same way
repl_urls(shinylive_r_link("library(shiny)"))

# Decoded files (returns the original URL)
decoded <- decode_webr_link(as.character(link))
repl_urls(decoded)

Set global base URL for webR links

Description

Overrides the base URL used when building webR REPL links, for instance to point at a webR site you host yourself or at a fixed webR version.

Usage

set_webr_base_url(base_url = NULL)

Arguments

base_url

Custom base URL to use for all webR links

Details

The value is stored in the livelink.base_url option and applies to webR links only. Shinylive links are unaffected. Once set, a custom base URL takes precedence over the version argument given to the functions that build links.

Value

Invisibly returns the value supplied to base_url.

  • The custom URL when you set one.

  • NULL when you reset to the default.

See Also

webr_repl_link() for single-script links that honor this option.

webr_repl_project() for multi-file projects that honor this option.

Examples

# Remember the current setting so it can be restored afterwards
old <- getOption("livelink.base_url")

# Set custom base URL
set_webr_base_url("https://my-custom-webr.com/")

# Reset to default (removes custom setting)
set_webr_base_url(NULL)

# Restore the previous setting
set_webr_base_url(old)

Create Shinylive sharelinks from a directory of Shiny apps

Description

Batch processes directories containing Shiny applications to create individual Shinylive links. Each subdirectory is treated as a separate Shiny app project.

Usage

shinylive_directory(
  directory_path,
  engine,
  mode = "editor",
  header = TRUE,
  app_file = NULL,
  base_url = NULL
)

Arguments

directory_path

Character string specifying the path to the directory containing Shiny app directories

engine

Engine to use, either "r" for R Shiny or "python" for Python Shiny

mode

Shinylive display mode (default "editor"). "editor" shows an editable code panel beside the running app. "app" shows only the running app.

header

Logical, whether to show the Shinylive header bar. It applies only when mode = "app" and is ignored in the default "editor" mode. Defaults to TRUE.

app_file

Main app filename to look for (defaults to "app.R" for R and "app.py" for Python)

base_url

Custom Shinylive base URL. If NULL (default), links point at https://shinylive.io.

Details

Only text files with extensions .R, .py, .txt, .md, .csv, .json, .yaml, or .yml are embedded in a link. Other files (for example images or binary data) are skipped with a warning.

Value

A shinylive_directory object, which is a list with these entries.

  • urls, one sharelink per app, as a named character vector whose names are the app subdirectory names. It is empty when no app was found.

  • engine, the Shiny flavor the links run, "r" or "python".

  • mode, the Shinylive display mode, "editor" or "app".

  • source_directory, the directory that was read.

Use repl_urls() on the object to get the URLs on their own.

See Also

shinylive_project() for a single multi-file app.

shinylive_r_link() and shinylive_py_link() for single apps.

Examples

# Each app lives in its own subdirectory:
#   shiny_apps/
#     app1/app.R
#     app2/app.R
shiny_apps <- tempfile()
dir.create(file.path(shiny_apps, "app1"), recursive = TRUE)
dir.create(file.path(shiny_apps, "app2"), recursive = TRUE)
writeLines("library(shiny)", file.path(shiny_apps, "app1", "app.R"))
writeLines("library(shiny)", file.path(shiny_apps, "app2", "app.R"))

links <- shinylive_directory(shiny_apps, engine = "r", mode = "editor")
print(links)

# Extract just the URLs
repl_urls(links)

Create a Shinylive sharelink for multi-file projects

Description

Creates Shinylive projects for either R or Python from named lists or file path vectors.

Usage

shinylive_project(
  input,
  engine,
  mode = "editor",
  header = TRUE,
  base_url = NULL
)

Arguments

input

Input for multiple files. Can be:

  • Named list: list("app.R" = code1, "utils.R" = code2)

  • Vector of file paths: c("app.R", "utils.R", "data.csv")

engine

Engine to use, either "r" for R Shiny or "python" for Python Shiny

mode

Shinylive display mode (default "editor"). "editor" shows an editable code panel beside the running app. "app" shows only the running app.

header

Logical, whether to show the Shinylive header bar. It applies only when mode = "app" and is ignored in the default "editor" mode. Defaults to TRUE.

base_url

Custom Shinylive base URL. If NULL (default), links point at https://shinylive.io.

Value

A shinylive_project object, which is a list with these entries.

  • url, the one sharelink that carries every file, as a single string.

  • files, the names of the files carried in the link, as a character vector.

  • engine, the Shiny flavor the link runs, "r" or "python".

  • mode, the Shinylive display mode, "editor" or "app".

Use as.character() on the object to get the URL on its own.

See Also

shinylive_r_link() and shinylive_py_link() for single apps.

decode_shinylive_link() and preview_shinylive_link() to read a link back.

Examples

# Named list input
files <- list(
  "app.R" = "library(shiny)\nshinyApp(fluidPage(), function(i, o) {})",
  "utils.R" = "# Utility functions"
)
shinylive_project(files, engine = "r", mode = "editor")

# File paths input
project_dir <- tempfile()
dir.create(project_dir)
app <- file.path(project_dir, "app.R")
utils <- file.path(project_dir, "utils.R")
writeLines("library(shiny)", app)
writeLines("# utils", utils)
shinylive_project(c(app, utils), engine = "r")

Create webR REPL sharelinks from a directory of R files

Description

Batch processes all R files in a directory into webR sharelinks.

Usage

webr_repl_directory(
  directory_path,
  autorun = FALSE,
  single_link = FALSE,
  pattern = "\\.R$",
  base_path = "/home/web_user/",
  panels = NULL,
  version = "latest",
  base_url = NULL
)

Arguments

directory_path

Character string specifying the path to the directory containing R files

autorun

Logical. Whether to enable autorun for all generated links. Defaults to FALSE. With single_link = TRUE, this runs every R file in the bundle on arrival.

single_link

Logical. If FALSE (default), each matched file becomes its own link and the result is a webr_directory. If TRUE, all matched files are packed into one link and the result is a single webr_project.

pattern

Regular expression matched against file names in directory_path. Defaults to "\\.R$", i.e. files ending in .R.

base_path

Base directory path for files in webR. Defaults to "/home/web_user/".

panels

Character vector or string specifying which webR interface panels to show. The valid panels are "plot", "files", "terminal", and "editor". Can be c("plot", "files") or "plot-files". If NULL (default), shows all panels.

version

webR version to use ("latest" or specific version >= "v0.5.4")

base_url

webR application URL. If NULL, uses global option or builds from version

Details

By default each file becomes its own webR sharelink. With single_link = TRUE the whole directory is bundled into one link instead, exactly as webr_repl_project() would. Useful for converting collections of scripts, examples, or course materials.

Value

By default, a webr_directory object, which is a list with these entries.

  • urls, the sharelinks, as a named character vector with one entry per matched file, named by file name.

  • base_path, where the files are placed inside webR.

  • mode, the panels the links ask for, or NULL for all of them.

  • version, the webR version the links point at.

  • source_directory, the directory the files were read from.

With single_link = TRUE, a webr_project object instead, which is a list with these entries.

  • url, the one sharelink that carries every matched file, as a single string.

  • files, the file contents that went into the link, as a named list keyed by file name.

  • base_path, where the files are placed inside webR.

  • mode, the panels the link asks for, or NULL for all of them.

  • version, the webR version the link points at.

  • autorun_files, the files that run as soon as the link opens.

Use as.character() on either object to get the URLs on their own.

See Also

webr_repl_project(), which bundles a named list or a vector of file paths into one link.

Examples

# A directory of R scripts
examples <- tempfile()
dir.create(examples)
writeLines("plot(1:10)", file.path(examples, "plot.R"))
writeLines("hist(rnorm(100))", file.path(examples, "hist.R"))

links <- webr_repl_directory(examples, autorun = TRUE)
print(links)

# Bundle the whole directory into one link instead
webr_repl_directory(examples, single_link = TRUE, panels = c("editor", "plot"))

# Show only the editor and terminal panels
webr_repl_directory(examples, panels = c("editor", "terminal"))

# Match a subset of files
webr_repl_directory(examples, pattern = "^plot")

# The URLs, named by file
repl_urls(links)

Create paired exercise and solution webR REPL links

Description

Generates a pair of webR links for teaching. One link holds the exercise for the student and the other holds the solution.

Usage

webr_repl_exercise(
  exercise_text,
  solution_text,
  exercise_name,
  base_path = "/home/web_user/",
  version = "latest",
  base_url = NULL
)

Arguments

exercise_text

Character string containing the exercise code with placeholders or TODOs

solution_text

Character string containing the complete solution code

exercise_name

Base name for the exercise (will create "name_exercise.R" and "name_solution.R")

base_path

Base directory path for files (default: "/home/web_user/")

version

webR version to use ("latest" or specific version >= "v0.5.4")

base_url

webR application URL. If NULL, uses global option or builds from version

Details

The exercise link is built without autorun, so the student works through it, while the solution link is built with autorun enabled.

Value

A webr_exercise object, which is a list with these entries.

  • exercise, the student's link, a webr_link object that does not autorun.

  • solution, the answer link, a webr_link object that autoruns on opening.

Each of the two is itself a list holding the entries described in webr_repl_link(), so links$solution$url is the solution URL on its own.

See Also

webr_repl_link(), which this builds on.

vignette("teaching", package = "livelink") for using links in a course.

Examples

exercise_code <- "
# Exercise: Calculate mean of mtcars$mpg
# TODO: Complete the line below
mean_mpg <- 0
print(mean_mpg)
"

solution_code <- "
# Solution: Calculate mean of mtcars$mpg
mean_mpg <- mean(mtcars$mpg)
print(mean_mpg)
"

links <- webr_repl_exercise(exercise_code, solution_code, "basic_stats")
links$exercise
links$solution

# Custom path and version
webr_repl_exercise(exercise_code, solution_code, "stats",
                   base_path = "/exercises/", version = "v0.5.4")

Create webR REPL sharelink for multiple files

Description

Creates a webR sharelink for projects with multiple R files, data files, or other resources.

Usage

webr_repl_project(
  input,
  autorun_files = character(0),
  base_path = "/home/web_user/",
  panels = NULL,
  version = "latest",
  base_url = NULL
)

Arguments

input

Input for multiple files. Can be:

  • Named list of braced expressions, so each file is written as R rather than as a string full of escaped newlines: list("main.R" = { plot(1:10) }, "utils.R" = { f <- function() 42 })

  • Named list of strings: list("main.R" = code1, "utils.R" = code2)

  • Vector of file paths: c("main.R", "utils.R", "data.csv")

The two list forms mix freely, which is what you want for a project holding both code and, say, a README.md.

autorun_files

Character vector of filenames to auto-execute when project loads, or "all" to autorun all R files (default: none)

base_path

Base directory path for all files (default: "/home/web_user/")

panels

Character vector or string specifying which webR interface panels to show. The panels are "plot", "files", "terminal", and "editor". Can be c("plot", "files") or "plot-files". If NULL (default), shows all panels.

version

webR version to use ("latest" or specific version >= "v0.5.4")

base_url

webR application URL. If NULL, uses global option or builds from version

Details

Every file is placed under base_path inside webR, so a source("utils.R") in one file finds its sibling. Names are used verbatim and may carry a subdirectory, as in "R/helpers.R".

Value

A webr_project object, which is a list with these entries.

  • url, the sharelink itself, as a single string.

  • files, the names of the files carried in the link.

  • base_path, the folder the files are placed in inside webR.

  • mode, the panels the link asks for, or NULL for all of them.

  • version, the webR version the link points at.

  • autorun_files, the names you asked to run on opening.

Writing a project as code

A file's contents can be given as a { ... } block instead of a string, which spares you escaping every newline and quote:

webr_repl_project(list(
  "main.R"    = { source("utils.R"); summarise(mtcars) },
  "utils.R"   = { summarise <- function(d) summary(d) },
  "README.md" = "# Analysis"
))

The blocks are never evaluated. They are source to ship, not code to run, so an assignment inside one leaves nothing behind in your session.

Two things to know. Comments inside { } survive in an interactive session but not in a knitted document (see webr_repl_link()). And a library() call inside a block is visible to ⁠R CMD check⁠, which will report the package as an undeclared dependency of yours. In a vignette or an example, use a string for code that loads packages.

See Also

webr_repl_link() for the single-file case.

Examples

# Each file written as R, rather than as an escaped string
webr_repl_project(list(
  "main.R"  = { source("utils.R"); summarise(mtcars) },
  "utils.R" = { summarise <- function(d) summary(d) }
))

# Strings still work, and the two forms mix
files <- list(
  "main.R" = "source('utils.R')\nresult <- analyze_data(mtcars)",
  "utils.R" = "analyze_data <- function(data) { summary(data) }",
  "README.md" = "# My Analysis\nThis project analyzes the mtcars dataset."
)
webr_repl_project(files, autorun_files = "main.R")

# Autorun every R file in the project
webr_repl_project(files, autorun_files = "all")

# File paths input
project_dir <- tempfile()
dir.create(project_dir)
main <- file.path(project_dir, "main.R")
utils <- file.path(project_dir, "utils.R")
writeLines("source('utils.R')", main)
writeLines("# utils", utils)
webr_repl_project(c(main, utils))