I like the idea of pack and unpack, especially as they sometimes come quite natural when working with JSON data from APIs. In such a case I might want to unpack a tibble column, manipulate it and pack it afterwards again. E.g.
library(tidyr)
api_data <- tibble(
Sepal = tibble(
Length = c(5.1, 4.7),
Width = c(3.5, 3.2)
),
Petal = tibble(
Length = c(1.4, 1.3),
Width = c(0.2, 0.2)
)
)
data_to_work_with <- api_data %>%
unpack(c(Sepal, Petal), names_sep = ".")
# some data manipulation
data_to_work_with %>%
pack(Sepal = starts_with("Sepal"))
#> # A tibble: 2 x 3
#> Petal.Length Petal.Width Sepal$Sepal.Length $Sepal.Width
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1.4 0.2 5.1 3.5
#> 2 1.3 0.2 4.7 3.2
Created on 2019-10-30 by the reprex package (v0.3.0)
Unfortunately, this repacking step isn't supported very nicely as I cannot change the names in the step of packing. I would imagine something along the following lines to be possible:
data_to_work_with %>%
pack(Sepal = c(Length = Sepal.Length, Width = Sepal.Width))
data_to_work_with %>%
pack(Sepal = c(Sepal.Length, Width = Width), .prefix = "Sepal.")
The first case is especially confusing as the names are simply ignored without a warning.
I like the idea of pack and unpack, especially as they sometimes come quite natural when working with JSON data from APIs. In such a case I might want to unpack a tibble column, manipulate it and pack it afterwards again. E.g.
Created on 2019-10-30 by the reprex package (v0.3.0)
Unfortunately, this repacking step isn't supported very nicely as I cannot change the names in the step of packing. I would imagine something along the following lines to be possible:
The first case is especially confusing as the names are simply ignored without a warning.