Combine tinytable objects by rows (vertically)
Source: R/rbind2.R
rbind2-tinytable-tinytable-method.RdCombine tinytable objects by rows (vertically)
Usage
# S4 method for class 'tinytable,tinytable'
rbind2(x, y, use_names = TRUE, headers = TRUE, ...)Details
Transformations recorded via format_tt() and style_tt() are evaluated at the very end of the rendering pipeline, after rbind2() has combined the tables. When headers are inserted or columns differ in type, the combined data is first coerced to character, so subsequent formatting/styling works on strings. Apply format_tt() directly to raw data frames before calling tt(), or re-run the formatting/styling steps on the combined table to preserve rounding and other rules.
Calls to other tinytable functions such as style_tt() or group_tt() are ignored when applied to x or y. These functions should be applied to the final table instead.
Information in these S4 slots is carried over from x to the combined table:
x@outputx@captionx@width
Information in these S4 slots is concatenated and carried over to the combined table:
c(x@notes, y@notes)
This function relies on the rbindlist() function from the data.table package.
Examples
library(tinytable)
x <- tt(mtcars[1:3, 1:2], caption = "Combine two tiny tables.")
y <- tt(mtcars[4:5, 8:10])
# rbind() does not support additional aarguments
# rbind2() supports additional arguments
# basic combination
rbind(x, y)
#>
#> +------+-----+----+----+------+
#> | mpg | cyl | vs | am | gear |
#> +======+=====+====+====+======+
#> | 21.0 | 6 | NA | NA | NA |
#> +------+-----+----+----+------+
#> | 21.0 | 6 | NA | NA | NA |
#> +------+-----+----+----+------+
#> | 22.8 | 4 | NA | NA | NA |
#> +------+-----+----+----+------+
#> | NA | NA | vs | am | gear |
#> +------+-----+----+----+------+
#> | NA | NA | 1 | 0 | 3 |
#> +------+-----+----+----+------+
#> | NA | NA | 0 | 0 | 3 |
#> +------+-----+----+----+------+
#>
#> Table: Combine two tiny tables.
rbind(x, y) |> format_tt(replace = "")
#>
#> +------+-----+----+----+------+
#> | mpg | cyl | vs | am | gear |
#> +======+=====+====+====+======+
#> | 21.0 | 6 | | | |
#> +------+-----+----+----+------+
#> | 21.0 | 6 | | | |
#> +------+-----+----+----+------+
#> | 22.8 | 4 | | | |
#> +------+-----+----+----+------+
#> | | | vs | am | gear |
#> +------+-----+----+----+------+
#> | | | 1 | 0 | 3 |
#> +------+-----+----+----+------+
#> | | | 0 | 0 | 3 |
#> +------+-----+----+----+------+
#>
#> Table: Combine two tiny tables.
# omit y header
rbind2(x, y, headers = FALSE)
#>
#> +------+-----+----+----+------+
#> | mpg | cyl | vs | am | gear |
#> +======+=====+====+====+======+
#> | 21.0 | 6 | NA | NA | NA |
#> +------+-----+----+----+------+
#> | 21.0 | 6 | NA | NA | NA |
#> +------+-----+----+----+------+
#> | 22.8 | 4 | NA | NA | NA |
#> +------+-----+----+----+------+
#> | NA | NA | 1 | 0 | 3 |
#> +------+-----+----+----+------+
#> | NA | NA | 0 | 0 | 3 |
#> +------+-----+----+----+------+
#>
#> Table: Combine two tiny tables.
# bind by position rather than column names
rbind2(x, y, use_names = FALSE)
#>
#> +------+-----+------+
#> | mpg | cyl | gear |
#> +======+=====+======+
#> | 21.0 | 6 | NA |
#> +------+-----+------+
#> | 21.0 | 6 | NA |
#> +------+-----+------+
#> | 22.8 | 4 | NA |
#> +------+-----+------+
#> | vs | am | gear |
#> +------+-----+------+
#> | 1 | 0 | 3 |
#> +------+-----+------+
#> | 0 | 0 | 3 |
#> +------+-----+------+
#>
#> Table: Combine two tiny tables.
# `iris` example with pre-tt() formatting
dat <- iris[1:3, 1:4]
a <- format_tt(dat, i = 1:3, digits = 1) |> tt()
b <- format_tt(dat, i = 1:3, digits = 2) |> tt()
rbind2(a, b)
#>
#> +--------------+-------------+--------------+-------------+
#> | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width |
#> +==============+=============+==============+=============+
#> | 5 | 4 | 1 | 0.2 |
#> +--------------+-------------+--------------+-------------+
#> | 5 | 3 | 1 | 0.2 |
#> +--------------+-------------+--------------+-------------+
#> | 5 | 3 | 1 | 0.2 |
#> +--------------+-------------+--------------+-------------+
#> | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width |
#> +--------------+-------------+--------------+-------------+
#> | 5.1 | 3.5 | 1.4 | 0.2 |
#> +--------------+-------------+--------------+-------------+
#> | 4.9 | 3 | 1.4 | 0.2 |
#> +--------------+-------------+--------------+-------------+
#> | 4.7 | 3.2 | 1.3 | 0.2 |
#> +--------------+-------------+--------------+-------------+