Skip to content

Instantly share code, notes, and snippets.

View jonocarroll's full-sized avatar
👨‍💻
Learning all the languages

Jonathan Carroll jonocarroll

👨‍💻
Learning all the languages
View GitHub Profile
@jonocarroll
jonocarroll / copy_on_modify.R
Last active August 29, 2024 20:09
Understanding R's copy-on-modify semantics wrt the symbol table
## Modify a vector in the workspace; x is a user-accessible symbol
x <- 42
.Internal(inspect(x))
# @5631a3a19e20 14 REALSXP g0c1 [REF(5)] (len=1, tl=0) 42
x[1] <- 43 # modification causes a copy (address changes)
.Internal(inspect(x))
# @5631a36c1cb8 14 REALSXP g0c1 [REF(4)] (len=1, tl=0) 43
## Modify a vector inside a function; user cannot access y
f <- function() {
@jonocarroll
jonocarroll / Change.hs
Created July 31, 2023 10:26
WIP Haskell solution to Exercism problem 'change'
module Change (findFewestCoins) where
import Data.List (find)
import Data.List (minimumBy)
import Data.Function (on)
import Debug.Trace
smallestLengthList :: [[Integer]] -> [Integer]
smallestLengthList = minimumBy (compare `on` length)
@jonocarroll
jonocarroll / sharepoint_filetree.R
Last active November 13, 2023 23:04
Microsoft365 Sharepoint File Tree Capture and View
library(Microsoft365R)
library(jsTree) # for viewing results
sp_url <- "https://org.sharepoint.com/sites/SITENAME/"
# sharepoint site
site <- get_sharepoint_site(site_url = sp_url)
# default document library
drv <- site$get_drive()
@jonocarroll
jonocarroll / expand_collapse_datatable.R
Last active May 4, 2023 22:40
Expand/Collapse buttons plus rowcounts and totals for rowGroups in DT::datatable
library(shiny)
ui <- fluidPage(
titlePanel("Expand/Collapse DataTable"),
mainPanel(
fluidRow(
column(6, DT::dataTableOutput("tbl")),
column(6, DT::dataTableOutput("tbl2"))
)
)
@jonocarroll
jonocarroll / dbg.R
Created November 6, 2022 05:45
Debug function for R based on Rust's dbg! macro
dbg <- function(x) {
ex <- rlang::f_text(rlang::enquos(x)[[1]])
ret <- rlang::eval_bare(x)
message(glue::glue("DEBUG: {ex} = {ret}"))
ret
}
a <- 1
b <- 3
x <- dbg(a + b)
@jonocarroll
jonocarroll / aukbus.R
Created November 3, 2022 06:01
Auckland Bus Cancellations plot
# https://notstatschat.rbind.io/2022/11/03/improving-a-graph/
d <- read.table(
"https://gist.githubusercontent.com/tslumley/9ac8df14309ecc5936183de84b57c987/raw/9ebf665b2ff9a93c1dbc73caf5ff346909899827/busdata.txt",
header = TRUE
)
d$date <- as.Date(paste(2022, d$mo, d$d, sep = "-"))
d$weekend <- with(d, weekdays(d$date) %in% c("Saturday", "Sunday"))
d$workday <- with(d, !(weekdays(date) %in% c("Saturday", "Sunday")))
@jonocarroll
jonocarroll / sort_polyglot.R
Created October 12, 2022 00:20
string sorting in R
splt <- function(s) {
x <- strsplit(s, "-")
s[order(sapply(x, `[[`, 1), as.integer(sapply(x, `[[`, 2)))]
}
splt_radix <- function(s) {
x <- strsplit(s, "-")
s[order(sapply(x, `[[`, 1), as.integer(sapply(x, `[[`, 2)), method = "radix")]
}
@jonocarroll
jonocarroll / lissajous.jl
Created May 12, 2022 12:32
Lissajous curve matrix in Julia
## Goal: reproduce e.g. https://www.reddit.com/r/oddlysatisfying/comments/uc054a/lissajous_polygons
using Plots
import GeometryBasics: Point
## https://jcarroll.xyz/2022/04/07/interpolation-animation-in.html
interpolate(a, b) = t -> ((1.0 - t) * a + t * b)
## define the vertices of an N-gon
@jonocarroll
jonocarroll / starts_with_ordered.R
Created May 6, 2022 02:03
{tidyselect} columns with alphabetical ordering
starts_with_ordered <- function(match, decreasing = FALSE) {
all_vars <- tidyselect::peek_vars()
matches <- tidyselect::starts_with(match)
named_matches <- setNames(matches, all_vars[matches])
named_matches[order(names(named_matches), decreasing = decreasing)]
}
mtcars |>
dplyr::select(starts_with_ordered("c", decreasing = FALSE)) |>
head()
@jonocarroll
jonocarroll / subset_SummarizedExperiment.R
Created May 2, 2022 08:37
Quick hack to enable some assay-level metadata filtering on SummarizedExperiment
library(SummarizedExperiment)
## create a SummarizedExperiment
se <- SummarizedExperiment(assays =
list(
counts = matrix(0:99, 10, 10),
cpm = matrix(200:299, 10, 10)
)
)
## add tabular metadata with assay-specific properties