knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
msafer is a package that frees users from having to manually find the location of an error when using the function map()
. When map()
returns an error, it's unclear where this error occurs. The function map_safe()
takes in a vector, function and requirements, and spits out TRUE
or FALSE
based on whether an error occurs when running the function over the vector. map_safe()
will return a list of logical returns where it will be easy to identify the place of the error.
You can install the development version of msafer from GitHub with:
``` {r, eval=FALSE}
devtools::install_github("kpien/msafer")
## Example For demonstration purposes, let's create a sample list of dataframes from the `starwars` and `mtcars` datasets. ```r library(dplyr) sample_a <- starwars %>% sample_n(34) %>% select(-height, -hair_color) sample_b <- sample_n(starwars, 35) sample_c <- mtcars %>% sample_n(20) %>% select(-hp) sample_d <- tibble::tibble() list_sample <- list(starwars, sample_a, sample_b, sample_c, mtcars, sample_d)
Pass map_safe_merge()
the same arguments as map()
. These would be the list of dataframes, the function to map over all the dataframes, and any arguments that the function needs. map_safe_merge()
will return a tibble with the file numbers and any errors that may have occurred while trying to apply map()
, grouping repeat errors together.
library(msafer) map_safe_merge(list_sample, select, height)
However, if the vector is huge, then the result would be messy and hard to grab information from the tibble. Thus, we can use map_safe()
which nests the error message, and returns a tibble that contains only the unique error message and the index locating where the error occurs within the vector.
sample_df <- map_safe(list_sample, dplyr::select, height) sample_df sample_df %>% tidyr::unnest(which_id)
check_match()
identified whether the user’s requirement existed within the dataset/list of dataframes, if yes, then the function will return TRUE
, if not, it returns FALSE
.
check_match(dplyr::starwars, height == 172)
check_match()
and also be used under map_safe()
when applying check_match()
to a vector which could contain multiple elements
d <- map_safe(list_sample, check_match, height == 172) d
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.