# R 4.4+ Complete Reference

## R 4.4 Feature Matrix

| Feature                  | Version | Status | Production Ready |
| ------------------------ | ------- | ------ | ---------------- |
| Native Pipe `\|>`        | 4.1+    | Stable | Yes              |
| Lambda Syntax `\(x)`     | 4.1+    | Stable | Yes              |
| Placeholder `_` in Pipes | 4.2+    | Stable | Yes              |
| `\(x, y)` Shorthand      | 4.1+    | Stable | Yes              |
| Improved Error Messages  | 4.4+    | Stable | Yes              |
| Performance Improvements | 4.4+    | Stable | Yes              |

### R 4.4 New Features

Improved Error Handling:

```r
# Better error messages with suggestions
mean(c(1, 2, "three"))
# Error in mean.default(c(1, 2, "three")) :
#   argument is not numeric or logical: returning NA
# Did you mean to use as.numeric() first?

# More informative warnings
data.frame(x = 1:3, y = 1:2)
# Warning: longer object length is not a multiple of shorter object length
# Columns: x (length 3), y (length 2)
```

Performance Optimizations:

```r
# Faster data frame operations
system.time({
  df <- data.frame(x = 1:1e6, y = rnorm(1e6))
  df$z <- df$x + df$y
})

# Improved memory management
gc(verbose = TRUE)
```

---

## tidyverse Package Reference

### dplyr 1.1+ Complete Functions

#### Core Verbs

| Function      | Purpose               | Example                             |
| ------------- | --------------------- | ----------------------------------- |
| `filter()`    | Subset rows           | `filter(data, x > 5)`               |
| `select()`    | Select columns        | `select(data, x, y)`                |
| `mutate()`    | Create/modify columns | `mutate(data, z = x + y)`           |
| `summarise()` | Aggregate data        | `summarise(data, mean_x = mean(x))` |
| `arrange()`   | Sort rows             | `arrange(data, desc(x))`            |
| `group_by()`  | Group data            | `group_by(data, category)`          |
| `ungroup()`   | Remove grouping       | `ungroup(data)`                     |

#### Selection Helpers

| Helper          | Purpose               | Example                             |
| --------------- | --------------------- | ----------------------------------- |
| `starts_with()` | Columns starting with | `select(data, starts_with("x"))`    |
| `ends_with()`   | Columns ending with   | `select(data, ends_with("_id"))`    |
| `contains()`    | Columns containing    | `select(data, contains("date"))`    |
| `matches()`     | Regex pattern         | `select(data, matches("\\d+"))`     |
| `num_range()`   | Numbered columns      | `select(data, num_range("x", 1:3))` |
| `where()`       | Function predicate    | `select(data, where(is.numeric))`   |
| `everything()`  | All columns           | `select(data, id, everything())`    |
| `last_col()`    | Last column(s)        | `select(data, last_col())`          |

#### Window Functions

| Function         | Purpose                 | Example                                |
| ---------------- | ----------------------- | -------------------------------------- |
| `row_number()`   | Row number              | `mutate(data, rank = row_number(x))`   |
| `ntile()`        | Percentile groups       | `mutate(data, quartile = ntile(x, 4))` |
| `min_rank()`     | Ranking                 | `mutate(data, rank = min_rank(x))`     |
| `percent_rank()` | Percent rank            | `mutate(data, pct = percent_rank(x))`  |
| `cume_dist()`    | Cumulative distribution | `mutate(data, cdf = cume_dist(x))`     |
| `lag()`          | Previous value          | `mutate(data, prev = lag(x))`          |
| `lead()`         | Next value              | `mutate(data, next = lead(x))`         |
| `cumsum()`       | Cumulative sum          | `mutate(data, total = cumsum(x))`      |
| `cummean()`      | Cumulative mean         | `mutate(data, avg = cummean(x))`       |

#### Advanced Functions

across() Patterns:

