# Advanced R Programming: Write R Like an Expert ----
# Video 6 of R Programming: Zero to Expert, the finale.
# Functions, environments, OOP, speed, testing and a project.

# Quick recap of videos 1 to 5 ----

scores <- c(72, 88, 95, 61)
grade <- function(x) ifelse(x >= 70, "pass", "fail")
grade(scores)
mean(scores)
table(grade(scores))

# Functions are objects ----
# You can store them, pass them around and return them.

square <- function(x) x^2
class(square)
formals(square)
body(square)
square

x <- c(4, 8, 15, 16, 23, 42)
summaries <- list(mean = mean, median = median, sd = sd)
summaries$median(x)
sapply(summaries, function(f) f(x))

apply_twice <- function(f, x) f(f(x))
apply_twice(square, 3)
apply_twice(sqrt, 16)

# Anonymous functions and the lambda shortcut ----

sapply(1:5, function(n) n * 10)
sapply(1:5, \(n) n * 10)
(\(a, b) a + b)(2, 3)

# Closures and function factories ----
# A factory returns a new function that remembers its setup.

power_factory <- function(exp) {
  function(x) x^exp
}
square2 <- power_factory(2)
cube_it <- power_factory(3)
square2(4)
cube_it(4)

cube_it
environment(cube_it)
ls(environment(cube_it))
get("exp", envir = environment(cube_it))

# Lazy evaluation ----
# R only evaluates an argument when it is first used.

lazy_demo <- function(a, b) {
  a * 2
}
lazy_demo(5, stop("this is never evaluated"))

adder_factory <- function(n) {
  function(x) x + n
}
k <- 5
add_k <- adder_factory(k)
k <- 100
add_k(1)

adder_factory2 <- function(n) {
  force(n)
  function(x) x + n
}
k <- 5
add_k2 <- adder_factory2(k)
k <- 100
add_k2(1)

# Pause and try it: a multiplier factory
# Write make_multiplier(m) returning a function that multiplies by m.

make_multiplier <- function(m) {
  force(m)
  \(x) x * m
}
double_it <- make_multiplier(2)
triple_it <- make_multiplier(3)
double_it(1:4)
triple_it(1:4)

# Environments: where R finds things ----
# An environment is a bag of names, plus a link to its parent.

environment()
e <- new.env()
assign("fruit", "mango", envir = e)
e$count <- 3
get("fruit", envir = e)
ls(e)

exists("fruit")
exists("fruit", envir = e)
mget(c("fruit", "count"), envir = e)
parent.env(e)

search()

# Lexical scoping ----
# Functions look up variables where they were DEFINED, not called.

x <- "global x"
show_x <- function() x
use_local <- function() {
  x <- "local x"
  show_x()
}
use_local()

# Remembering state with <<- ----
# <<- assigns in a PARENT environment. Use it with care.

make_counter <- function() {
  count <- 0
  function() {
    count <<- count + 1
    count
  }
}
counter_a <- make_counter()
counter_b <- make_counter()
counter_a()
counter_a()
counter_b()

running <- 0
add_all <- function(v) {
  for (val in v) running <<- running + val
}
add_all(1:4)
running

# Environments are modified in place ----

set_fruit <- function(env) {
  env$fruit <- "papaya"
}
set_fruit(e)
e$fruit

basket <- list(fruit = "mango")
set_fruit_list <- function(lst) {
  lst$fruit <- "papaya"
  lst
}
new_basket <- set_fruit_list(basket)
basket$fruit
new_basket$fruit

# Pause and try it: a bank account closure
# Write make_account(balance) returning deposit() and balance().

make_account <- function(balance = 0) {
  list(
    deposit = function(amount) {
      balance <<- balance + amount
      invisible(balance)
    },
    balance = function() balance
  )
}
acct <- make_account(100)
acct$deposit(50)
acct$balance()

# Functional programming with purrr ----
# purrr gives type-stable, consistent tools for iteration.

library(tidyverse)

nums <- list(
  a = 1:5,
  b = c(10, 20),
  c = c(2.5, 3.5, 4)
)
map(nums, mean)

map_dbl(nums, mean)
map_int(nums, length)
map_chr(nums, \(v) paste("n =", length(v)))
map_lgl(nums, \(v) all(v > 2))

