R get0
Display a fallback stock count for an item missing from the ledger.
A simple example
get0
- Start with Map with A = 2
- Do this Read missing B with fallback 0
- Result 0
Syntax
get0(item, envir = stock, inherits = FALSE, ifnotfound = 0L)
Run these examples in R
Save one example at a time as Lesson.R. Use R 4.x.
Rscript --vanilla Lesson.RShow a balance for an item never received
A kiosk receives its first delivery of pencils but has yet to receive any rulers. Its stock display should show zero for rulers while keeping the ledger limited to products actually received.
stock <- list2env(list(Pencil = 12L), parent = emptyenv())
on_hand <- get0("Ruler", envir = stock, inherits = FALSE, ifnotfound = 0L)
cat(sprintf("%d on hand, %d product", on_hand, length(stock)))
0 on hand, 1 product
The display shows zero for rulers. The original pencil entry is still the only product in the ledger.
Look up a borrowed tripod
The equipment desk has a two-day loan period for a camera, but no special period for a tripod. The system uses the recorded value when present and the displayed fallback otherwise. Which value reaches the screen?
Read a room preference
The timetable stores a room choice for the choir club. The system uses the recorded value when present and the displayed fallback otherwise. Which value reaches the screen?
What to watch for
Turn inheritance off so the lookup stays inside this inventory. ifnotfound supplies the fallback without creating a binding.