```r
# Multiple functions
data |>
  mutate(
    across(
      where(is.numeric),
      list(
        mean = mean,
        sd = sd,
        min = min,
        max = max
      ),
      .names = "{.col}_{.fn}"
    )
  )

# With arguments
data |>
  summarise(
    across(
      where(is.numeric),
      \(x) mean(x, na.rm = TRUE),
      .names = "mean_{.col}"
    )
  )

# Multiple columns and functions
data |>
  mutate(
    across(
      c(x, y, z),
      list(log = log, sqrt = sqrt),
      .names = "{.fn}_{.col}"
    )
  )
```

if_any() and if_all():

```r
# Filter rows where ANY condition is true
data |>
  filter(
    if_any(
      c(revenue, profit, growth),
      \(x) x > quantile(x, 0.9, na.rm = TRUE)
    )
  )

# Filter rows where ALL conditions are true
data |>
  filter(
    if_all(
      starts_with("score_"),
      \(x) x >= 80
    )
  )
```

### tidyr 1.3+ Complete Functions

| Function                 | Purpose               | Example                                |
| ------------------------ | --------------------- | -------------------------------------- |
| `pivot_longer()`         | Wide to long          | `pivot_longer(data, cols = c(x, y))`   |
| `pivot_wider()`          | Long to wide          | `pivot_wider(data, names_from = key)`  |
| `separate()`             | Split column          | `separate(data, col, c("a", "b"))`     |
| `separate_wider_delim()` | Modern separate       | `separate_wider_delim(data, col, ",")` |
| `unite()`                | Combine columns       | `unite(data, "new", x, y)`             |
| `nest()`                 | Create list-column    | `nest(data, data = c(x, y))`           |
| `unnest()`               | Expand list-column    | `unnest(data, cols = c(data))`         |
| `complete()`             | Fill combinations     | `complete(data, x, y)`                 |
| `expand()`               | Generate combinations | `expand(data, x, y)`                   |
| `drop_na()`              | Remove NA rows        | `drop_na(data)`                        |
| `fill()`                 | Fill missing values   | `fill(data, x, .direction = "down")`   |
| `replace_na()`           | Replace NA values     | `replace_na(data, list(x = 0))`        |

### purrr 1.0+ Complete Functions

Map Family:

```r
# Basic map variants
map(.x, .f)              # Returns list
map_dbl(.x, .f)          # Returns numeric vector
map_chr(.x, .f)          # Returns character vector
map_lgl(.x, .f)          # Returns logical vector
map_int(.x, .f)          # Returns integer vector
map_dfr(.x, .f)          # Returns data frame (row-bind)
map_dfc(.x, .f)          # Returns data frame (column-bind)

# Multiple inputs
map2(.x, .y, .f)         # Two inputs
pmap(.l, .f)             # Multiple inputs (list)

# Indexed mapping
imap(.x, .f)             # With index/name
```

Error Handling:

```r
# safely() - Returns list(result, error)
safe_fn <- safely(function(x) x / 0)
safe_fn(10)
# $result: Inf
# $error: NULL

# possibly() - Returns default on error
poss_fn <- possibly(function(x) x / 0, otherwise = NA)
poss_fn(10)  # NA

# quietly() - Captures output/messages
quiet_fn <- quietly(function(x) { message("hi"); x + 1 })
quiet_fn(5)
# $result: 6
# $output: ""
# $warnings: character(0)
# $messages: "hi\n"
```

List Manipulation:

```r
keep(.x, .p)             # Keep elements matching predicate
discard(.x, .p)          # Remove elements matching predicate
compact(.x)              # Remove NULL elements
pluck(.x, ...)           # Extract element by position/name
chuck(.x, ...)           # Like pluck but errors on missing
flatten(.x)              # Flatten one level
flatten_dbl(.x)          # Flatten to numeric vector
```

Reduce/Accumulate:

