Skip to contents

export_module_code() writes R code that rebuilds a program: the complete program artifact (see program_artifact()) followed by a call to restore_module_config(). Nested programs and exact output schemas are preserved.

Usage

export_module_code(
  module,
  name = "mod",
  include_demos = TRUE,
  file = NULL,
  registry = list(),
  trusted = FALSE
)

Arguments

module

A module or composed program.

name

Variable name for the program in the generated code (default "mod").

include_demos

Whether to include the demonstrations (default TRUE).

file

Optional path. When given, the code is written there; an existing file is replaced only after the new code parses.

registry, trusted

As in program_artifact(). Standalone code cannot embed registry or trusted runtime references, so programs that need them are rejected; use save_program() for those.

Value

The code as a single string, invisibly when file is given.

Examples

mod <- module(signature("text -> sentiment"))
path <- tempfile(fileext = ".R")
export_module_code(mod, name = "sentiment_mod", file = path)
#> Module code written to /tmp/Rtmp4xxnp9/file1b1b738acbad.R

# Running the file rebuilds the program
source(path)
#> ✔ Restored program artifact
#> ℹ Root module: <PredictModule>
#> ℹ Artifact version: 6
sentiment_mod
#> 
#> ── PredictModule ──
#> 
#> ── Signature 
#> 
#> ── Signature ──
#> 
#> ── Inputs 
#> • text: "string" - Input: text
#> 
#> ── Output 
#> Type: "object(sentiment: string)"
#> 
#> ── Instructions 
#> Given the fields `text`, produce the fields `sentiment`.