Skip to content

Instantly share code, notes, and snippets.

@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 = "dperepolkin@gmail.com", 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)
@dmi3kno
dmi3kno / lundaloppet_plot.R
Created April 18, 2019 19:09
plotting running time distribution from
library(readxl)
library(tidyverse)
library(hms)
library(hrbrthemes)
library(extrafont)
read_excel("lundaloppet2018.xlsx", col_names = F, col_types = "text") %>%
janitor::clean_names() %>%
mutate(x2=parse_number(x2),
x5n = parse_number(x5),
@dmi3kno
dmi3kno / topogeo.R
Last active December 7, 2018 20:59
# create geometry matrix
geom <- cbind(x=c(4,4,2,5,2,3,3,3,4,2),
y=c(6,2,3,4,5,2,4,7,8,0))
# create data frame with special topology list column
df <- tibble::tibble(name="John", age=32,
topo=list( # list column containing polygon(s) and inner/outer flag
list(poly = list(c(10, 5, 8, 9, 4, 2),
c(3, 6, 1, 7)),
type = c(1L, 0L)))