Skip to content

Instantly share code, notes, and snippets.

View andrewmoles2's full-sized avatar
🦕

Andrew Moles andrewmoles2

🦕
View GitHub Profile
@andrewmoles2
andrewmoles2 / coolors_hex_extract.py
Last active March 14, 2022 17:03
Coolors function in Python
# import plotnine for demo and testing
from plotnine import *
from plotnine.data import mtcars
def coolors(URL):
# function takes coolors url, extracts hex codes and adds #
if URL.find("palette") != -1:
# extract just the hex
cstr = URL.replace("coolors.co", "")
cstr = cstr.replace("https:///", "")
@andrewmoles2
andrewmoles2 / coolors_hex_extract.R
Last active December 3, 2024 14:49
Coolors function in R
# Function for generating coolors hex codes
coolors <- function(URL) {
#' Coolors hex generator
#'
#' Function for taking coolors URL that contains hex codes, and extracting that information so we can use the hex codes
#' Currently works for palettes in: palette generator, explore palettes, and gradients
#' @param URL a URL from https://coolors.co/ that contains hex codes, e.g. https://coolors.co/ddfcad-c8e087
# detect if from default or explored palettes
# function to check install and load packages
load_library <- function(pkg) {
# check if package is installed
new_package <- pkg[!(pkg %in% installed.packages()[, "Package"])]
# install any new packages
if (length(new_package))
install.packages(new_package, dependencies = TRUE, Ncpus = 6)
# sapply to loop through and load packages
@andrewmoles2
andrewmoles2 / update.R
Last active October 5, 2023 12:08
Script to aid in installing packages after R update
# script to upgrade R and re-install all (or at least most) packages
# Pre-upgrade: save your packages as an rds file
my_pkg <- pak::pkg_list()
# if loaded from RStudio
#here::here("R", "update_packages", "my_packages.rds")
# if loaded from finder
saveRDS(object = my_pkg, file = "my_packages.rds")
# code from http://r.iresmi.net/2023/04/05/cherry-blossom/ written into function for future use
get_blossom <- function(url = "http://atmenv.envi.osakafu-u.ac.jp/aono/kyophenotemp4/") {
GET(url) |>
content() |>
html_element("pre") |>
html_text2() |>
str_replace_all("\xc2\xa0", " ") |> # bad encoding needs correction
read_fwf(fwf_cols("ad" = c(7, 10),
"fifd" = c(17, 20),
# function to make table of descriptive statistics
# rows are column names, columns are the descriptives (mean, min etc.)
descriptive_tab <- function(df, cols, round_digits = 2) {
# cols needs to be a vector
# cols <- c('col1', 'col2', 'col3')
tab <- data.frame(
t(
@andrewmoles2
andrewmoles2 / clean_names.py
Created December 6, 2024 11:46
Janitor clean names in Python
def clean_names(cols):
current_cols = cols
new_cols = []
for c in range(0, len(current_cols)):
new_cols.append(
current_cols[c].lower().replace(' ', '_').replace('(', '').replace(')', '')
)
return(new_cols)
# we use the .columns method to access the column names