Skip to content

Instantly share code, notes, and snippets.

View jmbarbone's full-sized avatar

Jordan Mark Barbone jmbarbone

View GitHub Profile
@jmbarbone
jmbarbone / fibonocci.md
Created October 9, 2025 04:20
'fib'-o-nocci; or
.fibs <- new.env()

.fib <- function(n) {
  name <- as.character(n)
  get0(name, envir = .fibs) %||% assign(name, fibonocci(n), envir = .fibs)
}

fibonocci <- function(n = 0) {
 n &lt;- floor(n)
@jmbarbone
jmbarbone / another-version.md
Last active October 18, 2025 23:31
typed functions in R
arg <- function(sym, type, default) {
  sym <- substitute(sym)
  spec <- structure(
    list(
      sym = as.character(sym),
      type = type
    ),
    class = "arg"
  )
@jmbarbone
jmbarbone / iterators.md
Created October 2, 2025 05:55
just some iterations in R
enumerate <- function(x, i = "i", v = "v") {
  lapply(
    seq_along(x),
    function(iteration) {
      structure(
        list(iteration, x[[iteration]]),
        class = c("enumeration", "list"),
        names = c(i, v)
      )
@jmbarbone
jmbarbone / global-calling-handlers.R
Created October 1, 2025 01:06
R environment/class for handling handlers
global_calling_handlers <- local({
. <- environment()
class(.) <- c("cnd:global_calling_handlers", "environment")
.handlers <- NULL
handlers <- NULL
add <- function(...) {
"Adds calling handlers
If handlers with the same name already exist, they are replaced.
pkg_version <- function(package, keep = c("all", "patch", "minor", "major")) {
keep <- match.arg(keep)
version <- utils::packageVersion(package)
version <- unclass(version)[[1L]]
version <- switch(
keep,
all = version,
patch = version[1:3],
minor = version[1:2],
major = version[1L]
@jmbarbone
jmbarbone / pak-install-pkgs.R
Last active March 5, 2025 17:32
custom wrappers for installing R
pak_install_pkgs <- function(
pkg,
new_lib = old_lib,
old_lib = .libPaths()[1L],
upgrade = FALSE,
update = TRUE,
dependencies = NA,
force = FALSE,
use_temp = FALSE,
pak_lib = NULL
@jmbarbone
jmbarbone / typed-function.md
Created October 5, 2024 01:38
playing around with some type setting for R functions
library(S7)
typed <- function(fun, ...) {
  ..params <- list(...)
  ..syms <- names(..params)
  stopifnot(..syms %in% names(formals(fun)))
  
  # should make this a function
  ..validator <- S7::new_class("..validator", properties = ..params)
@jmbarbone
jmbarbone / plotly-move-legend-nope-doesnt-work.R
Created July 23, 2024 21:15
I just want to move the legend to the cursor position....
library(plotly)
library(htmlwidgets)
x_unified_y_cursor <- "
// hovermode is 'x unified' but we don't want the default y/vertical
// positioning of the hover label. We want the hoverlabel to appear at the
// current cursor position (i.e. the y position of the cursor)
function(el) {
el.on('plotly_hover', function(data) {
console.log('event data: ', data);
@jmbarbone
jmbarbone / progressr-examples.R
Created June 24, 2024 16:06
examples of using the {progressr} package
library(progressr)
library(furrr)
handlers("void")
handlers(list(
handler_progress(
format = ":spin :current/:total (:message) [:bar] :percent in :elapsed ETA: :eta",
width = getOption("width"),
complete = "="
@jmbarbone
jmbarbone / sym-to-data.R
Last active June 20, 2024 16:27
Replaces `!!rlang::sym("...")` with `.data$...`
invisible(lapply(
list.files(
path = ".",
pattern = "\\.(r|rmd|qmd)$",
full.names = TRUE,
recursive = TRUE,
ignore.case = TRUE
),
function(path) {
pat <- "!!rlang::sym[(]\"([a-zA-z0-9_]+)\"[)]"