Package 'charpente'

Title: Seamlessly design robust 'shiny' extensions
Description: 'charpente' eases the creation of 'shiny' extensions like 'shinydashboard', 'bs4Dash', 'shinyMobile'. It provides helpers to quickly set up a relevant package structure, import all external web dependencies (JavaScript, CSS) as well as initialize input/output bindings and custom handlers boilerplates. 'charpente' offers tools to convert HTML code into R to dramatically speed up the development of the template components as well as an high level interface to 'htmltools'. 'charpente' is a chatty package relying on the same principle as 'usethis' and more recently 'golem', that is make ('shiny') developer's life much easier.
Authors: David Granjon [aut, cre], John Coene [aut], Alan Dipert [ctb] (Functions from html2r), Veerle van Leemput [ctb], RinteRface [cph], ThinkR [cph] (Some utils from golem)
Maintainer: David Granjon <[email protected]>
License: MIT + file LICENSE
Version: 0.7.1
Built: 2026-05-28 08:57:51 UTC
Source: https://github.com/RinteRface/charpente

Help Index


Compress and optimize all files in the current folder

Description

Generates a minified file under inst/pkg_name-pkg_version, if mode is prod. If mode is dev, aggregates all js files without mangling or compression.

Usage

build_js(dir = "srcjs", mode = c("prod", "dev"), entry_points = "main.js")

Arguments

dir

Default to srcjs.

mode

Production or development mode. Choose either "prod" or "dev". "prod" bundles, aggregates and minifyies files. "dev" only bundles the code. Modules follow the ES6 format (import/export).

entry_points

Entry point(s) to use in esbuild configuration. In case of a monolithic bundle, only one entrypoint is needed. This the default. In case of component based bundles, a vector of entrypoints is needed. The output files will match the entrypoints names.


Configure charpente

Description

Configure charpente

Usage

charpente_options(local = TRUE)

Arguments

local

Whether to download files locally or to point to a CDN. Default to TRUE.

Value

A list of options.


Create a package using charpente and usethis conventions

Description

A charpente powered package does not differ from other package structure. create_charpente calls a series of function to help you quickly setting up the package structure.

Usage

create_charpente(path, remote = NULL, private = FALSE, license)

Arguments

path

A path. If it exists, it is used. If it does not exist, it is created, provided that the parent path exists.

remote

Name of the remote Github organization to connect to.

private

Whether the repository is private. Default to FALSE.

license

Which license is your project under?

Examples

## Not run: 
 create_charpente("bs4Dash")

## End(Not run)

Imports Internal Dependencies

Description

Wrap internal scripts and stylesheets in one htmlDependency.

Usage

create_custom_dependency(
  name,
  version,
  entry_points,
  open = interactive(),
  mode
)

Arguments

name

Package name.

version

Package version.

entry_points

Entry points to create dependency for.

open

Whether to allow rstudioapi to open the newly created script. Default to TRUE.

mode

Internal. Don't use.


Imports External Dependencies

Description

Download and create dependency function.

Usage

create_dependency(
  name,
  tag = NULL,
  open = interactive(),
  options = charpente_options()
)

Arguments

name

Name of library.

tag

Library version. Default to NULL. If NULL, takes the latest version.

open

Whether to allow rstudioapi to open the newly created script. Default to TRUE.

options

See charpente_options.

Examples

## Not run: 
 create_dependency("tabler")
 # Use CDNs
 create_dependency(
  "framework7",
  options = charpente_options(local = FALSE)
 )

## End(Not run)

Create a shiny custom input binding boilerplate

Description

Creates a script in inst and the R part in ./R

Usage

create_input_binding(
  name,
  pkg = ".",
  dir = "srcjs",
  open = TRUE,
  initialize = FALSE,
  dev = FALSE,
  events = list(name = "click", rate_policy = FALSE),
  add_reference = TRUE
)

create_output_binding(
  name,
  pkg = ".",
  dir = "srcjs",
  open = TRUE,
  add_reference = TRUE
)

create_custom_handler(
  name,
  pkg = ".",
  dir = "srcjs",
  open = TRUE,
  add_reference = TRUE
)

create_js(
  name,
  dir = "srcjs",
  open = TRUE,
  with_doc_ready = FALSE,
  template = golem::js_template,
  ...,
  add_reference = TRUE
)

create_scss(
  name,
  dir = "styles",
  open = TRUE,
  template = golem::sass_template,
  ...,
  add_reference = TRUE
)

Arguments

name

The name of the module.

pkg

Path to the root of the package. Default is get_golem_wd().

dir

Path to the dir where the file while be created.

open

Should the created file be opened?

initialize

For JS file - Whether to add the initialize method. Default to FALSE. Some JavaScript API require to initialize components before using them.

dev

Whether to insert console.log calls in the most important methods of the binding. This is only to help building the input binding. Default is FALSE.

events

List of events to generate event listeners in the subscribe method. For instance, list(name = c("click", "keyup"), rate_policy = c(FALSE, TRUE)). The list contain names and rate policies to apply to each event. If a rate policy is found, the debounce method with a default delay of 250 ms is applied. You may edit manually according to https://shiny.rstudio.com/articles/building-inputs.html

add_reference

Whether to add an import statement in main.js. Defaults to TRUE.

with_doc_ready

For JS file - Should the default file include ⁠$( document ).ready()⁠?

template

Function writing in the created file. You may overwrite this with your own template function.

...

Arguments to be passed to the template function.


Create a manifest for your shiny app

Description

This is a central piece if you want to have your app standalone for instance

Usage

create_manifest(
  path,
  name = "My Progressive Web App",
  shortName = "My App",
  description = "What it does!",
  lang = "en-US",
  startUrl = "/",
  display = c("standalone", "minimal-ui", "fullscreen", "browser"),
  background_color = "#ffffff",
  theme_color = "#ffffff"
)

