Skip to content

Instantly share code, notes, and snippets.

View djnavarro's full-sized avatar

Danielle Navarro djnavarro

View GitHub Profile
@djnavarro
djnavarro / orientation.R
Created January 16, 2020 21:00
A simple psychological experiment in R using X11
library(tidyverse)
# create an X11 window
X11(width = 8, height = 8,
xpos = 100, ypos = 30)
# clear the plotting area
clear_plot <- function() {
plot.new()
plot.window(xlim = c(0,1), ylim = c(0,1))
@djnavarro
djnavarro / bad_seeds.R
Last active December 27, 2019 08:21
Two pseudorandom functions that give the same output given the same RNG, but leave the RNG in different states
# version 1 of the function
pick_name1 <- function() {
names <- c("Dax", "Wug", "Lep", "Sik", "Bop")
sample(names)[1]
}
# version 2 of the function
pick_name2 <- function() {
names <- c("Dax", "Wug", "Lep", "Sik", "Bop")
sample(names, size = 1)
@djnavarro
djnavarro / dependency_hell.R
Created December 27, 2019 07:02
The joy of coding when the RNG state is global...
# someone else's package includes this inefficient function
pick_name <- function() {
names <- c("Dax", "Wug", "Lep", "Sik", "Bop")
sample(names)[1]
}
# here is my code that calls it...
set.seed(100)
print("--- Attempt 1 ---")
print(pick_name()) # Wug
@djnavarro
djnavarro / cubismism.R
Last active December 22, 2019 01:38
demo of jasmines 0.0.0.9001
# install.packages("remotes")
# remotes::install_github("djnavarro/jasmines")
library(jasmines)
set.seed(435)
scene_delaunay(6, 500) %>%
unfold_slice() %>%
style_ribbon(
palette = rainbow,
seed_col = "#ffffffaa",
alpha_init = .1,
@djnavarro
djnavarro / ozunconf1.R
Created December 12, 2019 23:20
ozunconf heart
library(jasmines)
seed_heart(2000) %>%
time_tempest(iterations = 20, curl_seed = 43) %>%
style_ribbon(palette = rainbow, alpha_decay = .1) %>%
export_image("~/Desktop/ozunconf1.png")
@djnavarro
djnavarro / molten_heart4.R
Created December 1, 2019 00:03
fifty shades of pink
#install.packages("remotes")
#remotes::install_github("djnavarro/jasmines")
#remotes::install_github("djnavarro/fifty")
library(jasmines)
library(dplyr)
set.seed(125)
seed_heart(10000) %>%
mutate(
x = x * 35,
library(DiagrammeR)
g <- DiagrammeR("
graph TB
A[Is this tweet about #rstats?]-->|yes|B[Hell yeah tweet it honey!]
A-->|no|C[Is a statistics tweet?]
C-->|yes|D[Is it about statistics in psychology?]
D-->|no|B
D-->|yes|E[Are you prepared for a pointlessly heated argument?]
E-->|um...no|F[ARE YOU CRAZY? DO NOT TWEET THIS]
library(ggplot2)
ozmap <- function(proj4string) {
dat <- sf::st_transform(ozmaps::abs_ced, proj4string)
ggplot(dat) +
geom_sf() +
coord_sf() +
theme_void()
}
@djnavarro
djnavarro / .Rprofile
Created July 22, 2019 01:49
my r profile
if(interactive()) {
# packages helpful for development
suppressMessages({
require(devtools)
require(usethis)
require(testthat)
require(roxygen2)
require(git2r)
@djnavarro
djnavarro / censor.R
Last active July 10, 2019 02:41
quick and dirty code for systematic censoring
library(tidyverse)
# define the censoring function
censor_smoking <- function(df) {
# (this code is awful I am so sorry)
df <- df %>%
rowwise() %>% # ugh... rowwise
mutate(keep_case = case_when(
(sex.factor == "Male" & runif(1) < .3) ~ 0, # censor male with prob .3