Skip to content

Instantly share code, notes, and snippets.

View jmbarbone's full-sized avatar

Jordan Mark Barbone jmbarbone

View GitHub Profile
@jmbarbone
jmbarbone / cli-args.R
Created January 10, 2022 23:55
format cli args
cli_args <- function(...) {
x <- list(...)
nm <- names(x)
stopifnot(all(nm != ""))
single <- nchar(nm) == 1L
nm[single] <- paste0("-", nm[single])
nm[!single] <- paste0("--", gsub("[[:punct:][:space:]]", "-", nm[!single]))
paste(nm, x, collapse = " ")
}
@jmbarbone
jmbarbone / bigs-and-smalls.R
Last active February 8, 2022 22:05
4/5 felines
numbers <- function(big = 2L, small = 6L - big) {
stopifnot(big + small == 6L)
c(sample(50:100, big), sample(0:49, small))
}
problem <- function(x = numbers(), parenthesis = TRUE, ns = length(x)) {
stopifnot(all(ns > 1), all(ns <= length(x)))
x <- as.integer(x)
@jmbarbone
jmbarbone / snippets.R
Last active March 17, 2022 16:58
update snippets
# You'll need to change {pkg} here for your package because I'm too lazy to generalize this right now
# I'll probably put this in jmbarbone/mark or jmbarbone/markExtra
#' Add code snippets
#'
#' Adds code snippets
#'
#' @export
add_pkg_snippets <- function() {
@jmbarbone
jmbarbone / filter.R
Last active June 21, 2022 15:51
filtering
# vector filtering --------------------------------------------------------
# Modeled from `base::Filter()` but less generalized (therefore more efficient).
# These functions also use a vector as the first argument rather than the
# filtering function.
base::Filter
#> function (f, x)
#> {
#> f <- match.fun(f)
@jmbarbone
jmbarbone / ranks-and-stuff.R
Last active April 15, 2022 15:17
ranking stuff
rank_limited <- function(x, method = c("auto", "shell", "quick", "radix")) {
u <- sort(unique(x), method = method)
match(x, u)
}
rank_limited2 <- function(x, method = c("auto", "shell", "quick", "radix")) {
u <- unique(sort(x, method = method))
match(x, u)
}
# original ----------------------------------------------------------------
new <- list() # construct as list -- data.frames are fancy lists
cols <- c(1, 5, 3) # use a vector of column indices
for (i in seq_along(cols)) {
# append the list at each column
new[[i]] <- mtcars[, cols[i], drop = FALSE]
}
new <- as.data.frame(new) # make list into data.frame
@jmbarbone
jmbarbone / prepare-commit-msg-github
Last active December 2, 2022 19:38
Prepend all commits with number of JIRA-like issue (from branch)
#! /usr/bin/env sh
# Based on https://serebrov.github.io/html/2019-06-16-git-hook-to-add-issue-number-to-commit-message.html
#
# This hook works for branches named such as "123-description" and will add "#123 " to the commit message.
#
# 123-description >> #123
#
# Example:
#
# ```
@jmbarbone
jmbarbone / jira-table-paste.R
Last active June 14, 2022 19:34
paste data.frame as a Jira table
``` r
jira_table_paste <- function(data, quiet = FALSE) {
stopifnot(is.data.frame(data), all(!sapply(data, is.list)))
data[] <- lapply(data, function(i) paste0("{{", i, "}}"))
data <- rbind(matrix(colnames(data), nrow = 1L), as.matrix(data))
data[] <- apply(data, 2L, function(i) format(paste0(" ", i, " ")))
res <- c(
# the headers come out as a little annoying
paste0("||", paste0(data[1L, ], collapse = "||"), "||"),
paste0("|", apply(data[-1L, ], 1L, paste0, collapse = "|"), "|")
@jmbarbone
jmbarbone / quarto-render2.R
Last active July 12, 2022 19:46
alternative quarto rendering function
#' Render quarto files
#'
#' Render quarto files
#'
#' @details
#' The `.qmd` input file is copied as a temporary file, which is then used for
#' rendering. The output of this is then copied over to the intended output
#' file.
#'
@jmbarbone
jmbarbone / detach-recursive.R
Last active August 3, 2022 17:09
Recursive detach
detach_recursive <- function(name, ...) {
stopifnot(!missing(name), is.character(name))
params <- list(...)
params$name <- name
params$character.only <- TRUE
repeat {
if (inherits(try(do.call(detach, params), silent = TRUE), "try-error")) {
break
}