|
| 1 | +# R Frameworks and Libraries |
| 2 | + |
| 3 | +- [Packages Management](#packages-management) |
| 4 | + - [Package Management Frameworks](#package-management-frameworks) |
| 5 | + - [Loading Packages](#loading-packages) |
| 6 | + - [Reproducible Environments](#reproducible-environments) |
| 7 | +- [Assertions and Testing](#assertions-and-testing) |
| 8 | + |
| 9 | + |
| 10 | +## Packages Management |
| 11 | + |
| 12 | +### Package Management Frameworks |
| 13 | + |
| 14 | +- **renv**: A dependency management toolkit for R that helps you create reproducible environments. It allows you to isolate project-specific package libraries. |
| 15 | +- **librarian**: A package that simplifies the process of loading and installing R packages, making it easier to manage dependencies. |
| 16 | +- **checkpoint**: A package that allows you to install packages from a specific date, ensuring that your project uses the same package versions over time. |
| 17 | + |
| 18 | +### Loading Packages |
| 19 | + |
| 20 | +You can use the `librarian` package to load and install packages easily. Here's an example: |
| 21 | + |
| 22 | +```R |
| 23 | +install.packages("librarian") |
| 24 | + |
| 25 | +# Add librarian to default list of packages that load on session start |
| 26 | +librarian::lib_startup(librarian, lib = .libPaths()[1], global = F) |
| 27 | + |
| 28 | +# Load and install packages as needed |
| 29 | +librarian::shelf( |
| 30 | + dplyr, |
| 31 | + ggplot2, |
| 32 | + tidyr, |
| 33 | + readr |
| 34 | +) |
| 35 | +``` |
| 36 | + |
| 37 | +### Reproducible Environments |
| 38 | + |
| 39 | +Use `renv` to manage R package dependencies for your projects. This ensures that your code runs consistently across different systems. |
| 40 | + |
| 41 | +```R |
| 42 | +shelf(renv) # or install.packages("renv") |
| 43 | + |
| 44 | +renv::init() # Initialize a new renv environment |
| 45 | +renv::snapshot() # Save the current state of your project's R packages |
| 46 | +renv::restore() # Restore the project's R packages to the saved state |
| 47 | +renv::status() # Check the status of your renv environment |
| 48 | +``` |
| 49 | + |
| 50 | +## Assertions and Testing |
| 51 | + |
| 52 | +- **checkmate**: A package that provides functions for argument checking and validation, making it easier to ensure that your functions receive the correct types of inputs. |
| 53 | +- **ensurer**: A package that allows you to create validation rules for your data, helping you ensure that your data meets specific criteria. |
| 54 | +- **validate**: A package for data validation that allows you to define and apply validation rules to your datasets. |
| 55 | + |
| 56 | +```R |
| 57 | +librarian::shelf( |
| 58 | + checkmate, |
| 59 | + ensurer, |
| 60 | + validate |
| 61 | +) |
| 62 | + |
| 63 | +# Example usage of checkmate |
| 64 | +checkmate::assert_numeric(x, lower = 0, upper = 100) |
| 65 | +checkmate::assert_character(name, pattern = "^[a-zA-Z]+$") |
| 66 | +checkmate::assert_data_frame(df, min.rows = 1) |
| 67 | + |
| 68 | +# Example usage of ensurer |
| 69 | +safe_sqrt <- function(x) { |
| 70 | + sqrt(x) |
| 71 | +} %>% |
| 72 | + ensures_that(x >= 0, err_desc = "Input must be non-negative") %>% |
| 73 | + ensures_that(. >= 0, err_desc = "Output must be non-negative") |
| 74 | + |
| 75 | +# Example usage of validate |
| 76 | +rules <- validator( |
| 77 | + age >= 0, |
| 78 | + income >= 0 |
| 79 | +) |
| 80 | +validation_results <- confront(data, rules) |
| 81 | +summary(validation_results) |
| 82 | +``` |
0 commit comments