map_dbl(nums, range)

# Two or more inputs: map2 and pmap

names_vec <- c("Amina", "Kofi", "Lena")
ages <- c(31, 45, 28)
map2_chr(names_vec, ages, \(n, a) paste(n, "is", a))

params <- tibble(
  n = c(2, 3, 2),
  mean = c(0, 50, 100),
  sd = c(1, 5, 10)
)
set.seed(6)
pmap(params, \(n, mean, sd) round(rnorm(n, mean, sd), 1))

# Filtering and combining: keep, discard, reduce

nums |> keep(\(v) mean(v) > 3)
nums |> discard(\(v) mean(v) > 3)

reduce(1:5, \(acc, x) acc + x)
accumulate(1:5, \(acc, x) acc + x)

t1 <- tibble(id = 1:3, score = c(5, 6, 7))
t2 <- tibble(id = 1:3, team = c("x", "y", "z"))
t3 <- tibble(id = 1:3, active = c(TRUE, FALSE, TRUE))
reduce(list(t1, t2, t3), left_join, by = "id")

# When things fail: safely and possibly

inputs <- list(10, "oops", 4)
map(inputs, log)

safe_log <- safely(log)
results <- map(inputs, safe_log)
results[[2]]
results |> map("error") |> map_lgl(is.null)

possible_log <- possibly(log, otherwise = NA_real_)
map_dbl(inputs, possible_log)

# Pause and try it: count words
# Count the words in each sentence with strsplit() and map_int().

sentences <- c("R is fun", "purrr makes loops tidy", "hi")
sentences |>
  strsplit(" ") |>
  map_int(length)

# Conditions: errors, warnings and messages ----
# R has three main conditions: error, warning and message.

check_age <- function(age) {
  if (!is.numeric(age)) stop("age must be a number")
  if (age < 0) warning("negative age, using 0")
  message("Checking age ", age)
  max(age, 0)
}
check_age(30)

check_age(-5)

check_age("ten")

# Handling conditions with tryCatch

result <- tryCatch(
  check_age("ten"),
  error = function(e) {
    message("Caught: ", conditionMessage(e))
    NA
  }
)
result

tryCatch(
  check_age(-5),
  warning = function(w) paste("Handled:", conditionMessage(w)),
  finally = message("Cleanup always runs")
)

# withCallingHandlers: handle, then carry on

withCallingHandlers(
  check_age(-5),
  warning = function(w) {
    message("Logged: ", conditionMessage(w))
    invokeRestart("muffleWarning")
  }
)

tidy_up <- function() {
  on.exit(message("on.exit: tidying up"))
  stop("something broke")
}
res <- try(tidy_up(), silent = TRUE)
class(res)

# Custom condition classes

validate_price <- function(p) {
  if (p < 0) {
    stop(errorCondition(
      paste("price cannot be negative:", p),
      class = "negative_price",
      data = list(price = p)
    ))
  }
  p
}
tryCatch(validate_price(-1), error = function(e) class(e))

tryCatch(
  validate_price(-3),
  negative_price = function(e) {
    message("Fixing negative price ", e$data$price)
    abs(e$data$price)
  },
  error = function(e) NA
)

# Defensive programming ----
# Check inputs at the top: stopifnot(), match.arg(), missing().

summarise_scores <- function(x,
                             method = c("mean", "median",
                                        "trimmed")) {
  stopifnot("x must be numeric" = is.numeric(x),
            "x must not be empty" = length(x) > 0)
  method <- match.arg(method)
  switch(method,
    mean = mean(x),
    median = median(x),
    trimmed = mean(x, trim = 0.1)
  )
}
summarise_scores(c(50, 60, 70, 100))
summarise_scores(c(50, 60, 70, 100), "med")

summarise_scores(c(50, 60), "mode")

summarise_scores("ninety")

describe_vec <- function(x, label) {
  if (missing(label)) label <- deparse(substitute(x))
  cat(label, "has", length(x), "values\n")
}
describe_vec(mtcars$mpg)
describe_vec(1:10, "my numbers")

# Pause and try it: a safe divide
# Write safe_divide(a, b): error if not numeric, warn if b is 0.

