Skip to content

Instantly share code, notes, and snippets.

View schochastics's full-sized avatar
👨‍💻

David Schoch schochastics

👨‍💻
View GitHub Profile
@schochastics
schochastics / nba_scorer.R
Created April 10, 2019 17:58
Barchart Race of Top NBA Scorers
library(rvest)
library(tidyverse)
library(gganimate)
library(extrafont)
loadfonts()
get_season <- function(x){
url <- paste0("https://www.basketball-reference.com/leagues/NBA_",x,"_totals.html")
url %>%
read_html() %>%
@schochastics
schochastics / ggpyramid.R
Created May 30, 2019 14:15
ugly 3D pyramid plot
pyramid <- function(ymax=5,mcol="#CD3333",xoffset=0){
f <- function(a,b,y){
(y-b)/a
}
a1 <- ymax/0.65
b1 <- 0
c1 <- -ymax/(1-0.65)
d1 <- -c1
a2 <- -ymax/(1-0.65)
@schochastics
schochastics / lissajous.R
Last active October 22, 2019 19:47
animated lissajous curves
lissajou <- function(a,b,delta){
t <- seq(0,2*pi,0.01)
x <- sin(a*t+delta)
y <- sin(b*t)
tibble(x,y)
}
# setup parameters ----
n <- 7
params <- tibble(a=sample(1:10,n,replace = FALSE),
@schochastics
schochastics / time_prediction.R
Created November 12, 2019 18:56
time prediction
library(tidyverse)
library(ggforce)
d <- tibble(x = c(1, 6, 10, 23,18), xpred = c(4, 8, 11, 2,22), y = 1:5, id = factor(1:5))
n <- nrow(d)+1
time_angle <- tibble(hour=1:24,angle=0.25*60*hour)
d$start <- time_angle$angle[d$x]*pi/180
d$end <- time_angle$angle[d$xpred]*pi/180
d$r <- d$y
@schochastics
schochastics / prime_viz.R
Last active August 20, 2020 10:14
Visualization of Prime Factorization
# Recreation of https://twitter.com/stevenstrogatz/status/1295915404574040065/photo/1
library(tidyverse)
library(Polychrome)
fun <- function(x){
n <- c()
i <- 2
r <- x
while(prod(n)!=x){
if(!r%%i) {n=c(n,i);r=r/i;i=1}
i <- i+1
@schochastics
schochastics / get_tweets.R
Last active September 22, 2021 08:40
Get tweets from the Academic Research product track.
# start_time: %Y-%m-%dT%H:%M:%SZ
# end_time: %Y-%m-%dT%H:%M:%SZ
# needs jsonlite and httr
# next_token can be obtained from meta$next_token to paginate through results
get_tweets <- function(q="",n=10,start_time,end_time,token,next_token=""){
if(n>500){
warning("n too big. Using 500 instead")
n <- 500
}
if(n<5){
@schochastics
schochastics / yt_rstudio.R
Last active February 1, 2024 13:55
Play youtube videos in Rstudio viewer pane
library(shiny)
xy <- c(784,479) #output of grDevices::dev.size("px")
url <- "https://www.youtube.com/watch?v=Ef2jmf2vy00" #copy yt link here
url <- gsub("watch\\?v=","embed/",url)
ui <- fluidPage(
HTML(paste0('<iframe width="',xy[1],'" height="',xy[2],'" src="',url,'" frameborder="0"></iframe>'))
)
server <- function(input, output, session) {
}
@schochastics
schochastics / layout_yearly.R
Created March 29, 2021 15:02
use constrained stress
library(igraph)
library(ggraph)
library(graphlayouts)
#handdrawn graph without isolates
el <- matrix(c(1,3,1,3,1,6,2,3,2,6,
3,4,3,4,3,6,4,6,4,8,
5,7,5,8,7,8,7,9,7,10,
7,11,9,10,10,11,9,11),ncol = 2,byrow = TRUE)
g <- graph_from_edgelist(el,F)
@schochastics
schochastics / pack_circles.cpp
Last active January 16, 2022 07:34
Reimplementation of @ijeamaka_a randomized circle packing R code with Rcpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double distance(double x1, double x2, double y1, double y2){
double dist = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
return dist;
}
// [[Rcpp::export]]
@schochastics
schochastics / cran.lua
Last active July 15, 2022 21:48
automatic styling and linking for R packages mentioned in quarto docs
function CRAN(handle)
local output = '<span class="cran-pkg"><a href="https://cran.r-project.org/package=' ..
pandoc.utils.stringify(handle) .. '">'..
pandoc.utils.stringify(handle)..'</a><i class="fa-brands fa-r-project fa-2xs"></i></span>'
return pandoc.RawBlock('html', output)
end