```r
reduce(.x, .f)           # Combine elements sequentially
reduce2(.x, .y, .f)      # Reduce with auxiliary input
accumulate(.x, .f)       # Like reduce but return intermediates
```

### stringr 1.5+ String Operations

| Function            | Purpose              | Example                           |
| ------------------- | -------------------- | --------------------------------- |
| `str_detect()`      | Detect pattern       | `str_detect(x, "pattern")`        |
| `str_extract()`     | Extract first match  | `str_extract(x, "\\d+")`          |
| `str_extract_all()` | Extract all matches  | `str_extract_all(x, "\\w+")`      |
| `str_replace()`     | Replace first match  | `str_replace(x, "old", "new")`    |
| `str_replace_all()` | Replace all matches  | `str_replace_all(x, "\\d+", "X")` |
| `str_remove()`      | Remove first match   | `str_remove(x, "pattern")`        |
| `str_remove_all()`  | Remove all matches   | `str_remove_all(x, "\\s+")`       |
| `str_split()`       | Split string         | `str_split(x, ",")`               |
| `str_c()`           | Concatenate          | `str_c(x, y, sep = " ")`          |
| `str_glue()`        | Interpolate          | `str_glue("Value: {x}")`          |
| `str_to_lower()`    | Lowercase            | `str_to_lower(x)`                 |
| `str_to_upper()`    | Uppercase            | `str_to_upper(x)`                 |
| `str_to_title()`    | Title case           | `str_to_title(x)`                 |
| `str_trim()`        | Remove whitespace    | `str_trim(x)`                     |
| `str_squish()`      | Normalize whitespace | `str_squish(x)`                   |
| `str_pad()`         | Pad string           | `str_pad(x, width = 10)`          |
| `str_sub()`         | Substring            | `str_sub(x, start = 1, end = 5)`  |
| `str_length()`      | String length        | `str_length(x)`                   |

### forcats 1.0+ Factor Functions

| Function            | Purpose            | Example                                    |
| ------------------- | ------------------ | ------------------------------------------ |
| `fct_relevel()`     | Reorder levels     | `fct_relevel(f, "b", "a", "c")`            |
| `fct_reorder()`     | Reorder by value   | `fct_reorder(f, x, .fun = median)`         |
| `fct_infreq()`      | Order by frequency | `fct_infreq(f)`                            |
| `fct_rev()`         | Reverse levels     | `fct_rev(f)`                               |
| `fct_lump()`        | Lump infrequent    | `fct_lump(f, n = 5)`                       |
| `fct_recode()`      | Rename levels      | `fct_recode(f, new = "old")`               |
| `fct_collapse()`    | Combine levels     | `fct_collapse(f, new = c("a", "b"))`       |
| `fct_explicit_na()` | Make NA explicit   | `fct_explicit_na(f, na_level = "Missing")` |

---

## ggplot2 3.5+ Complete Reference

### Geom Reference Table

| Geom               | Purpose            | Required Aesthetics |
| ------------------ | ------------------ | ------------------- |
| `geom_point()`     | Scatter plot       | x, y                |
| `geom_line()`      | Line plot          | x, y                |
| `geom_path()`      | Connected points   | x, y                |
| `geom_bar()`       | Bar chart (count)  | x                   |
| `geom_col()`       | Bar chart (values) | x, y                |
| `geom_histogram()` | Histogram          | x                   |
| `geom_boxplot()`   | Box plot           | x, y                |
| `geom_violin()`    | Violin plot        | x, y                |
| `geom_density()`   | Density plot       | x                   |
| `geom_area()`      | Area plot          | x, y                |
| `geom_tile()`      | Heatmap            | x, y, fill          |
| `geom_raster()`    | Fast heatmap       | x, y, fill          |
| `geom_polygon()`   | Polygons           | x, y                |
| `geom_smooth()`    | Smoothed line      | x, y                |
| `geom_text()`      | Text labels        | x, y, label         |
| `geom_label()`     | Text with box      | x, y, label         |
| `geom_errorbar()`  | Error bars         | x, ymin, ymax       |
| `geom_ribbon()`    | Confidence band    | x, ymin, ymax       |
| `geom_segment()`   | Line segments      | x, y, xend, yend    |

