Skip to content

Instantly share code, notes, and snippets.

@dmi3kno
dmi3kno / distracted_boyfriend.R
Created April 25, 2020 23:58
{magick} code for distracted boyfriend
library(magick)
library(bunny)
# function for calculating mode
mode_n <- function(x, n=1){
res <- with(rle(sort(x)), values[order(lengths, decreasing = TRUE)])
res[n]
}
img <- image_read("EWOwbehXkAgxeHO.jpeg") %>%
library(tidyverse)
library(brms)
library(tidybayes)
mpg_model <- brm(
bf(hwy ~ 0 + drv),
data = mpg,
prior = c(
set_prior("normal(20, 3)", class = "b", lb = 0, ub = 40),
set_prior("student_t(3, 0, 10)", class = "sigma")
@dmi3kno
dmi3kno / devstuff.R
Last active February 20, 2020 21:33
Devstuff file that should go into your data-raw/ directory for starting a new package
# this file was created with
# usethis::use_data_raw("devstuff")
usethis::use_description(fields = list(
# Package = "yourcoolpackagename", # default, no need to specify
# Version = "0.0.0.9000", # default, no need to specify
Title = "Catchy Title For Your Package In Title Case",
Description = "Describe what your package does. End with a dot.", # at least one sentence
`Authors@R` = 'c(person("Dmytro", "Perepolkin",
email = "[email protected]", role = c("aut", "cre"),
@dmi3kno
dmi3kno / make_hex.R
Last active February 4, 2020 10:16
Making magick logo
library(magick)
# remotes::install_github("dmi3kno/bunny")
library(bunny)
# this is for editing svg removing all tags of type <text>
library(xml2)
im_path <- "https://upload.wikimedia.org/wikipedia/commons/9/9a/ImageMagick_logo.svg"
a <- xml2::read_xml(im_path)
a1 <- a %>%
@dmi3kno
dmi3kno / future_cover_magick.R
Last active July 28, 2019 14:26
Thanks to Will Chase @W_R_Chase
library(magick)
library(bunny)
library(baRcodeR)
joe <- image_read("~/Downloads/bg/joe_mango2.png")
################## make barcode #################
baRcodeR::custom_create_PDF(user=FALSE, Labels = 20190728,
name = '~/Downloads/bg/LabelsOut', type = 'linear')
library(magick)
library(tidyverse)
library(tesseract)
library(hocr)
################ helpers ####################
image_plot<-function(img, x, y, col="green", pointsize=5){
res <- image_draw(img, pointsize = pointsize)
points(x, y, col=col)
dev.off()
@dmi3kno
dmi3kno / blobs.R
Last active July 24, 2019 23:05
Making pretty blobs
library(magick)
library(tidyverse)
# generate noise and apply morphological transformations to make blobs
blobs <- image_blank(600,400, "black") %>%
image_noise("Impulse") %>%
image_channel("Green") %>%
image_morphology("Dilate", "Diamond:6") %>%
image_morphology("Open", "Disk:10", 3) %>%
image_blur(10,10) %>%
@dmi3kno
dmi3kno / babynames.R
Last active June 23, 2019 23:08
Ukrainian baby names in Lviv Region
library(tidyverse)
library(rvest)
robotstxt::paths_allowed("http://ukrcensus.gov.ua")
urlencode_utf_win1251 <- function(x){
d <- "0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29, 30: 30, 31: 31, 32: 32, 33: 33, 34: 34, 35: 35, 36: 36, 37: 37, 38: 38, 39: 39, 40: 40, 41: 41, 42: 42, 43: 43, 44: 44, 45: 45, 46: 46, 47: 47, 48: 48, 49: 49, 50: 50, 51: 51, 52: 52, 53: 53, 54: 54, 55: 55, 56: 56, 57: 57, 58: 58, 59: 59, 60: 60, 61: 61, 62: 62, 63: 63, 64: 64, 65: 65, 66: 66, 67: 67, 68: 68, 69: 69, 70: 70, 71: 71, 72: 72, 73: 73, 74: 74, 75: 75, 76: 76, 77: 77, 78: 78, 79: 79, 80: 80, 81: 81, 82: 82, 83: 83, 84: 84, 85: 85, 86: 86, 87: 87, 88: 88, 89: 89, 90: 90, 91: 91, 92: 92, 93: 93, 94: 94, 95: 95, 96: 96, 97: 97, 98: 98, 99: 99, 100: 100, 101: 101, 102: 102, 103: 103, 104: 104, 105: 105, 106: 106, 107: 107, 108: 108, 109: 109, 110: 110,
compute_hpdi_TJ <- function(xs, prob = .9) {
x_sorted <- sort(xs)
n <- length(xs)
num_to_keep <- ceiling(prob * n)
num_to_drop <- n - num_to_keep
possible_starts <- seq(1, num_to_drop + 1, by = 1)
# Just count down from the other end
possible_ends <- rev(seq(from = n, length = num_to_drop + 1, by = -1))
set.seed(42) # The answer to life, the universe and everything
## This is what the expert said
p10 <- 8
p50 <- 12
p90 <- 25
# I can approximate it with Myerson distribution defined by quantiles
prior_predictive_elicited <- tidyear::rMyerson(1e5, p10, p50, p90, tl=0.2)