safe_divide <- function(a, b) {
  stopifnot(is.numeric(a), is.numeric(b))
  if (b == 0) {
    warning("division by zero, returning NA")
    return(NA_real_)
  }
  a / b
}
safe_divide(10, 4)
tryCatch(safe_divide(1, 0), warning = \(w) -1)

# Object-oriented R: S3 ----
# S3 = a class attribute + generic functions that dispatch on it.

f <- factor(c("low", "high", "low"))
typeof(f)
class(f)
unclass(f)

print

# Constructor, validator and helper
# Example: a temperature vector that knows its unit.

new_temp <- function(x = double(), unit = "C") {
  stopifnot(is.double(x), is.character(unit))
  structure(x, class = "temperature", unit = unit)
}

validate_temp <- function(t) {
  unit <- attr(t, "unit")
  if (!unit %in% c("C", "F")) stop("unit must be C or F")
  limit <- if (unit == "C") -273.15 else -459.67
  if (any(unclass(t) < limit)) stop("below absolute zero")
  t
}

temperature <- function(x, unit = "C") {
  validate_temp(new_temp(as.double(x), unit))
}
t1 <- temperature(c(21.5, 30, 18))
t1

temperature(-300)

# Methods: format, print and summary

format.temperature <- function(x, ...) {
  paste(formatC(unclass(x), format = "f", digits = 1),
        attr(x, "unit"))
}
print.temperature <- function(x, ...) {
  cat("<temperature:", length(x), "readings>\n")
  print(format(x), quote = FALSE)
  invisible(x)
}
t1

summary.temperature <- function(object, ...) {
  v <- unclass(object)
  u <- attr(object, "unit")
  cat("Range:", min(v), "to", max(v), u, "\n")
  cat("Mean: ", round(mean(v), 2), u, "\n")
  invisible(object)
}
summary(t1)

t1[2:3]

`[.temperature` <- function(x, i) {
  new_temp(unclass(x)[i], attr(x, "unit"))
}
t1[2:3]

# Your own generic function

to_fahrenheit <- function(x, ...) UseMethod("to_fahrenheit")
to_fahrenheit.temperature <- function(x, ...) {
  if (attr(x, "unit") == "F") return(x)
  temperature(unclass(x) * 9 / 5 + 32, unit = "F")
}
to_fahrenheit.default <- function(x, ...) {
  stop("can't convert an object of class ", class(x)[1])
}
to_fahrenheit(t1)

# Inheritance with NextMethod()

body_temp <- function(x) {
  t <- temperature(x, "C")
  class(t) <- c("body_temp", class(t))
  t
}
print.body_temp <- function(x, ...) {
  NextMethod()
  cat("Readings with fever:", sum(unclass(x) >= 38), "\n")
  invisible(x)
}
patient <- body_temp(c(36.8, 38.4, 37.1, 39))
patient
inherits(patient, "temperature")

# S4 in brief ----
# Formal classes with typed slots, validity checks and setMethod().

setClass("Account",
  slots = c(owner = "character", balance = "numeric"),
  validity = function(object) {
    if (object@balance < 0) "balance cannot be negative" else TRUE
  }
)
savings <- new("Account", owner = "Amina", balance = 250)
savings@balance

new("Account", owner = "Ben", balance = -5)

setGeneric("deposit", function(acc, amount) {
  standardGeneric("deposit")
})
setMethod("deposit", "Account", function(acc, amount) {
  acc@balance <- acc@balance + amount
  validObject(acc)
  acc
})
setMethod("show", "Account", function(object) {
  cat("Account of", object@owner, "| balance:", object@balance, "\n")
})
savings <- deposit(savings, 100)
savings

# Copy-on-modify ----
# Names point to values. R copies a value only when you modify it.

a <- c(1, 2, 3)
b <- a
b[1] <- 100
a
b

double_first <- function(v) {
  v[1] <- v[1] * 2
  v
}
out <- double_first(a)
a
out

e1 <- new.env()
e1$value <- 1
e2 <- e1
e2$value <- 999
e1$value

# R6: classes with reference semantics ----
# Mutable objects with fields and methods, like Python or Java.

library(R6)

