Skip to content

Instantly share code, notes, and snippets.

View mikmart's full-sized avatar

Mikko Marttila mikmart

  • Orion Pharma
  • United Kingdom
View GitHub Profile
@mikmart
mikmart / nested-observers.R
Last active May 19, 2023 22:16
Handle nested observers in Shiny
library(shiny)
ui <- fluidPage(
sliderInput("a", "Slider A", 0, 10, 1),
sliderInput("b", "Slider B", 0, 10, 1),
actionButton("create_observers", "Create observers")
)
server <- function(input, output, session) {
slider_observers <- list()
@mikmart
mikmart / util.h
Created May 5, 2023 22:04
C Macros for R
#define PRINT_TYPEOF(x) \
Rprintf("Type of %s is %s [%d].\n", #x, Rf_type2char(TYPEOF(x)), TYPEOF(x))
@mikmart
mikmart / liars.csv
Last active December 2, 2021 23:53
Frequent strong liars among witness numbers (with top 100 results up to 115,471)
witness lies
4096 246
256 171
1296 140
729 115
16 114
6561 108
1024 98
625 96
64 90
@mikmart
mikmart / kent-county.md
Last active November 15, 2020 18:11
Kent county direct vs. party vote shares in the 2020 US presidential elections

A replication of the results in Matt Parker’s video: https://youtu.be/aokNwKx7gM8

library(tidyverse)
#> Warning: package 'tidyr' was built under R version 4.0.2

# Download the raw data from Matt's Dropbox link
file_url <- "https://dl.dropboxusercontent.com/s/7gusbewresl6zsg/Kent%20County%20Precinct%20Data%20from%20Stand-up%20Maths%20video.xlsx"
file <- tempfile(fileext = ".xlsx")
r <- httr::GET(file_url, httr::write_disk(file))
library(ggplot2)
library(patchwork)

set.seed(31987)

n <- 100
g <- sample(c("a", "b"), n, replace = TRUE)
x <- rexp(n, 1 / 5)
b <- c(5, 10)
@mikmart
mikmart / split-interval.R
Created November 9, 2019 08:26
A helper function to split a time interval into multiple smaller periods
library(tidyverse)
library(lubridate)
split_interval <- function(start, end, unit) {
breaks <- seq(floor_date(start, unit), ceiling_date(end, unit), by = unit)
timeline <- c(start, breaks[breaks > start & breaks < end], end)
tibble(
.seq = seq_len(length(timeline) - 1),
.start = head(timeline, -1),
.end = tail(timeline, -1),
@mikmart
mikmart / move.R
Last active September 27, 2019 13:41
A dplyr-like verb to move columns in a data frame
vars_select_pos <- function(.vars, ...) {
match(tidyselect::vars_select(.vars, ...), .vars)
}
before <- function(..., .vars = tidyselect::peek_vars()) {
min(vars_select_pos(.vars, ...)) - 1
}
after <- function(..., .vars = tidyselect::peek_vars()) {
max(vars_select_pos(.vars, ...))
@mikmart
mikmart / reshaping.md
Last active March 25, 2019 10:07
Thoughts on what reshaping data is in general

Some thoughts on reshaping data

Key idea: Reshaping is essentially about decoding data from old column names, and then encoding other data into new column names. How the values move around, i.e. how shape of the data changes, is just a byproduct.

Method

Essential steps:

  1. Select columns that are transformed.
  2. Decode variables from selected column names.
@mikmart
mikmart / file-format-timings.md
Last active March 1, 2019 13:01
Testing read/write times for data frames in different file formats
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
@mikmart
mikmart / slow-groups.md
Last active February 5, 2019 13:37
A grouped summarize gets slow with many groups?
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union