Skip to content

Instantly share code, notes, and snippets.

View jmbarbone's full-sized avatar

Jordan Mark Barbone jmbarbone

View GitHub Profile
@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_]+)\"[)]"
@jmbarbone
jmbarbone / lambda-functions-2.R
Last active June 3, 2024 03:23
base 'map()`-like functions
lambda <- function(expr, args = ".i") {
fun <- function() { }
formals(fun) <- structure(
rep(list(substitute()), length(args)),
class = "alist",
names = args
)
if (is.function(expr)) {
@jmbarbone
jmbarbone / databricks-env.R
Last active July 4, 2024 14:38
{databricks} but it's an R6 object
databricks <- local(envir = new.env(), {
.self <- environment()
.client <- NULL
.call <- function(method, args) {
if (is.null(.client)) {
stop(
"No client conigured",
"\nplease use $configure() function to set client",
call. = FALSE
@jmbarbone
jmbarbone / example.R
Created April 26, 2024 16:30
vline for plotly
x <- runif(1000)
plotly::plot_ly(
x = x,
type = "histogram",
) |>
plotly::layout(
bargap = 0.05,
shapes = list(
vline(mean(x), "mint", "mean"),
@jmbarbone
jmbarbone / git-arbor.sh
Last active August 31, 2024 17:48
clean up merged branches
# I usually alias this as `git arbor`
#
# I should also probably add https://stackoverflow.com/a/28502292 for the git-checkout one
git fetch -a -p && git branch --merged | grep -Ev \"(^\\*|master|main|dev)\" | xargs git branch -d
@jmbarbone
jmbarbone / enclose.R
Created March 6, 2024 03:16
evaluate an R6 object within its enclosed environment
#' Evaluate an R6 object within its enclosed environment
#'
#' @param x An R6 object
#' @param expr An expression to run
#' @export
#' @examples
#' Foo <- R6::R6Class(
#' "Foo",
#' public = list(
#' hello = function() cat("hello\n")
@jmbarbone
jmbarbone / sql-snakecase.R
Created February 14, 2024 05:58
R SQL snakecase implementation
# nolint start: object_usage_linter.
sql_snakecase0 <- function(.data, new, old = new, na = "(missing)") {
force(old)
na <- as.character(na)
dplyr::mutate(
.data,
!!rlang::sym(new) := tolower(!!rlang::sym(old)),
!!rlang::sym(new) := REGEXP_REPLACE(!!rlang::sym(new), "\\%", "percent "),
!!rlang::sym(new) := REGEXP_REPLACE(!!rlang::sym(new), "\\#", "n "),