This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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:///", "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |