Skip to content

Instantly share code, notes, and snippets.

Avatar
🍕

Nicholas Tierney njtierney

🍕
View GitHub Profile
View rename-file-helpers.R
rename_with_dash <- function(dir){
files_to_rename <- list.files(dir,
pattern = "_",
recursive = TRUE,
full.names = TRUE)
files_new_names <- files_to_rename %>%
str_replace_all(pattern = "_", replacement = "-")
walk2(files_to_rename,
View example-leaflet.R
library(leaflet)
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
add_birthplace_marker <- function(map, popup = "The birthplace of R"){
map %>%
addMarkers(lng=174.768, lat=-36.852, popup= popup)
}
View wa-hikes.md
bushwalk_url <- "http://trailswa.com.au/trails/trail-types/bush-walk/"

library(polite)
library(rvest)
#> Loading required package: xml2
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
View water-grid-thing.md
library(magrittr)
compute_posterior_water_grid <- function(n = 20,
                                         n_water = 6,
                                         n_land = 3,
                                         prior = rep(x = 1,times = n),
                                         p_grid = seq(from = 0, 
                                                      to = 1, length.out = n)){
  
  # define posterior
  # p_grid <- seq(from = 0, to = 1, length.out = n)
View halving.md
library(purrr)
cases <- 250
halving_thing <- function(cases, n_times){
  purrr::accumulate(.x = rep(cases, n_times), .f = ~.x * 0.5)
}

halving_thing(532, 3)
#> [1] 532 266 133
halving_thing(227, 3)
#> [1] 227.00 113.50  56.75
View example-quote-bare-vars.md
library(tidyverse)
my_plot <- function(data, x,y){
  ggplot(data,
         aes(x = {{ x }},
             y = {{ y }})) + 
    geom_point()
}

library(palmerpenguins)
View example-glm.md
library(tidyverse)

vec_prob <- c(6, 3, 2, 1.5, 1, 0.5, 0.1)

example_data <- crossing(
  id = c(1:100),
  day = 1:7
) %>% 
  group_by(id) %>% 
  mutate(sleep = sample(c("normal", "slow_wave", "REM"), size = 1)) %>% 
View chord-table.md
library(tidyverse)
library(knitr)

chord_table <- tribble(
  ~number, ~flavour, ~intervals,
  1,       "Major 7",    "1 3 5 7",
  2,       "Minor 7",    "1 b3 5 b7",
  3,       "Minor 7",    "1 b3 5 b7",
  4,       "Major 7",    "1 3 5 7",
  5,       "Dominant 7", "1 3 5 b7",
@njtierney
njtierney / alpha_num.md
Created Sep 21, 2020
Function to generate random alpha numeric strings of a given length. Used to generate random API keys
View alpha_num.md
alpha_num <- function(n){
  
  pool <- c(letters,
            LETTERS,
            rep(0:9, length.out = length(letters)))
  
  paste0(
  sample(x = pool,
         size = n,
         replace = TRUE),
View map-purrr-bind_rows.md

some things on purrr

# using bind_rows
head(bind_rows(one = cars, 
               two = cars, 
               .id = "anything"))
#> Error in bind_rows(one = cars, two = cars, .id = "anything"): could not find function "bind_rows"
tail(bind_rows(one = cars, two = cars, .id = "anything"))
#> Error in bind_rows(one = cars, two = cars, .id = "anything"): could not find function "bind_rows"
You can’t perform that action at this time.