BankAccount <- R6Class("BankAccount",
  public = list(
    owner = NULL,
    balance = 0,
    initialize = function(owner, balance = 0) {
      self$owner <- owner
      self$balance <- balance
    },
    deposit = function(amount) {
      self$balance <- self$balance + amount
      invisible(self)
    },
    withdraw = function(amount) {
      if (amount > self$balance) stop("insufficient funds")
      self$balance <- self$balance - amount
      invisible(self)
    },
    print = function(...) {
      cat("<BankAccount>", self$owner, "|", self$balance, "\n")
      invisible(self)
    }
  )
)

acct1 <- BankAccount$new("Amina", 100)
acct1$deposit(50)$withdraw(30)
acct1

acct2 <- acct1
acct2$deposit(1000)
acct1$balance

acct3 <- acct1$clone()
acct3$deposit(5)
c(acct1$balance, acct3$balance)

# Private fields and active bindings

Thermostat <- R6Class("Thermostat",
  private = list(.celsius = 20),
  active = list(
    celsius = function(value) {
      if (missing(value)) return(private$.celsius)
      stopifnot(is.numeric(value), value >= 5, value <= 35)
      private$.celsius <- value
    },
    fahrenheit = function() private$.celsius * 9 / 5 + 32
  )
)

th <- Thermostat$new()
th$celsius
th$fahrenheit
th$celsius <- 25
th$fahrenheit

# Inheritance in R6

SavingsAccount <- R6Class("SavingsAccount",
  inherit = BankAccount,
  public = list(
    rate = 0.05,
    add_interest = function() {
      self$deposit(self$balance * self$rate)
    },
    print = function(...) {
      super$print()
      cat("  interest rate:", self$rate, "\n")
      invisible(self)
    }
  )
)
sav <- SavingsAccount$new("Kofi", 1000)
sav$add_interest()
sav
class(sav)

# Performance: writing fast R ----
# Measure with system.time(). Vectorise. Preallocate. Never grow.

set.seed(1)
x <- runif(1e6)
loop_version <- function(x) {
  out <- numeric(length(x))
  for (i in seq_along(x)) {
    out[i] <- if (x[i] > 0.5) x[i]^2 else 0
  }
  out
}
system.time(r1 <- loop_version(x))

system.time(r2 <- ifelse(x > 0.5, x^2, 0))
system.time(r3 <- (x > 0.5) * x^2)
identical(r1, r2)
identical(r1, r3)

# Never grow an object inside a loop

grow <- function(n) {
  out <- c()
  for (i in 1:n) out <- c(out, i^2)
  out
}
prealloc <- function(n) {
  out <- numeric(n)
  for (i in 1:n) out[i] <- i^2
  out
}
system.time(grow(3e4))
system.time(prealloc(3e4))

pieces <- lapply(1:2000, \(i) data.frame(id = i, value = i * 2))
system.time({
  slow_df <- data.frame()
  for (p in pieces) slow_df <- rbind(slow_df, p)
})
system.time(fast_df <- do.call(rbind, pieces))
system.time(tidy_df <- bind_rows(pieces))
nrow(fast_df)

Reduce(`+`, 1:5, accumulate = TRUE)

# data.table: speed for big data ----
# Syntax: DT[i, j, by] = where, select or compute, grouped by.

library(data.table)

set.seed(2024)
n <- 1e6
DT <- data.table(
  id = 1:n,
  region = sample(c("North", "South", "East", "West"), n, TRUE),
  product = sample(LETTERS[1:5], n, replace = TRUE),
  units = rpois(n, 3),
  price = round(runif(n, 5, 50), 2)
)
dim(DT)
head(DT, 3)

DT[region == "North" & units > 5, .N]
DT[, .(revenue = sum(units * price)), by = region]

DT[, revenue := units * price]
DT[order(-revenue)][1:3, .(id, region, revenue)]

system.time(
  DT[, .(total = sum(revenue)), by = .(region, product)]
)
tb <- as_tibble(DT)
system.time(
  tb |>
    group_by(region, product) |>
    summarise(total = sum(revenue), .groups = "drop")
)

setkey(DT, region)
key(DT)
DT["East", .(orders = .N, avg_units = mean(units))]

DT_alias <- DT
DT_alias[, flag := TRUE]
"flag" %in% names(DT)