### Scale Reference

Color/Fill Scales:

```r
# Continuous scales
scale_color_gradient()        # 2-color gradient
scale_color_gradient2()       # 3-color gradient (diverging)
scale_color_gradientn()       # n-color gradient
scale_color_viridis_c()       # Viridis continuous

# Discrete scales
scale_color_manual()          # Manual colors
scale_color_brewer()          # ColorBrewer palettes
scale_color_viridis_d()       # Viridis discrete
scale_color_grey()            # Greyscale

# Binned scales
scale_color_steps()           # Stepped gradient
scale_color_steps2()          # Stepped diverging
scale_color_stepsn()          # n-step gradient
```

Position Scales:

```r
# X/Y scales
scale_x_continuous()          # Numeric x-axis
scale_x_discrete()            # Categorical x-axis
scale_x_log10()               # Log10 transform
scale_x_sqrt()                # Square root transform
scale_x_reverse()             # Reverse axis
scale_x_date()                # Date axis
scale_x_datetime()            # Datetime axis

# Breaks and labels
scale_x_continuous(
  breaks = seq(0, 100, 10),
  labels = scales::comma
)

# Limits and expansion
scale_y_continuous(
  limits = c(0, 100),
  expand = expansion(mult = c(0, 0.05))
)
```

### Theme Reference

Complete Theme Elements:

```r
theme(
  # Text elements
  text = element_text(family = "sans", size = 12),
  plot.title = element_text(face = "bold", size = 16),
  plot.subtitle = element_text(color = "gray40"),
  plot.caption = element_text(hjust = 0, size = 8),
  axis.title = element_text(face = "bold"),
  axis.text = element_text(color = "black"),
  legend.title = element_text(face = "bold"),
  legend.text = element_text(size = 10),
  strip.text = element_text(face = "bold"),

  # Line elements
  axis.line = element_line(color = "black"),
  panel.grid.major = element_line(color = "gray80"),
  panel.grid.minor = element_blank(),

  # Rect elements
  plot.background = element_rect(fill = "white"),
  panel.background = element_rect(fill = "white"),
  legend.background = element_rect(fill = "white"),
  strip.background = element_rect(fill = "gray90"),

  # Positioning
  legend.position = "bottom",
  legend.justification = "center",
  plot.title.position = "plot",
  plot.caption.position = "plot"
)
```

Built-in Themes:

- `theme_gray()` - Default ggplot2 theme
- `theme_bw()` - Black and white theme
- `theme_minimal()` - Minimal theme
- `theme_classic()` - Classic theme (no gridlines)
- `theme_light()` - Light theme
- `theme_dark()` - Dark theme
- `theme_void()` - Empty theme

---

## Shiny Component Reference

### UI Components

Input Widgets:

```r
# Text inputs
textInput(id, label, value = "")
textAreaInput(id, label, value = "")
passwordInput(id, label, value = "")

# Numeric inputs
numericInput(id, label, value, min, max, step)
sliderInput(id, label, min, max, value, step)

# Selection inputs
selectInput(id, label, choices, selected, multiple = FALSE)
selectizeInput(id, label, choices, selected, multiple = FALSE)
radioButtons(id, label, choices, selected)
checkboxInput(id, label, value = FALSE)
checkboxGroupInput(id, label, choices, selected)

# Date/Time inputs
dateInput(id, label, value = Sys.Date())
dateRangeInput(id, label, start, end)

# File inputs
fileInput(id, label, multiple = FALSE, accept = NULL)

# Action buttons
actionButton(id, label, icon = NULL)
downloadButton(id, label)
```

Output Containers:

