Skip to content

Instantly share code, notes, and snippets.

View mattgalbraith's full-sized avatar

mattgalbraith

  • Linda Crnic Institute for Down Syndrome
  • University of Colorado Anschutz Medical Campus
View GitHub Profile
@mattgalbraith
mattgalbraith / .gitignore
Created May 17, 2024 15:44
Git ignore file for R Projects
# Ignore .DS_Store
.DS_Store
# Ignore R image files
rdata/*RData
#####################################
# From Github R .gitignore template #
#####################################
# History files
@mattgalbraith
mattgalbraith / wget_CVATICA_download.txt
Last active October 12, 2023 22:38
Download files from CAVATICA via text files with temporary (48hrs) file links
cat download_links.txt | xargs -P 1 wget --content-disposition --trust-server-names -nv -a wget_downloading.log
@mattgalbraith
mattgalbraith / GitHub_repo_setup.md
Last active February 12, 2023 19:41
GitHub repo setup

Set up new GitHub repo and push from local

  1. Create local repo and create/add/edit required files and dirs
    eg README.md, Dockerfile, .gitignore, .github/workflows/docker-image.yml

  2. On GitHub select "New repository" from the "+" menu and set name to match local repo.
    Do not add any template files.

  3. Initialize up local repo and push to GitHub:
    (assumes currently in local repo root dir)

@mattgalbraith
mattgalbraith / gg_color_hue.R
Created February 1, 2023 18:27
R function to get hex codes for standard ggplot colors
# get standard ggplot colors
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
@mattgalbraith
mattgalbraith / obj_size.R
Created February 1, 2023 18:25
R function to get object size in human-readable units
obj_size <- function(x) object.size(x) %>% print(units = "auto")
@mattgalbraith
mattgalbraith / mem_used.R
Created February 1, 2023 18:25
R function to check current memory usage
mem_used <- function() lobstr::mem_used() %>% as.numeric() %>% R.utils::hsize()
@mattgalbraith
mattgalbraith / export_excel.R
Last active February 1, 2023 18:37
R function to export tables in XLSX format
## Excel export function
export_excel <- function(named_list, filename = "") {
wb <- openxlsx::createWorkbook()
## Loop through the list of split tables as well as their names
## and add each one as a sheet to the workbook
Map(function(data, name){
openxlsx::addWorksheet(wb, name)
openxlsx::writeData(wb, name, data)
}, named_list, names(named_list))
## Save workbook to working directory
@mattgalbraith
mattgalbraith / density_color_function.R
Last active February 1, 2023 18:43
R function to generate 2D Density colors for ggplot
## Density color function
# usage:
# tbl |>
# mutate(density = getDenCols(x, y, transform = TRUE)) |> # transformation here must match aes()
# arrange(density) |> # ensures highest desity points on top
# ggplot(aes(log2(x), log2(y), color = density)) +
# geom_point() +
# scale_color_vididis_c() # works best with viridis
getDenCols <- function(x, y, transform = TRUE) { # set to TRUE if using log2 transformation of data
if(transform) {
@mattgalbraith
mattgalbraith / git-credentials.R
Created February 1, 2023 18:07
Managing Github credentials from R
# See https://usethis.r-lib.org/articles/git-credentials.html
# Need to periodically add/update personal access token (PAT) for Github
# check token status and git details
usethis::gh_token_help()
usethis::git_sitrep()
# To generate a new PAT (takes you to a pre-filled form to create a new PAT)
usethis::create_github_token()
@mattgalbraith
mattgalbraith / ggplot_theme.R
Last active February 1, 2023 21:23
Default theme for ggplot
## Setting and modifying default theme for plots
theme_set(theme_gray(base_size=12, base_family="Arial") +
theme(
panel.border=element_rect(colour="black", fill="transparent"),
plot.title=element_text(face="bold", hjust=0),
axis.text=element_text(color="black", size=14),
axis.text.x=element_text(angle=0, hjust=0.5),
axis.ticks = element_line(color = "black"), # make sure tick marks are black
panel.background=element_blank(),
panel.grid=element_blank(),