Skip to contents
library(tabulate)
# Global styling
class_diagram <- tabulate_table()
class_diagram %>% 
  format_font_style("bold") %>% 
  format_font_align("center") %>% 
  format_width(60)

# Animal class
animal <- tabulate_table() %>% 
  table_add_row("Animal")
animal[1,] %>% 
  format_font_align("center")

# Animal properties nested table
animal_properties <- tabulate_table() %>% 
  table_add_row("+age: Int") %>% 
  table_add_row("+gender: String")

animal_properties %>% 
  format_width(20)

animal_properties[2,] %>% 
  format_hide_border_top()

# Animal methods nested table
animal_methods <- tabulate_table() %>% 
  table_add_row("+isMammal()") %>% 
  table_add_row("+mate()")

animal_methods %>% 
  format_width(20)

animal_methods[2,] %>% 
  format_hide_border_top()

animal %>% 
  table_add_row(animal_properties) %>% 
  table_add_row(animal_methods)

animal[3,] %>% 
  format_hide_border_top()

class_diagram %>% 
  table_add_row(animal)

# Add rows in the class diagram for the up-facing arrow
# THanks to center alignment, these will align just fine
class_diagram %>% 
  table_add_row("▲");
class_diagram[2,1] %>% 
  format_hide_border_top() %>% 
  format_multi_byte_characters(TRUE)

class_diagram %>% 
  table_add_row({"|"})
class_diagram[3,] %>% 
  format_hide_border_top()
class_diagram %>% 
  table_add_row({"|"})
class_diagram[4,] %>% 
  format_hide_border_top()

# Duck class
duck <- tabulate_table() %>% 
  table_add_row("Duck")
duck[1,] %>% 
  format_font_align("center")

# Duck properties nested table
duck_properties <- tabulate_table() %>% 
  table_add_row("+beakColor: String = \"yellow\"")
duck_properties %>% 
  format_width(40)

# Duck methods
duck_methods <- tabulate_table() %>% 
  table_add_row("+swim()") %>% 
  table_add_row("+quack()")
duck_methods %>% 
  format_width(40)
duck_methods[2,] %>% 
  format_hide_border_top()

duck %>% 
  table_add_row(duck_properties) %>% 
  table_add_row(duck_methods)

class_diagram %>% 
  table_add_row(duck)

class_diagram[5,] %>%
  format_hide_border_top()
print(class_diagram)
#> +------------------------------------------------------------+
#> |                 +------------------------+                 |
#> |                 |         Animal         |                 |
#> |                 +------------------------+                 |
#> |                 | +--------------------+ |                 |
#> |                 | | +age: Int          | |                 |
#> |                 | | +gender: String    | |                 |
#> |                 | +--------------------+ |                 |
#> |                 | +--------------------+ |                 |
#> |                 | | +isMammal()        | |                 |
#> |                 | | +mate()            | |                 |
#> |                 | +--------------------+ |                 |
#> |                 +------------------------+                 |
#> |                                                           |
#> |                              |                             |
#> |                              |                             |
#> |       +--------------------------------------------+       |
#> |       |                    Duck                    |       |
#> |       +--------------------------------------------+       |
#> |       | +----------------------------------------+ |       |
#> |       | | +beakColor: String = "yellow"          | |       |
#> |       | +----------------------------------------+ |       |
#> |       +--------------------------------------------+       |
#> |       | +----------------------------------------+ |       |
#> |       | | +swim()                                | |       |
#> |       | | +quack()                               | |       |
#> |       | +----------------------------------------+ |       |
#> |       +--------------------------------------------+       |
#> +------------------------------------------------------------+