```r
# Text outputs
textOutput(id)
verbatimTextOutput(id)

# Plot outputs
plotOutput(id, width, height, click, hover, brush)
plotlyOutput(id, width, height)

# Table outputs
tableOutput(id)
dataTableOutput(id)

# UI outputs
uiOutput(id)
htmlOutput(id)

# Download handlers
downloadHandler(filename, content)
```

Layout Functions:

```r
# Basic layouts
fluidPage(...)
fixedPage(...)
fillPage(...)

# Sidebars
sidebarLayout(
  sidebarPanel(...),
  mainPanel(...)
)

# Rows and columns
fluidRow(...)
column(width, ...)

# Panels
wellPanel(...)
conditionalPanel(condition, ...)
tabsetPanel(
  tabPanel(title, ...),
  tabPanel(title, ...)
)

# Cards (bslib)
card(
  card_header(...),
  card_body(...),
  card_footer(...)
)
```

### Server Functions

Reactivity Functions:

```r
# Reactive expressions
reactive({ ... })              # Cached reactive expression
reactiveVal(value)             # Single reactive value
reactiveValues(...)            # Multiple reactive values

# Observers
observe({ ... })               # Run on dependency change
observeEvent(input$x, { ... }) # Run on specific input

# Event reactives
eventReactive(input$x, { ... })

# Reactive utilities
isolate({ ... })               # Read without taking dependency
invalidateLater(millis)        # Schedule invalidation
req(...)                       # Require values before running

# Debounce and throttle
debounce(reactive_expr, millis)
throttle(reactive_expr, millis)

# Reactive polling
reactivePoll(
  intervalMillis,
  session,
  checkFunc,
  valueFunc
)

# File reactive
reactiveFileReader(
  intervalMillis,
  session,
  filePath,
  readFunc
)
```

Update Functions:

```r
updateTextInput(session, id, value, label)
updateNumericInput(session, id, value, label, min, max)
updateSelectInput(session, id, choices, selected, label)
updateSliderInput(session, id, value, label, min, max)
updateCheckboxInput(session, id, value, label)
updateDateInput(session, id, value, label, min, max)
```

---

## Database Integration

### DBI Interface

Connection Management:

```r
library(DBI)

# PostgreSQL
con <- dbConnect(
  RPostgres::Postgres(),
  dbname = "mydb",
  host = "localhost",
  port = 5432,
  user = "user",
  password = "password"
)

# SQLite
con <- dbConnect(RSQLite::SQLite(), "mydb.sqlite")

# Disconnect
dbDisconnect(con)
```

Query Operations:

```r
# Execute query
dbExecute(con, "CREATE TABLE users (id INT, name TEXT)")

# Fetch results
result <- dbGetQuery(con, "SELECT * FROM users")

# Send query without fetching
res <- dbSendQuery(con, "SELECT * FROM large_table")
while (!dbHasCompleted(res)) {
  chunk <- dbFetch(res, n = 1000)
  # Process chunk
}
dbClearResult(res)

# Write table
dbWriteTable(con, "new_table", data, overwrite = TRUE)

# List tables
dbListTables(con)

# Table info
dbListFields(con, "users")
dbExistsTable(con, "users")
```

### dbplyr Integration

Remote Tables:

```r
library(dbplyr)

# Create table reference
users_tbl <- tbl(con, "users")

# dplyr operations (lazy)
result <- users_tbl |>
  filter(age > 18) |>
  select(id, name, age) |>
  arrange(desc(age))

# Show generated SQL
show_query(result)

# Collect results
local_data <- collect(result)

# Compute intermediate table
intermediate <- users_tbl |>
  filter(active == TRUE) |>
  compute(name = "active_users")
```

### Pool for Connection Pooling

