Skip to contents

boundary_tree() displays prefectures, municipalities, former municipalities, and agricultural communities as a tree that can be printed or saved as text.

Usage

boundary_tree(
  boundary,
  file = NULL,
  show_romaji = TRUE,
  show_year = TRUE,
  show_key = TRUE,
  show_count = TRUE,
  ascii = FALSE,
  max_lines = Inf
)

Arguments

boundary

A data frame or a list of data frames with boundary attributes, such as the result of get_boundary() or a data frame from extract_boundary(). For prefecture-only data, pref_name is sufficient. For lower levels, include key and a name column (city_name, kcity_name, or rcom_name) or, when show_romaji = TRUE, a corresponding _romaji column.

file

A file path for UTF-8 text output. If NULL, display the tree in the console. An existing file at the specified path is overwritten.

show_romaji

Logical. If TRUE, include Romanized names where available.

show_year

Logical. If TRUE, show the agricultural community reference year (rcom_year) and boundary dataset year (boundary_data_year) in the heading. List all known dataset years when more than one is present. Use unknown when a year is unavailable.

show_key

Logical. If TRUE, show available region codes next to names.

show_count

Logical. If TRUE, append / ... to region labels with child nodes. The count includes the distinct nodes displayed immediately below that label.

ascii

Logical. If TRUE, use ASCII branch markers.

max_lines

A non-negative integer or Inf (the default). Limits the number of tree lines displayed in the console, including headings. When lines are omitted, an additional line reports how many. File output and the returned character vector contain the full tree.

Value

Invisibly returns a character vector containing one element per line.

Details

Nodes are identified by the first 2, 5, 7, or 10 characters of key, so regions with the same name remain distinct. Levels with no label column across the input are skipped. Any available lower levels are attached to the nearest displayed parent. When a name is missing within a displayed level, the region code serves as its label. In prefecture-only data without key, missing or blank names are skipped and repeated names appear once per reference year. A name that matches a single prefecture code in the same tree is grouped with that prefecture. Boundaries spanning multiple reference years are shown as separate trees with the reference year in each heading.

Examples

b <- data.frame(
  key = c("3820102001", "3820102002"),
  pref_name = "\u611b\u5a9b\u770c",
  city_name = "\u677e\u5c71\u5e02",
  kcity_name = "\u8208\u5c45\u5cf6\u6751",
  rcom_name = c("\u5fa1\u624b\u6d17", "\u6cca"),
  rcom_year = 2025L,
  boundary_data_year = 2025L
)
boundary_tree(b, show_count = TRUE)
#> Agricultural community boundaries (rcom_year = 2025, boundary_data_year = 2025)
#> └─ 愛媛県 [38] / 1
#>     └─ 松山市 [38201] / 1
#>         └─ 興居島村 [3820102] / 2
#>             ├─ 御手洗 [3820102001]
#>             └─ 泊 [3820102002]
boundary_tree(b, max_lines = 4)
#> Agricultural community boundaries (rcom_year = 2025, boundary_data_year = 2025)
#> └─ 愛媛県 [38] / 1
#>     └─ 松山市 [38201] / 1
#>         └─ 興居島村 [3820102] / 2
#> ... (2 lines omitted)
boundary_tree(b["pref_name"], show_year = FALSE)
#> Agricultural community boundaries
#> └─ 愛媛県
boundary_tree(b, file = tempfile(fileext = ".txt"))