## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = FALSE)

## ----eval=FALSE---------------------------------------------------------------
# install.packages("courieR")

## ----eval=FALSE---------------------------------------------------------------
# pak::pkg_install("lennon-li/courieR")

## -----------------------------------------------------------------------------
# library(courieR)
# 
# routes <- find_routes()
# routes
# #>   version                                          rscript_path is_current
# #> 1   4.4.2        C:/Program Files/R/R-4.4.2/bin/x64/Rscript.exe       TRUE
# #> 2   4.3.1  C:/Users/you/AppData/Local/Programs/R/R-4.3.1/bin/...      FALSE
# #> 3   4.1.3            C:/Users/you/Documents/R/R-4.1.3/bin/...      FALSE

## -----------------------------------------------------------------------------
# routes <- find_routes(search_paths = "/opt/custom-r/bin/Rscript")

## -----------------------------------------------------------------------------
# # scan the first (newest) R
# src_pkgs <- manifest(rscript_path = routes$rscript_path[1])
# head(src_pkgs[, c("package", "version", "source")])
# #>     package version source
# #> 1     broom   1.0.7   CRAN
# #> 2    callr   3.7.6   CRAN
# #> 3   courieR   0.2.0 GitHub
# #> 4   ggplot2   3.5.1   CRAN
# #> 5      glue   1.8.0   CRAN
# #> 6   stringr   1.5.1   CRAN

## -----------------------------------------------------------------------------
# my_pkgs <- manifest()
# nrow(my_pkgs)
# #> [1] 312

## -----------------------------------------------------------------------------
# src_pkgs <- manifest(rscript_path = routes$rscript_path[2])  # old R
# tgt_pkgs <- manifest(rscript_path = routes$rscript_path[1])  # new R
# 
# comp <- inventory(src_pkgs, tgt_pkgs)

## -----------------------------------------------------------------------------
# # packages in source but missing from target
# nrow(comp$missing)
# #> [1] 47
# 
# # packages where source has a newer version
# nrow(comp$outdated)
# #> [1] 12
# 
# # packages at the same version in both
# nrow(comp$same)
# #> [1] 201

## -----------------------------------------------------------------------------
# comp$missing[, c("package", "version", "source")]
# #>       package version  source
# #>  1:    bookdown   0.39    CRAN
# #>  2:  brms   2.21.0    CRAN
# #>  3:   officer   0.6.6    CRAN
# #>  ...
# 
# comp$outdated[, c("package", "version.x", "version.y", "source")]
# #>    package version.x version.y source
# #> 1:  ggplot2     3.5.1     3.4.4   CRAN
# #> 2:   tibble     3.2.1     3.2.0   CRAN

## -----------------------------------------------------------------------------
# result <- ship(
#   source_path = routes$rscript_path[2],   # old R — package source
#   target_path = routes$rscript_path[1]    # new R — install destination
# )

## -----------------------------------------------------------------------------
# result <- ship(
#   source_path = routes$rscript_path[2],
#   target_path = routes$rscript_path[1],
#   dry_run = TRUE
# )
# 
# # review the plan before anything is installed
# print(result$plan)
# #>         package action  pak_spec
# #>  1:    bookdown install bookdown
# #>  2:        brms install     brms
# #>  3:     ggplot2 upgrade  ggplot2
# #>  ...

## -----------------------------------------------------------------------------
# result <- ship(
#   source_path = routes$rscript_path[2],
#   target_path = routes$rscript_path[1],
#   upgrade = TRUE
# )

## -----------------------------------------------------------------------------
# # per-package outcomes
# result$results
# #>     package  status                       message
# #>  1: bookdown success                     Installed
# #>  2:     brms success                     Installed
# #>  3:    rJava   error  installation of rJava failed...
# 
# # count by outcome
# table(result$results$status)
# #>   error success
# #>       1      58
# 
# # failed packages only
# result$results[result$results$status == "error", ]

## -----------------------------------------------------------------------------
# # mixed-source example — all handled automatically
# result$plan[, c("package", "source", "pak_spec")]
# #>    package   source        pak_spec
# #> 1:  ggplot2     CRAN         ggplot2
# #> 2:  courieR   GitHub  lennon-li/courieR
# #> 3:    DESeq2  Bioconductor  bioc::DESeq2

## -----------------------------------------------------------------------------
# # explicit mode
# ship(source_path = old_r, target_path = new_r, mode = "offline")

## -----------------------------------------------------------------------------
# library(courieR)
# 
# # dry run first
# migrate("4.5.2", "4.6.0", dry_run = TRUE)
# 
# # for real
# result <- migrate("4.5.2", "4.6.0")
# table(result$results$status)

## -----------------------------------------------------------------------------
# routes  <- find_routes()
# old_r   <- routes$rscript_path[!routes$is_current][1]
# new_r   <- routes$rscript_path[routes$is_current]
# 
# result <- ship(source_path = old_r, target_path = new_r, upgrade = TRUE)

## -----------------------------------------------------------------------------
# r_a <- routes$rscript_path[1]
# r_b <- routes$rscript_path[2]
# 
# # push everything A has into B
# ship(source_path = r_a, target_path = r_b, upgrade = TRUE)
# 
# # then push everything B has into A
# ship(source_path = r_b, target_path = r_a, upgrade = TRUE)

## -----------------------------------------------------------------------------
# src  <- manifest(rscript_path = old_r)
# tgt  <- manifest(rscript_path = new_r)
# comp <- inventory(src, tgt)
# 
# # only migrate CRAN packages — skip GitHub/unknown sources
# cran_missing <- comp$missing[comp$missing$source == "CRAN", ]
# cat(nrow(cran_missing), "CRAN packages to install\n")

## -----------------------------------------------------------------------------
# pkgs <- manifest(rscript_path = routes$rscript_path[1])
# write.csv(pkgs[, c("package", "version", "source")], "my_packages.csv", row.names = FALSE)

## -----------------------------------------------------------------------------
# saved <- read.csv("my_packages.csv", stringsAsFactors = FALSE)
# # pak installs from a character vector of package names
# pak::pkg_install(saved$package)

## -----------------------------------------------------------------------------
# library(courieR)
# hub()

## ----bulk-dispatch-gif, eval=TRUE, echo=FALSE, out.width="100%", fig.cap="Bulk Dispatch: detect R installations, click Compare, review the colour-coded summary chips and package table, then Ship. The log pane on the right shows real-time progress; a live hero panel counts delivered packages during the ship."----
knitr::include_graphics("figures/bulk_dispatch.gif")

## ----custom-dispatch-gif, eval=TRUE, echo=FALSE, out.width="100%", fig.cap="Custom Dispatch: colour-coded filter chips narrow the table to the statuses you care about; check individual packages and press Ship. The Repo column highlights packages with an unknown source in red."----
knitr::include_graphics("figures/custom_dispatch.gif")

