These functions restore selection of keys using corresponding scoped variant of select. .funs argument can be used to rename some keys (without touching actual keys) before restoring.

restore_keys_all(.tbl, .funs = list(), ..., .remove = FALSE, .unkey = FALSE)

restore_keys_if(.tbl, .predicate, .funs = list(), ..., .remove = FALSE,
  .unkey = FALSE)

restore_keys_at(.tbl, .vars, .funs = list(), ..., .remove = FALSE,
  .unkey = FALSE)

Arguments

.tbl

Reference data frame.

.funs

Parameter for scoped functions.

...

Parameter for scoped functions.

.remove

Whether to remove keys after restoring.

.unkey

Whether to unkey() .tbl in case there are no keys left.

.predicate

Parameter for scoped functions.

.vars

Parameter for scoped functions.

Examples

df <- mtcars %>% dplyr::as_tibble() %>% key_by(vs, am, disp)
# Just restore all keys
df %>% restore_keys_all()
#> # A keyed object. Keys: vs, am, disp 
#> # A tibble: 32 × 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # … with 22 more rows

# Restore all keys with renaming and without touching actual keys
df %>% restore_keys_all(.funs = toupper)
#> # A keyed object. Keys: vs, am, disp 
#> # A tibble: 32 × 14
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb    VS    AM
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4     0     1
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4     0     1
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1     1     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1     1     0
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2     0     0
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1     1     0
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4     0     0
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2     1     0
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2     1     0
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4     1     0
#> # … with 22 more rows, and 1 more variable: DISP <dbl>

# Restore with renaming and removing
df %>%
  restore_keys_all(.funs = toupper, .remove = TRUE)
#> # A keyed object. Keys: there are no keys.
#> # A tibble: 32 × 14
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb    VS    AM
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4     0     1
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4     0     1
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1     1     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1     1     0
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2     0     0
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1     1     0
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4     0     0
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2     1     0
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2     1     0
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4     1     0
#> # … with 22 more rows, and 1 more variable: DISP <dbl>

# Restore with renaming, removing and unkeying
df %>%
  restore_keys_all(.funs = toupper, .remove = TRUE, .unkey = TRUE)
#> # A tibble: 32 × 14
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb    VS    AM
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4     0     1
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4     0     1
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1     1     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1     1     0
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2     0     0
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1     1     0
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4     0     0
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2     1     0
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2     1     0
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4     1     0
#> # … with 22 more rows, and 1 more variable: DISP <dbl>

# Restore with renaming keys satisfying the predicate
df %>%
  restore_keys_if(rlang::is_integerish, .funs = toupper)
#> # A keyed object. Keys: vs, am, disp 
#> # A tibble: 32 × 13
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb    VS    AM
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4     0     1
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4     0     1
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1     1     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1     1     0
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2     0     0
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1     1     0
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4     0     0
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2     1     0
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2     1     0
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4     1     0
#> # … with 22 more rows

# Restore with renaming specified keys
df %>%
  restore_keys_at(c("vs", "disp"), .funs = toupper)
#> # A keyed object. Keys: vs, am, disp 
#> # A tibble: 32 × 13
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb    VS  DISP
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4     0  160 
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4     0  160 
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1     1  108 
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1     1  258 
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2     0  360 
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1     1  225 
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4     0  360 
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2     1  147.
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2     1  141.
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4     1  168.
#> # … with 22 more rows