assertr

所属分类:collect
开发工具:R
文件大小:0KB
下载次数:0
上传日期:2023-04-30 02:52:16
上 传 者sh-1993
说明:  R分析管道的断言编程,
(Assertive programming for R analysis pipelines,)

文件列表:
.Rbuildignore (198, 2023-11-22)
DESCRIPTION (964, 2023-11-22)
LICENSE (49, 2023-11-22)
Makefile (980, 2023-11-22)
NAMESPACE (1025, 2023-11-22)
NEWS (3304, 2023-11-22)
R/ (0, 2023-11-22)
R/assertions.R (29320, 2023-11-22)
R/assertr-package.R (93, 2023-11-22)
R/assertr.R (2124, 2023-11-22)
R/errors.R (22752, 2023-11-22)
R/predicates.R (15123, 2023-11-22)
R/row-redux.R (7136, 2023-11-22)
R/utils.R (5441, 2023-11-22)
assertr.Rproj (386, 2023-11-22)
codecov.yml (232, 2023-11-22)
codemeta.json (5417, 2023-11-22)
man/ (0, 2023-11-22)
man/assert.Rd (2662, 2023-11-22)
man/assert_rows.Rd (2948, 2023-11-22)
man/assertr.Rd (2134, 2023-11-22)
man/chaining_functions.Rd (1296, 2023-11-22)
man/col_concat.Rd (1132, 2023-11-22)
man/duplicates_across_cols.Rd (1472, 2023-11-22)
man/generate_id.Rd (445, 2023-11-22)
man/has_all_names.Rd (1087, 2023-11-22)
man/has_class.Rd (779, 2023-11-22)
man/has_only_names.Rd (1360, 2023-11-22)
man/in_set.Rd (1713, 2023-11-22)
man/insist.Rd (3124, 2023-11-22)
man/insist_rows.Rd (3249, 2023-11-22)
man/is_uniq.Rd (1455, 2023-11-22)
... ...

