Skip to content

Instantly share code, notes, and snippets.

View schochastics's full-sized avatar
👨‍🍼

David Schoch schochastics

👨‍🍼
View GitHub Profile
@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) {
}
---
title: "Untitled"
format: html
---
```{r}
#| label: prepare
#| echo: true
@schochastics
schochastics / mailify.R
Created February 10, 2023 14:57
recreate chart style of daily mail
library(ggplot2)
library(ggpubr)
library(ggtext)
df <- data.frame(x=seq.Date(as.Date("2020-04-01"),as.Date("2022-07-01"),by="month"),y=(1:28)^2)
p <- ggplot(df, aes(x, y))+
geom_line(linewidth = 2.2,color="white",lineend = "round")+
geom_line(linewidth = 1,color="red",lineend = "round")+
scale_x_date(labels=scales::label_date("%b\n%Y"))+
@schochastics
schochastics / signed_triad_census.R
Created December 22, 2022 19:23
Visualize al possible signed triads
library(signnet)
library(igraph)
library(ggraph)
library(patchwork)
triads <- c("003-000000", "012-0000P0", "012-0000N0",
"102-0000PP", "102-0000NP", "102-0000NN", "021C-0PP000",
"021C-0NP000", "021C-0PN000", "021C-0NN000", "021U-0P0P00",
"021U-0N0P00", "021U-0N0N00", "021D-P0P000", "021D-N0P000",
"021D-N0N000", "111U-0P00PP", "111U-0N00PP", "111U-0P00NP",
"111U-0P00PN", "111U-0N00NP", "111U-0N00PN", "111U-0P00NN",
# original python code: https://fosstodon.org/@jasonbaumgartner@infosec.exchange/109376317839501128
# mapping: https://twitter-elastic.pushshift.io/twitter_user/_mapping
library(curl)
library(jsonlite)
ngram_search <- function(q="",size = 10){
data <- list(query=list())
data[["query"]][["bool"]] <- list()
data[["query"]][["bool"]][["must"]] <- list()
match <- list()
@schochastics
schochastics / weather_spain.R
Created August 12, 2022 13:17
ggraph circlepack on maps
library(tidyverse)
library(sf)
library(ggraph)
library(igraph)
# create some random data
country <- mapSpain::esp_get_prov(moveCAN=TRUE)
provinces <- st_cast(country,"MULTIPOLYGON") |> st_cast("POLYGON")
centroids <- distinct(provinces,iso2.prov.name.es,.keep_all = TRUE) |>
dplyr::filter(!iso2.prov.name.es%in%c("Baleares","Las Palmas","Santa Cruz de Tenerife","Ceuta","Melilla")) |>
@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
@schochastics
schochastics / age_vs_value.R
Created August 24, 2018 21:21
scrape mean age and market values for European Football leagues
library(tidyverse)
library(rvest)
library(ggimage)
library(lubridate)
#get first 25 leagues in Europe ----
url <- "https://www.transfermarkt.de/wettbewerbe/europa"
doc <- read_html(url)
leagues <- doc %>% html_nodes(".hauptlink a") %>% html_attr("href")
@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 / 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){