Skip to content

Instantly share code, notes, and snippets.

View djnavarro's full-sized avatar

Danielle Navarro djnavarro

View GitHub Profile
# simple R6 class
Random <- R6::R6Class("Random",
active = list(draw = function() runif(1))
)
# instantiate the class
rnd <- Random$new()
# this calls the function
val <- rnd$draw
@djnavarro
djnavarro / smudged_hexagon_transparent.R
Created August 22, 2022 03:53
source code for the smudged hexagon t-shirt image
library(dplyr)
library(purrr)
library(tidyr)
library(tibble)
library(ggplot2)
library(tictoc)
library(ggthemes)
library(here)
edge_length <- function(x1, y1, x2, y2) {
unpack_double <- function(x) {
binary <- numToBits(x) # little-endian binary representation
structure(
list(
sign = binary[64], # 1-bit sign
exponent = binary[63:53], # 11-bit exponent
mantissa = binary[52:1] # 52-bit mantissa
),
class = "unpacked_double"
set.seed(1)
sample_values <- function(n, p_safe) {
val <- rep("safe", n)
val[runif(n) > p_safe] <- "UNSAFE\n"
return(val)
}
# the wild-caught data had this structure
old_df <- tibble::tibble(
@djnavarro
djnavarro / force_git_repo.R
Created August 22, 2021 01:12
lazy way to force git repo creation
library(gert)
library(usethis)
create_git_repo <- function(path) {
cat(path, "\n")
d <- setwd(path)
on.exit(setwd(d))
git_init()
use_git_ignore(usethis:::git_ignore_lines)
@djnavarro
djnavarro / .Rprofile
Created June 24, 2021 11:30
my .Rprofile settings
if(interactive()) {
cat("\014") # clear screen
cli::cli_text("")
cli::cli_text(R.version$version.string)
cli::cli_text("")
cli::cli_alert_success(
paste0(
@djnavarro
djnavarro / aggregate_rowwise_by_prefix.R
Created May 11, 2021 07:40
aggregate a tibble rowwise, grouped by prefix
library(tidyverse)
# make toy data -----------------------------------------------------------
# column of n randomly sampled responses
likert_col <- function(n = 10) {
sample(7, size = 10, replace = TRUE)
}
# toy data
@djnavarro
djnavarro / evil_paste.R
Created May 4, 2021 07:27
an evil paste0 function
# attach evil_shims if needed
if(!("evil_shims" %in% search())) {
attach(new.env(), name = "evil_shims", pos = 2)
}
# paste0 function appends an invisible utf8 character 10% of the time
# e.g., for(i in 1:100) print(paste0("a", "b") == "ab")
assign(
x = "paste0",
library(tidyverse)
library(scales)
library(ambient)
heart_x <- function(angle) {
x <- (16 * sin(angle)^3)/17
return(x - mean(x))
}
@djnavarro
djnavarro / pop_heart.R
Created February 20, 2021 07:11
turmite piece in pop art colours
pop <- (ggthemes::canva_pal("Pop art"))(4)
turmite59::turmite59(
shade = "#000000",
file = "~/Desktop/pop_heart.png",
palette_n = 4,
palette_heart = pop,
palette_turmite = pop,
turmite_steps = 100000000,
turmite_stepsize = 1
)