DT[, flag := NULL]
DT_copy <- copy(DT)
DT_copy[, flag := TRUE]
"flag" %in% names(DT)

fwrite(DT, "sales_big.csv")
system.time(DT2 <- fread("sales_big.csv"))
dim(DT2)
round(file.size("sales_big.csv") / 1e6, 1)

# Pause and try it: data.table practice
# Total units per product in the West region, sorted high to low.

DT[region == "West",
   .(units = sum(units)),
   by = product][order(-units)]

rm(DT, DT2, DT_alias, DT_copy, tb)
invisible(file.remove("sales_big.csv"))

# Tidy evaluation: your own tidyverse functions ----
# dplyr uses column names as if they were variables.

group_mean_wrong <- function(df, group, value) {
  df |>
    group_by(group) |>
    summarise(mean = mean(value))
}
group_mean_wrong(mpg, class, hwy)

group_mean <- function(df, group, value) {
  df |>
    group_by({{ group }}) |>
    summarise(
      n = n(),
      mean = mean({{ value }}, na.rm = TRUE),
      .groups = "drop"
    )
}
group_mean(mpg, class, hwy)

# Column names as strings: .data[[ ]]

col_range <- function(df, var) {
  df |>
    summarise(
      min = min(.data[[var]]),
      max = max(.data[[var]])
    )
}
c("hwy", "cty", "displ") |>
  set_names() |>
  map(\(v) col_range(mpg, v)) |>
  list_rbind(names_to = "variable")

# across() and ... inside functions

summarise_many <- function(df, group, cols) {
  df |>
    group_by({{ group }}) |>
    summarise(
      across({{ cols }}, \(x) round(mean(x), 1)),
      .groups = "drop"
    )
}
summarise_many(mpg, drv, c(hwy, cty, displ))

top_counts <- function(df, ..., n = 5) {
  df |>
    count(..., sort = TRUE) |>
    head(n)
}
top_counts(mpg, manufacturer)
top_counts(mpg, manufacturer, drv, n = 3)

# Pause and try it: top rows by a column
# Write top_by(df, var, n = 3) using slice_max().

top_by <- function(df, var, n = 3) {
  df |>
    slice_max({{ var }}, n = n, with_ties = FALSE)
}
top_by(mpg, hwy) |> select(manufacturer, model, hwy)

# Unit testing with testthat ----

library(testthat)

test_that("adder factory adds correctly", {
  add_two <- adder_factory2(2)
  expect_equal(add_two(3), 5)
  expect_equal(add_two(-2), 0)
  expect_equal(add_two(1:3), 3:5)
})

test_that("temperature class behaves", {
  expect_s3_class(temperature(20), "temperature")
  expect_error(temperature(-500), "absolute zero")
  expect_equal(length(t1[1:2]), 2)
  expect_error(validate_price(-1), class = "negative_price")
})

test_that("floating point is exact", {
  expect_identical(0.1 + 0.2, 0.3)
})

test_that("floating point is close enough", {
  expect_equal(0.1 + 0.2, 0.3)
})

# Recursion and memoisation ----

fact <- function(n) {
  if (n <= 1) return(1)
  n * fact(n - 1)
}
fact(5)
fact(20)

fib <- function(n) {
  if (n <= 2) return(1)
  fib(n - 1) + fib(n - 2)
}
fib(10)
system.time(fib(25))

make_memo_fib <- function() {
  memo <- c(1, 1)
  fib_m <- function(n) {
    if (n <= length(memo) && !is.na(memo[n])) return(memo[n])
    val <- fib_m(n - 1) + fib_m(n - 2)
    memo[n] <<- val
    val
  }
  fib_m
}
fib_fast <- make_memo_fib()
system.time(fib_fast(25))
fib_fast(80)
length(environment(fib_fast)$memo)

# Project: a reusable sales analytics toolkit ----
# S3 class + validated constructor + methods + tests + tidy eval.

set.seed(99)
n_sales <- 300
list_price <- c(Laptop = 900, Phone = 600, Tablet = 350)
raw_sales <- tibble(
  date = as.Date("2026-01-01") + sample(0:179, n_sales, TRUE),
  region = sample(c("North", "South", "East", "West"),
                  n_sales, replace = TRUE),
  product = sample(names(list_price), n_sales, replace = TRUE),
  units = sample(1:10, n_sales, replace = TRUE),
  price = round(list_price[product] * runif(n_sales, 0.9, 1.1))
)
glimpse(raw_sales)

