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 / 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
@jonocarroll
jonocarroll / spline.jl
Last active April 7, 2022 11:47
Julia interpolation animation
## Inspired by
## https://twitter.com/ted_dunning/status/1435027697386721280?s=20&t=cDVb0XOQRJeOjXoTrOz54w
using Plots
## These are required to be imported to write methods for them
import Base:*
import Base:+
@jonocarroll
jonocarroll / validate_non_reactives.R
Last active February 27, 2021 03:45
How should non-reactive functions handle errors to be compatible with shiny?
## How should we write non-reactive (business logic) functions so that they
## are maximally compatible with shiny/reactive processing?
## Here's a function which performs business logic. It may be used at the
## console or it may be used in a shiny app.
## A shiny app will probably provide the right type of input so the
## input validations are perhaps not as critical, but a typical design
## is to stop() if a critical condition is not met during calculations.
var_top_cars <- function(d, minval) {
stopifnot("mpg not found" = utils::hasName(d, "mpg"))
@jonocarroll
jonocarroll / fakelegend.R
Created May 7, 2020 01:38
Add a fake legend annotation to a ggplot
library(ggplot2)
library(dplyr)
## fake data
d <- mtcars %>%
group_by(cyl) %>%
summarise(lower = min(disp),
med = median(disp),
upper = max(disp))
p <- ggplot(d, aes(x = factor(cyl), y = med)) +
@jonocarroll
jonocarroll / coviddeaths.R
Last active May 7, 2020 03:24
USA daily deaths with and without NY, highlighting weekends
library(COVID19)
library(dplyr)
library(zoo) # rollmean
library(ggplot2)
library(ggeasy) # easy_add_legend_title
#### deaths ####
## gather USA data at state level and identify weekends
us <- covid19("USA", level = 2) %>%
@jonocarroll
jonocarroll / google_covid_mobility_plotter.R
Created April 16, 2020 12:30
Modification of google_covid_mobility_scraper to work with @nacnudus' data
# Fork of https://gist.github.com/johnburnmurdoch/1d23978fc8213ff656d9f629608dd1f5/revisions
# modified to work with https://github.com/nacnudus/google-location-coronavirus
# Install and load required packages
# install.packages("needs")
# library(needs)
# needs(tidyverse, magrittr, animation, pdftools, png, scales)
library(tidyverse)
library(magrittr)
library(animation)
@jonocarroll
jonocarroll / euler_d3.R
Created February 21, 2020 22:48
Interactive set selector
library(htmltools)
library(d3r)
library(eulerr)
ui <- list(
attachDependencies(
tagList(),
d3_dep_v5()
),
titlePanel("Interactive Set Selector"),
@jonocarroll
jonocarroll / R_sort.R
Last active December 3, 2019 07:05
R vs Julia sorting
fastsort <- function(x, partial) {
y <- if (length(partial) <= 10L) {
partial <- .Internal(qsort(partial, FALSE))
.Internal(psort(x, partial))
}
else {
.Internal(qsort(x, FALSE))
}
y
}