Skip to content

Instantly share code, notes, and snippets.

@kelly-sovacool
kelly-sovacool / tidy-eval-dynamic-colname.R
Last active September 22, 2022 14:52
How to use a data variable to name a new column name with tidy eval
library(tidyverse)
f <- function(dat, colname) {
varname <- rlang::as_name(rlang::enquo(colname))
new_df <- data.frame(x = c('a','b','c')) %>%
mutate({{varname}} := dat %>% pull({{ colname }}))
return(new_df)
}
df1 <- data.frame(a = 1:3, b = 4:6)
df1 %>% f(a)
@kelly-sovacool
kelly-sovacool / .gitattributes
Last active April 5, 2022 17:53
palettes from RColorBrewer
ColorBrewer.R linguist-vendored=true
@kelly-sovacool
kelly-sovacool / maintaining-R-pkgs.md
Last active November 28, 2021 03:36
Maintaining R packages - Schloss Lab Code Club 2021-04-19

Maintaining R packages

Why make a package?

  • To make it easier for others to re-use your code.
  • To signify that your code adheres to certain conventions agreed upon by the community.

What is an R package?

At minimum: a collection of R scripts in a directory called R/ + a DESCRIPTION file

@kelly-sovacool
kelly-sovacool / ggplot_function_reprex.R
Last active August 11, 2023 14:38
How to write a function to add multiple ggproto objects to a ggplot
#' ---
#' output:
#' md_document:
#' pandoc_args:
#' - '--from=markdown-implicit_figures'
#' - '--to=commonmark'
#' - '--wrap=preserve'
#' ---
@kelly-sovacool
kelly-sovacool / adv-R_custom-conditions.R
Created October 4, 2020 17:10
Practice with custom conditions in R
# practice with custom conditions
# https://adv-r.hadley.nz/conditions.html#custom-conditions
library(dplyr)
abort_bad_argument <- function(arg, must, not = NULL) {
msg <- glue::glue("`{arg}` must {must}")
if (!is.null(not)) {
not <- typeof(not)
msg <- glue::glue("{msg}; not {not}.")
}
@kelly-sovacool
kelly-sovacool / ifelse-gotcha.R
Last active September 22, 2020 19:05
R's ifelse() doesn't behave as I first expected
pick_something <- function(choice) {
ifelse(choice == 'list',
list("here's", "your", "list"),
"this isn't a list")
}
pick_something('anything_else')
#> [1] "this isn't a list"
pick_something('list')
#> [[1]]
@kelly-sovacool
kelly-sovacool / code_club_2020-05-04.Rmd
Last active June 1, 2020 14:37
Pat’s code club on ggplot themes
---
date: "2020-05-04"
output:
github_document:
html_preview: false
---
```{r, include=FALSE}
knitr::opts_chunk$set(fig.path = here::here())
```
@kelly-sovacool
kelly-sovacool / bob_ross.R
Created March 31, 2020 16:59
Joy of Coding: Nick's code club 2020-03-30 w/ Will & Katie
library(tidyverse)
ross_data <- read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/bob-ross/elements-by-episode.csv",
col_types="ccddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd") %>%
mutate(TITLE = gsub("\\\"", '', TITLE))
themes <- c('MOUNTAIN', 'WINTER', 'AUTUMN', 'LAKE', 'CABIN')
#############################################################
# Part 1 - fill in the blanks each line of the function
@kelly-sovacool
kelly-sovacool / URSSI_winterschool_notes.md
Last active September 3, 2020 20:25
Notes from the URSSI Winter School
@kelly-sovacool
kelly-sovacool / spearman-perm.md
Last active December 12, 2019 05:27
Comparing runtime of a for loop vs list comprehensions for a permutation test

spearman-perm

Comparing runtime of a for loop vs comprehensions for a permutation test

Usage

$ python spearman-perm.py <p_value> <num_sims>

Results