```r
library(pool)

# Create pool
pool <- dbPool(
  RPostgres::Postgres(),
  dbname = "mydb",
  host = "localhost",
  port = 5432,
  user = "user",
  password = "password",
  minSize = 2,
  maxSize = 10
)

# Use pool
dbGetQuery(pool, "SELECT * FROM users")

# With Shiny
server <- function(input, output, session) {
  output$table <- renderDataTable({
    dbGetQuery(pool, "SELECT * FROM data WHERE category = ?", input$category)
  })

  # Close pool on session end
  onStop(function() {
    poolClose(pool)
  })
}
```

---

## Testing Reference

### testthat 3.0+ Functions

Test Organization:

```r
test_that("description", {
  # Test code
})

describe("feature", {
  it("does something", {
    # Test code
  })
})

context("module name")  # Legacy, use file names instead
```

Expectations:

```r
# Equality
expect_equal(x, y, tolerance = 1e-10)
expect_identical(x, y)

# Types
expect_type(x, "double")
expect_s3_class(x, "data.frame")
expect_s4_class(x, "Matrix")

# Logical
expect_true(x)
expect_false(x)

# Errors and warnings
expect_error(func(), "error message")
expect_warning(func(), "warning message")
expect_message(func(), "message text")
expect_silent(func())

# Conditions
expect_condition(func(), class = "error")

# Output
expect_output(print(x), "output text")
expect_snapshot(func())
expect_snapshot_value(x, style = "json2")

# Numeric
expect_lt(x, 10)
expect_lte(x, 10)
expect_gt(x, 5)
expect_gte(x, 5)

# Vectors
expect_length(x, 5)
expect_named(x, c("a", "b", "c"))
expect_setequal(x, y)
```

Fixtures and Setup:

```r
# Test fixtures
test_that("example", {
  # Local scope modifications
  withr::local_tempdir()
  withr::local_options(digits = 3)

  # Test code
})

# Setup/teardown files
# tests/testthat/setup.R
# tests/testthat/teardown.R
```

---

## Package Management

### renv Workflow

```r
# Initialize project
renv::init()

# Install packages
renv::install("dplyr")
renv::install("tidyverse@2.0.0")
renv::install("username/repo")  # GitHub

# Update packages
renv::update()
renv::update("dplyr")

# Snapshot dependencies
renv::snapshot()

# Restore dependencies
renv::restore()

# Remove unused packages
renv::clean()

# Check status
renv::status()

# Deactivate renv
renv::deactivate()

# Project settings
renv::settings$snapshot.type("explicit")
renv::settings$use.cache(TRUE)
```

### pak for Fast Installation

```r
library(pak)

# Install packages
pak::pkg_install("dplyr")
pak::pkg_install(c("dplyr", "ggplot2", "tidyr"))

# Install from GitHub
pak::pkg_install("tidyverse/dplyr")

# Install specific version
pak::pkg_install("dplyr@1.1.0")

# System requirements
pak::pkg_sysreqs("dplyr")

# Dependency tree
pak::pkg_deps_tree("shiny")
```

---

## RStudio Configuration

### Project Options

```
# .Rproj file
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: XeLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
```

### .Rprofile Configuration

```r
# .Rprofile
options(
  # General
  repos = c(CRAN = "https://cloud.r-project.org"),
  browserNLdisabled = TRUE,
  deparse.max.lines = 2,

  # Tidyverse
  tidyverse.quiet = TRUE,

  # Data display
  max.print = 100,
  width = 120,

  # Parallelization
  mc.cores = parallel::detectCores(),

  # Memory
  keep.source.pkgs = TRUE
)

# Development packages
if (interactive()) {
  suppressMessages(require(devtools))
  suppressMessages(require(usethis))
  suppressMessages(require(testthat))
}

# Custom functions
.First <- function() {
  message("Welcome to R ", R.version.string)
}

.Last <- function() {
  message("Goodbye!")
}
```

---

## Performance Optimization

### Profiling