# Step 1: constructor, validator and helper

new_sales <- function(df, currency) {
  stopifnot(is.data.frame(df), is.character(currency))
  structure(list(data = df, currency = currency),
            class = "sales_data")
}
validate_sales <- function(x) {
  needed <- c("date", "region", "product", "units", "price")
  absent <- setdiff(needed, names(x$data))
  if (length(absent) > 0) {
    stop("missing columns: ", paste(absent, collapse = ", "))
  }
  if (any(x$data$units < 0)) stop("units cannot be negative")
  x
}

sales_data <- function(df, currency = "USD") {
  obj <- validate_sales(new_sales(as_tibble(df), currency))
  obj$data <- mutate(obj$data, revenue = units * price)
  obj
}
sales <- sales_data(raw_sales)
class(sales)

sales_data(select(raw_sales, date, region))

# Step 2: print and summary methods

print.sales_data <- function(x, ...) {
  d <- x$data
  cat("<sales_data>", nrow(d), "transactions\n")
  cat("Period: ", format(min(d$date)), "to",
      format(max(d$date)), "\n")
  regions <- paste(sort(unique(d$region)), collapse = ", ")
  cat("Regions:", regions, "\n")
  total <- format(round(sum(d$revenue)), big.mark = ",")
  cat("Revenue:", x$currency, total, "\n")
  invisible(x)
}
sales

summarise_sales <- function(sales, ...) {
  sales$data |>
    group_by(...) |>
    summarise(
      orders = n(),
      units = sum(units),
      revenue = round(sum(revenue)),
      .groups = "drop"
    ) |>
    arrange(desc(revenue))
}
summarise_sales(sales, product)

summary.sales_data <- function(object, by = region, ...) {
  summarise_sales(object, {{ by }})
}
summary(sales)
summary(sales, by = product)

# Step 3: a plot method

plot.sales_data <- function(x, ...) {
  monthly <- x$data |>
    mutate(month = floor_date(date, "month")) |>
    group_by(month, product) |>
    summarise(revenue = sum(revenue), .groups = "drop")
  p <- ggplot(monthly, aes(month, revenue, colour = product)) +
    geom_line(linewidth = 1) +
    geom_point(size = 2) +
    scale_y_continuous(labels = scales::comma) +
    labs(title = "Monthly revenue by product",
         x = NULL, y = paste("Revenue,", x$currency)) +
    theme_minimal(base_size = 14)
  print(p)
  invisible(p)
}
plot(sales)

# Step 4: tested helpers

growth_rate <- function(old, new) {
  stopifnot(is.numeric(old), is.numeric(new))
  if (any(old == 0, na.rm = TRUE)) stop("old values must be non-zero")
  (new - old) / old
}
test_that("growth_rate works", {
  expect_equal(growth_rate(100, 110), 0.1)
  expect_equal(growth_rate(c(50, 200), c(25, 300)), c(-0.5, 0.5))
  expect_error(growth_rate(0, 5), "non-zero")
})

test_that("sales_data is built and validated", {
  expect_s3_class(sales, "sales_data")
  expect_true("revenue" %in% names(sales$data))
  expect_equal(nrow(summary(sales)), 4)
  expect_error(sales_data(raw_sales[, 1:3]), "missing columns")
})

monthly_growth <- function(sales) {
  sales$data |>
    mutate(month = floor_date(date, "month")) |>
    summarise(revenue = sum(revenue), .by = month) |>
    arrange(month) |>
    mutate(growth = round(100 * growth_rate(dplyr::lag(revenue),
                                            revenue), 1))
}
monthly_growth(sales)

batches <- list(good = raw_sales, broken = raw_sales[, 1:2])
built <- map(batches, safely(sales_data))
map_lgl(built, \(r) is.null(r$error))
built$broken$error$message

# Recap: what you can do now ----
# Functions, closures, environments, purrr, conditions, S3/S4/R6,
# copy-on-modify, fast code, data.table, tidy eval, tests, recursion.
# What next: Shiny, Quarto, package development, tidymodels.