Compatibility with dplyr

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

safeframe philosophy is to prevent you from accidentally losing valuable data, but to otherwise be totally transparent and not to interfere with your workflow.

One popular ecosystem for data science workflow is the tidyverse. We try to ensure decent safeframe compatibility with the tidyverse. All dplyr verbs are tested in the tests/test-compat-dplyr.R file.

library(safeframe)
library(dplyr)

x <- make_safeframe(
  cars,
  mph = "speed",
  distance = "dist"
)

head(x)

Verbs operating on rows

safeframe does not modify anything regarding the behaviour for row-operations. As such, it is fully compatible with dplyr verbs operating on rows out-of-the-box. You can see in the following examples that safeframe does not produce any errors, warnings or messspeeds and its tags are conserved through dplyr operations on rows.

dplyr::arrange()

x %>%
  arrange(speed) %>%
  head()

dplyr:distinct()

x %>%
  distinct() %>%
  head()

dplyr::filter()

x %>%
  filter(speed >= 5) %>%
  head()

dplyr::slice()

x %>%
  slice(5:10)

x %>%
  slice_head(n = 5)

x %>%
  slice_tail(n = 5)

x %>%
  slice_min(speed, n = 3)

x %>%
  slice_max(speed, n = 3)

x %>%
  slice_sample(n = 5)

Verbs operating on columns

During operations on columns, safeframe will:

dplyr::count()

Count introduces new columns and retains tags for the existing columns:

x %>%
  count(speed) %>%
  head()

dplyr::mutate() ✓ (partial)

There is an incomplete compatibility with dplyr::mutate() in that simple renames without any actual modification of the column don't update the tags. In this scenario, users should rather use dplyr::rename()

Although dplyr::mutate() is not able to leverspeed to full power of safeframe tags, safeframe objects behave as expected the same way a data.frame would:

# In place modification doesn't lose tags
x %>%
  mutate(speed = speed + 10) %>%
  head()

# New columns don't affect existing tags
x %>%
  mutate(ticket = speed >= 50) %>%
  head()

# .keep = "unused" generate expected tag loss conditions
x %>%
  mutate(edad = speed, .keep = "unused") %>%
  head()

dplyr::pull()

dplyr::pull() returns a vector, which maintains the tag but result in loss of the safeframe class:

x %>%
  pull(speed)

dplyr::relocate()

x %>%
  relocate(speed, .before = 1) %>%
  head()

dplyr::rename() & dplyr::rename_with()

dplyr::rename() is fully compatible out-of-the-box with safeframe, meaning that tags will be updated at the same time that columns are renamed.

x %>%
  rename(edad = speed) %>%
  head()

x %>%
  rename_with(toupper) %>%
  head()

dplyr::select()

dplyr::select() is fully compatible with safeframe, including when columns are renamed in a select():

# Works fine
x %>%
  select(speed, dist) %>%
  head()

# tags are updated!
x %>%
  select(dist, edad = speed) %>%
  head()

Verbs operating on groups ✘

Groups are not yet supported. Applying any verb operating on group to a safeframe will silently convert it back to a data.frame or tibble.

# Does not retain tags
x %>%
  group_by(speed) %>%
  head()

Please indicate if you would like to see this supported in a future release by commenting on the GitHub issue about this.

Verbs operating on data.frames

dplyr::bind_rows()

dim(x)

dim(bind_rows(x, x))

dplyr::bind_cols()

bind_cols() is currently incompatible with safeframe:

bind_cols(
  suppressWarnings(select(x, speed)),
  suppressWarnings(select(x, dist))
) %>%
  head()

Joins ✘

Joins are currently not compatible with safeframe as tags from the second element are silently dropped.

full_join(
  suppressWarnings(select(x, speed, dist)),
  suppressWarnings(select(x, dist, speed))
) %>%
  head()

Verbs operating on multiple columns

dplyr::pick()

pick() makes tidyselect functions work in usually tidyselect-incompatible functions, such as:

x %>%
  dplyr::arrange(dplyr::pick(ends_with("loc"))) %>%
  head()

As such, we could expect it to work with safeframe custom tidyselect-like function: has_tag() but it's not the case since pick() currently strips out all attributes, including the safeframe class and all tags. This unclassing is documented in ?pick:

pick() returns a data frame containing the selected columns for the current group.



Try the safeframe package in your browser

Any scripts or data that you put into this service are public.

safeframe documentation built on June 28, 2025, 1:08 a.m.