Skip to contents

Writes the R code that builds a stage_map() widget again: the tables as data.frame() calls, then the pipeline of stage_map() and the add_*() functions. Run the code and you get a map that sends the same data to the engine, and that sm_trace(), sm_orphans() and sm_missing() answer in the same way.

Usage

stage_map_code(map)

Arguments

map

A stage_map() widget, with any layers added.

Value

A character vector of class stagemap_code, one line of code per element; it prints as the code. A map that is not a stage_map() widget, or a map whose hosted option no argument of stage_map() gives, is an error of class stagemap_error_widget.

Details

The code starts with library(stagemap). A table that would not let the call that takes it fit on one line is written first, as a variable named after its argument; the others are written in place. Lines keep to 80 characters, except where a single string or number is longer, as it is never cut. Only arguments that differ from their defaults are written, and stages and lanes are left out when they are what stage_map() takes from the cards. A column that holds only NA is not written: the map reads it as left out, so it is the one column stage_map_tables() of the rebuilt map lacks. Text, numbers, TRUE and FALSE, factors and dates are written as you would type them; a column of another class is written as its values inside structure(), with its attributes.

The text is UTF-8, with every letter as it is. To save it so that it reads back the same in any locale, write the bytes as they are and parse the file as UTF-8, as the example does: writeLines(code, path, useBytes = TRUE), then eval(parse(path, encoding = "UTF-8")). (source() with encoding = "UTF-8" cannot read letters beyond ASCII in a session whose locale is not UTF-8.) Text in the map that is not marked as UTF-8, such as a string typed in such a session, is written as the session holds it. A matrix column comes back as the separate columns data.frame() makes of it, and a raw column comes back without its names.

Examples

map <- stage_map(
  data.frame(id = c("a", "b"), label = c("Draft", "Review"), stage = c("w", "c"), lane = "docs"),
  title = "A small piece of work"
) |>
  add_links(data.frame(from = "a", to = "b"))
code <- stage_map_code(map)
code
#> library(stagemap)
#> 
#> cards <- data.frame(
#>   id = c("a", "b"),
#>   label = c("Draft", "Review"),
#>   stage = c("w", "c"),
#>   lane = "docs"
#> )
#> 
#> stage_map(cards, title = "A small piece of work") |>
#>   add_links(data.frame(from = "a", to = "b"))
# Save it as a script, and run it to get the map back.
path <- tempfile(fileext = ".R")
writeLines(code, path, useBytes = TRUE)
again <- eval(parse(path, encoding = "UTF-8"))
class(again)
#> [1] "stage_map"       "stagemap_widget" "htmlwidget"     
unlink(path)