## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(nestedscales)

## ----fuels--------------------------------------------------------------------
lt <- data.frame(
  group   = c("Fossil", "Fossil", "Fossil", "Fossil", "Fossil",
              "Bio", "Bio", "Bio", "Power"),
  phase   = c("solid", "solid", "liquid", "liquid", "gas",
              "solid", "liquid", "gas", NA),
  carrier = c("Coal", "Coal", "Oil", "Oil", "Gas",
              "Biomass", "Biomass", "Biomass", "Electricity"),
  fuel    = c("Anthracite", "Lignite", "Diesel", "Gasoline", "NaturalGas",
              "Wood", "Biodiesel", "Biogas", "Electricity")
)
at <- data.frame(
  frame     = c("fuel", "fuel", "carrier", "group", "fuel", "carrier"),
  unit      = c("Anthracite", "Lignite", "Oil", "Bio", "Anthracite", "Gas"),
  attribute = c("lhv", "lhv", "lhv", "lhv", "ef_co2", "ef_co2"),
  kind      = c(rep("property", 4), "factor", "factor"),
  value     = c(26.7, 11.9, 42.5, 15.0, 98300, 56100),
  unit_of_measure = c(rep("GJ/t", 4), "kg/TJ", "kg/TJ"),
  source    = "example"
)
al <- data.frame(
  frame  = c("fuel", "carrier", "fuel", "carrier", "carrier"),
  unit   = c("Anthracite", "Coal", "Electricity", "Electricity", "Electricity"),
  alias  = c("hard coal", "coal", "elc", "elc", "power"),
  source = "example"
)
x <- scale_from_leaftable(
  lt, frames = c("group", "phase", "carrier", "fuel"), key = "fuel",
  attributes = at, aliases = al, name = "fuels"
)
x

## ----props--------------------------------------------------------------------
S7::prop_names(x)

## ----leaftable----------------------------------------------------------------
scale_leaftable(x)

## ----frames-------------------------------------------------------------------
scale_frames(x)
scale_atom_level(x)              # the finest frame, or the key
scale_units(x, "carrier")        # the members at one frame
scale_units(x)                   # the atoms
scale_rank(x, "carrier")

## ----navigate-----------------------------------------------------------------
scale_nests(x, "carrier", "fuel")    # a tree
scale_nests(x, "phase", "carrier")   # Biomass is solid, liquid and gas
scale_parents(x, "carrier", "Biomass", target = "phase")
scale_ancestors(x, "fuel", "Biogas")
scale_descendants(x, "group", "Fossil")
head(scale_ancestry(x), 4)

## ----attributes---------------------------------------------------------------
scale_attributes(x)

## ----resolve------------------------------------------------------------------
scale_attributes(x, "lhv", frame = "fuel")[
  c("unit", "value", "unit_of_measure", "origin_frame", "origin_unit")]

## ----kinds--------------------------------------------------------------------
SCALE_ATTRIBUTE_KINDS

## ----aliases------------------------------------------------------------------
scale_aliases(x)
scale_match(x, c("hard coal", "elc", "Lignite"))

## ----meta---------------------------------------------------------------------
scale_weights(x)                        # none declared here
S7::prop(x, "meta")[c("name", "desc")]
carriers <- read_scale("carriers")
scale_weights(carriers)
S7::prop(carriers, "meta")[c("name", "source", "palette_version")]
head(S7::prop(carriers, "meta")$labels, 4)

## ----subset-------------------------------------------------------------------
fossil <- x["group", "Fossil"]
scale_attributes(fossil)[c("frame", "unit", "attribute", "value")]

## ----prune--------------------------------------------------------------------
tree <- scale_from_leaftable(
  lt[c("group", "carrier", "fuel")], frames = c("group", "carrier", "fuel"),
  key = "fuel", attributes = at, aliases = al, name = "tree"
)
scale_aliases(prune_scale(tree, "carrier"))

## ----definition---------------------------------------------------------------
path <- file.path(tempdir(), "fuels.json")
write_scale(x, path)
cat(head(readLines(path), 12), sep = "\n")
back <- read_scale(path)
identical(scale_frames(back), scale_frames(x))
identical(scale_attributes(back), scale_attributes(x))

## ----catalog------------------------------------------------------------------
scale_catalog()[c("id", "name", "kind", "frames", "n_atoms", "package")]

## ----cleanup, include = FALSE-------------------------------------------------
unlink(path)

## ----rules--------------------------------------------------------------------
SCALE_RULES
register_scale_rule("tj", "sum")
register_scale_rule("price", "weighted_mean", weight = "tj")
get_scale_rule("price")
list_scale_rules()
clear_scale_rules()