Arguments

path

App path.

name

App name.

shortName

App short name.

description

App description

lang

App language (en-US by default).

startUrl

Page to open at start.

display

Display mode. Choose among c("minimal-ui", "standalone", "fullscreen", "browser"). In practice, you want the standalone mode so that the app looks like a native app.

background_color

The background_color property is used on the splash screen when the application is first launched.

theme_color

The theme_color sets the color of the tool bar, and may be reflected in the app's preview in task switchers.

Value

This function creates a www folder for your shiny app. Must specify the path. It creates 1 folders to contain icons and the manifest.json file.

Note

See https://developer.mozilla.org/en-US/docs/Web/Manifest for more informations.

Examples

create_manifest(
  path = tempdir(),
  name = "My App",
  shortName = "My App",
  description = "What it does!",
  lang = "en-US",
  startUrl = "https://www.google.com/",
  display = "standalone",
  background_color = "#3367D6",
  theme_color = "#3367D6"
)

Create a PWA dependency

Description

List all relevant pwa resources in a custom dependency. Includes the web manifest + icons using the head parameter of htmltools::htmlDependency.

Usage

create_pwa_dependency(open = interactive())

Arguments

open

Whether to allow rstudioapi to open the newly created script. Default to TRUE.


Get all links to dependencies

Description

Query from https://data.jsdelivr.com/v1/package/npm/ under the hood.

Usage

get_dependency_assets(dep, tag = "latest")

Arguments

dep

Library name.

tag

Library version. Default to latest.

Value

A list of url containing links to CSS and JS dependencies for the given library.

Examples

## Not run: 
 get_dependency_assets("bootstrap")
 get_dependency_assets("framework7", tag = "5.5.5")

## End(Not run)

Get all version for the current dependency

Description

Query from https://data.jsdelivr.com/v1/package/npm/ under the hood.

Usage

get_dependency_versions(dep, latest = FALSE)

Arguments

dep

Library name.

latest

Whether to get the last version. Default to FALSE.

Value

A vector containing all versions

Examples

## Not run: 
 get_dependency_versions("framework7")
 get_dependency_versions("bootstrap")
 get_dependency_versions("react", latest = TRUE)

## End(Not run)

Get the version of the current installed dependency

Description

Used by update_dependency.

Usage

get_installed_dependency(name)

Arguments

name

Library name

Value

A character containing the version number


Retrieves Package Name

Description

Retrieves Package Name

Usage

get_pkg_name()

Value

Name of package.


Convert HTML content to R Shiny tags

Description

Convert HTML content to R Shiny tags

Usage

html_2_R(html, path = "/html/body/*", prefix = TRUE)

Arguments

html

HTML string

path

Path where to extract elements. Default to body content.

prefix

Whether to prefix elements by tag$...

Value

A list of R Shiny tags

Author(s)

Alan Dipert, RStudio

Examples

if (interactive()) {
 library(charpente)
 bs4_card <- '<div class="card" style="width: 18rem;">
   <img class="card-img-top" src="..." alt="Card image cap">
     <div class="card-body">
     <h5 class="card-title">Card title</h5>
     <p class="card-text">Some quick example text.</p>
       <a href="#" class="btn btn-primary">Go somewhere</a>
     </div>
   </div>'
 html_2_R(bs4_card)

 ## With non standard attributes
 tag <- "<div data-toggle='accordion'></div>"
 html_2_R(tag)
}

Setup esbuild

Description

Installs esbuild for the local project

Usage

set_esbuild(light = FALSE)

Arguments

light

Used to only install Sass plugins. This is to workaround a breaking change in charpente where styles does not exist in old versions.

Value

Installs esbuild in node_modules (dev scope), if not existing, creates srcjs + srcjs/main.js andstyles + styles/main.scss, and sets relevant files and folders in .gitignore. and .Rbuildignore.


Setup mocha

Description

Installs mocha for the local project

Usage

set_mocha()

Value

Installs mocha in node_modules (dev scope), creates srcjs/test folder, write basic test im test_basic.js


Utils to set up a PWA compatible structure

Description

Creates a web manifest, service-worker.js, icons and set the necessary dependencies. The app must be part of a package. Must not be used from the package root but from the app root.

Usage

set_pwa(
  path,
  name = "My Progressive Web App",
  shortName = "My App",
  description = "What it does!",
  lang = "en-US",
  startUrl = "/",
  display = c("standalone", "minimal-ui", "fullscreen", "browser"),
  background_color = "#ffffff",
  theme_color = "#ffffff",
  register_service_worker = TRUE,
  create_dependencies = TRUE
)

Arguments

path

App path.

name

App name.

shortName

App short name.

description

App description

lang

App language (en-US by default).

startUrl

Page to open at start.

display

Display mode. Choose among c("minimal-ui", "standalone", "fullscreen", "browser"). In practice, you want the standalone mode so that the app looks like a native app.

background_color

The background_color property is used on the splash screen when the application is first launched.

theme_color

The theme_color sets the color of the tool bar, and may be reflected in the app's preview in task switchers.

register_service_worker

Whether to register the service worker. Default to TRUE. Don't change the file name of service-worker.js!!!

create_dependencies

Default to TRUE. Relevant if used in a shinyMobile context. If used outside, you must set it to FALSE and handle the dependencies yourself.


Test JS code

Description

Test the entire srcjs/test folder

Usage

test_js()

Value

A message showing the test results


Update the given dependency to a specific version or latest

Description

Update the given dependency to a specific version or latest

Usage

update_dependency(name, version_target = "latest")

Arguments

name

Library name.

version_target

Targeted version. Default to latest.

Examples

## Not run: 
 update_dependency("framework7")

## End(Not run)