```r
# Time execution
system.time({
  result <- expensive_operation()
})

# Benchmark comparison
library(bench)
bench::mark(
  approach1 = method1(),
  approach2 = method2(),
  check = FALSE,
  iterations = 100
)

# Profile code
profvis::profvis({
  # Code to profile
  result <- expensive_function()
})

# Memory profiling
profmem::profmem({
  # Code to profile
})
```

### Optimization Techniques

Vectorization:

```r
# Bad: Loop
result <- numeric(length(x))
for (i in seq_along(x)) {
  result[i] <- x[i] * 2
}

# Good: Vectorized
result <- x * 2

# Pre-allocate vectors
n <- 1e6
result <- numeric(n)
for (i in seq_len(n)) {
  result[i] <- sqrt(i)
}
```

Data.table for Speed:

```r
library(data.table)

# Convert to data.table
dt <- as.data.table(df)

# Fast operations
dt[x > 5, mean(y), by = group]
dt[, .(sum_x = sum(x), mean_y = mean(y)), by = group]

# Reference semantics (in-place modification)
dt[, new_col := x + y]
dt[x > 5, y := y * 2]
```

Parallel Processing:

```r
library(future)
library(furrr)

# Setup
plan(multisession, workers = 4)

# Parallel map
result <- future_map(data, expensive_function)

# Parallel with progress
result <- future_map(data, expensive_function, .progress = TRUE)
```

---

## Context7 Library Mappings

```r
# Tidyverse packages
/tidyverse/dplyr          # Data manipulation
/tidyverse/ggplot2        # Visualization
/tidyverse/tidyr          # Data tidying
/tidyverse/purrr          # Functional programming
/tidyverse/readr          # Data import
/tidyverse/stringr        # String manipulation
/tidyverse/forcats        # Factor handling
/tidyverse/lubridate      # Date/time handling

# Shiny ecosystem
/rstudio/shiny            # Web applications
/rstudio/bslib            # Bootstrap themes
/rstudio/plotly           # Interactive plots

# Testing and development
/r-lib/testthat           # Unit testing
/r-lib/devtools           # Package development
/r-lib/usethis            # Project setup

# Data science
/tidymodels/tidymodels    # Machine learning
/tidymodels/recipes       # Feature engineering
/tidymodels/parsnip       # Model specification

# Database
/r-dbi/DBI                # Database interface
/tidyverse/dbplyr         # Database backend for dplyr

# Package management
/rstudio/renv             # Dependency management
/r-lib/pak                # Fast package installation
```

---

## Common Patterns

### Error Handling

```r
# tryCatch
result <- tryCatch(
  {
    # Code that might error
    risky_operation()
  },
  error = function(e) {
    message("Error occurred: ", e$message)
    return(NULL)
  },
  warning = function(w) {
    message("Warning: ", w$message)
    return(NULL)
  },
  finally = {
    # Cleanup code
    cleanup()
  }
)

# try with silent
result <- try(risky_operation(), silent = TRUE)
if (inherits(result, "try-error")) {
  # Handle error
}

# purrr error handling
safe_op <- safely(risky_function)
result <- safe_op(data)
if (!is.null(result$error)) {
  # Handle error
}
```

### Progress Bars

```r
library(progress)

# Create progress bar
pb <- progress_bar$new(
  format = "Processing [:bar] :percent eta: :eta",
  total = 100,
  width = 60
)

for (i in 1:100) {
  pb$tick()
  Sys.sleep(0.01)
}

# With cli
library(cli)
cli_progress_bar("Processing", total = 100)
for (i in 1:100) {
  cli_progress_update()
  Sys.sleep(0.01)
}
```

### Logging

```r
library(logger)

# Configure logger
log_threshold(INFO)
log_appender(appender_file("app.log"))

# Log messages
log_info("Application started")
log_debug("Debug information: {x}")
log_warn("Warning: {warning_message}")
log_error("Error occurred: {error_message}")

# Structured logging
log_info("User action", user_id = 123, action = "login")
```

---

Last Updated: 2026-01-10
Version: 1.0.0
