--- title: "Compatibility with dplyr" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Compatibility with dplyr} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} 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. ```{r} 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()` ✅ ```{r} x %>% arrange(speed) %>% head() ``` ### `dplyr:distinct()` ✅ ```{r} x %>% distinct() %>% head() ``` ### `dplyr::filter()` ✅ ```{r} x %>% filter(speed >= 5) %>% head() ``` ### `dplyr::slice()` ✅ ```{r} 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: - stay invisible and conserve tags if no tagged column is affected by the operation - trigger `lost_tags_action()` if tagged columns are affected by the operation ### `dplyr::count()` ✅ Count introduces new columns and retains tags for the existing columns: ```{r} 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: ```{r} # 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: ```{r} x %>% pull(speed) ``` ### `dplyr::relocate()` ✅ ```{r} 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. ```{r} 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()`: ```{r} # 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. ```{r} # 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](https://github.com/epiverse-trace/safeframe/issues/46). ## Verbs operating on data.frames ### `dplyr::bind_rows()` ✅ ```{r} dim(x) dim(bind_rows(x, x)) ``` ### `dplyr::bind_cols()` ✘ `bind_cols()` is currently incompatible with safeframe: - tags from the second element are lost - Warnings are produced about lost tags, even for tags that are not actually lost ```{r} 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. ```{r} 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: ```{r} 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.