assertr === ![assertr logo](https://thepolygram.com/assertrlogo.png) [![R-CMD-check](https://github.com/ropensci/assertr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ropensci/assertr/actions/workflows/R-CMD-check.yaml) [![Codecov test coverage](https://codecov.io/gh/ropensci/assertr/branch/master/graph/badge.svg)](https://app.codecov.io/gh/ropensci/assertr?branch=master) [![CRAN status](https://www.r-pkg.org/badges/version/assertr)](https://CRAN.R-project.org/package=assertr) [![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/assertr)](https://cran.r-project.org/package=assertr) ### What is it? The assertr package supplies a suite of functions designed to verify assumptions about data early in an analysis pipeline so that data errors are spotted early and can be addressed quickly. This package does not need to be used with the magrittr/dplyr piping mechanism but the examples in this README use them for clarity. ### Installation You can install the latest version on CRAN like this ```r install.packages("assertr") ``` or you can install the bleeding-edge development version like this: ```r install.packages("devtools") devtools::install_github("ropensci/assertr") ``` ### What does it look like? This package offers five assertion functions, `assert`, `verify`, `insist`, `assert_rows`, and `insist_rows`, that are designed to be used shortly after data-loading in an analysis pipeline... Let’s say, for example, that the R’s built-in car dataset, `mtcars`, was not built-in but rather procured from an external source that was known for making errors in data entry or coding. Pretend we wanted to find the average miles per gallon for each number of engine cylinders. We might want to first, confirm - that it has the columns "mpg", "vs", and "am" - that the dataset contains more than 10 observations - that the column for 'miles per gallon' (mpg) is a positive number - that the column for ‘miles per gallon’ (mpg) does not contain a datum that is outside 4 standard deviations from its mean, and - that the am and vs columns (automatic/manual and v/straight engine, respectively) contain 0s and 1s only - each row contains at most 2 NAs - each row is unique *jointly* between the "mpg", "am", and "wt" columns - each row's mahalanobis distance is within 10 median absolute deviations of all the distances (for outlier detection) This could be written (in order) using `assertr` like this: ```r library(dplyr) library(assertr) mtcars %>% verify(has_all_names("mpg", "vs", "am", "wt")) %>% verify(nrow(.) > 10) %>% verify(mpg > 0) %>% insist(within_n_sds(4), mpg) %>% assert(in_set(0,1), am, vs) %>% assert_rows(num_row_NAs, within_bounds(0,2), everything()) %>% assert_rows(col_concat, is_uniq, mpg, am, wt) %>% insist_rows(maha_dist, within_n_mads(10), everything()) %>% group_by(cyl) %>% summarise(avg.mpg=mean(mpg)) ``` If any of these assertions were violated, an error would have been raised and the pipeline would have been terminated early. Let's see what the error message look like when you chain a bunch of failing assertions together. ```r > mtcars %>% + chain_start %>% + assert(in_set(1, 2, 3, 4), carb) %>% + assert_rows(rowMeans, within_bounds(0,5), gear:carb) %>% + verify(nrow(.)==10) %>% + verify(mpg < 32) %>% + chain_end There are 7 errors across 4 verbs: - verb redux_fn predicate column index value 1 assert in_set(1, 2, 3, 4) carb 30 6.0 2 assert in_set(1, 2, 3, 4) carb 31 8.0 3 assert_rows rowMeans within_bounds(0, 5) ~gear:carb 30 5.5 4 assert_rows rowMeans within_bounds(0, 5) ~gear:carb 31 6.5 5 verify nrow(.) == 10 1 NA 6 verify mpg < 32 18 NA 7 verify mpg < 32 20 NA Error: assertr stopped execution ``` ### What does `assertr` give me? - `verify` - takes a data frame (its first argument is provided by the `%>%` operator above), and a logical (boolean) expression. Then, `verify` evaluates that expression using the scope of the provided data frame. If any of the logical values of the expression's result are `FALSE`, `verify` will raise an error that terminates any further processing of the pipeline. - `assert` - takes a data frame, a predicate function, and an arbitrary number of columns to apply the predicate function to. The predicate function (a function that returns a logical/boolean value) is then applied to every element of the columns selected, and will raise an error if it finds any violations. Internally, the `assert` function uses `dplyr`'s `select` function to extract the columns to test the predicate function on. - `insist` - takes a data frame, a predicate-generating function, and an arbitrary number of columns. For each column, the the predicate-generating function is applied, returning a predicate. The predicate is then applied to every element of the columns selected, and will raise an error if it finds any violations. The reason for using a predicate-generating function to return a predicate to use against each value in each of the selected rows is so that, for example, bounds can be dynamically generated based on what the data look like; this the only way to, say, create bounds that check if each datum is within x z-scores, since the standard deviation isn't known a priori. Internally, the `insist` function uses `dplyr`'s `select` function to extract the columns to test the predicate function on. - `assert_rows` - takes a data frame, a row reduction function, a predicate function, and an arbitrary number of columns to apply the predicate function to. The row reduction function is applied to the data frame, and returns a value for each row. The predicate function is then applied to every element of vector returned from the row reduction function, and will raise an error if it finds any violations. This functionality is useful, for example, in conjunction with the `num_row_NAs()` function to ensure that there is below a certain number of missing values in each row. Internally, the `assert_rows` function uses `dplyr`'s`select` function to extract the columns to test the predicate function on. - `insist_rows` - takes a data frame, a row reduction function, a predicate-generating function, and an arbitrary number of columns to apply the predicate function to. The row reduction function is applied to the data frame, and returns a value for each row. The predicate-generating function is then applied to the vector returned from the row reduction function and the resultant predicate is applied to each element of that vector. It will raise an error if it finds any violations. This functionality is useful, for example, in conjunction with the `maha_dist()` function to ensure that there are no flagrant outliers. Internally, the `assert_rows` function uses `dplyr`'s`select` function to extract the columns to test the predicate function on. `assertr` also offers four (so far) predicate functions designed to be used with the `assert` and `assert_rows` functions: - `not_na` - that checks if an element is not NA - `within_bounds` - that returns a predicate function that checks if a numeric value falls within the bounds supplied, and - `in_set` - that returns a predicate function that checks if an element is a member of the set supplied. (also allows inverse for "not in set") - `is_uniq` - that checks to see if each element appears only once and predicate generators designed to be used with the `insist` and `insist_rows` functions: - `within_n_sds` - used to dynamically create bounds to check vector elements with based on standard z-scores - `within_n_mads` - better method for dynamically creating bounds to check vector elements with based on 'robust' z-scores (using median absolute deviation) and the following row reduction functions designed to be used with `assert_rows` and `insist_rows`: - `num_row_NAs` - counts number of missing values in each row - `maha_dist` - computes the mahalanobis distance of each row (for outlier detection). It will coerce categorical variables into numerics if it needs to. - `col_concat` - concatenates all rows into strings - `duplicated_across_cols` - checking if a row contains a duplicated value across columns and, finally, some other utilities for use with `verify` - `has_all_names` - check if the data frame or list has all supplied names - `has_only_names` - check that a data frame or list have _only_ the names requested - `has_class` - checks if passed data has a particular class ### More info For more info, check out the `assertr` vignette ```r > vignette("assertr") ``` Or [read it here](https://CRAN.R-project.org/package=assertr/vignettes/assertr.html) # [![ropensci\_footer](https://ropensci.org/public_images/github_footer.png)](https://ropensci.org/)

近期下载者

相关文件


收藏者