Skip to content

Instantly share code, notes, and snippets.

View russHyde's full-sized avatar
💭
At home

Russ Hyde russHyde

💭
At home
View GitHub Profile
@russHyde
russHyde / gist:cdb6e5b5ee70f31a8a8b4eb6aabc63d0
Created August 9, 2023 14:00
Local lintr hook for pre-commit
# An issue with the lintr pre-commit hook is that packages are not fully loaded
# prior to linting, so you can get lots of object-usage lints when an object from
# your dev packages namespace is used.
#
# The following ensures your dev package is loaded before linting the changed
# files within it. It requires {lintr} to be installed in your dev environment.
# This differs from most pre-commit hooks (which usually run in a hook-specific
# renv environment)
#
# All available hooks: https://pre-commit.com/hooks.html
@russHyde
russHyde / .gitconfig
Last active August 4, 2022 09:17
Get in-branch changes since separating from another git branch
[alias]
biff = "!f() { \
# all changes since branch/hash $2 separated from branch/hash $1 \
old=${1:-main}; new=${2:-HEAD}; base=$(git merge-base ${old} ${new}); \
git diff ${base} ${new}; \
@russHyde
russHyde / jest_runners.sh
Last active May 27, 2022 14:15
Notes on puppeteer / jest tests
# Run the test file multiple times, break when it fails to pass
TEST_PATTERN="xxx"
for i in {1..10}; do
npx jest ${TEST_PATTERN} --silent --runInBand;
if [[ "$?" != 0 ]]; then echo "Failed after $i attempts" && break; fi;
done
# compare the object-usage lints obtained before/after a given PR
# the PR is always compared against master
library(magrittr)
library(dplyr)
library(purrr)
library(tibble)
# In lintr directory
@russHyde
russHyde / optparse_with_trailing_args.R
Created April 22, 2020 15:26
R {optparse} using trailing arguments
# My rscript template assumes all command line arguments are in option-form
# `Rscript <script_name> --logical_option --some_flag some_value --some_other_flag some_other_value`
#
# When you want to pass a variable number of files into your script, the option-form doesn't work well
# so it's better to use trailing arguments
# `Rscript <script_name> --some_option --flag with_value trailing1 trailing2 trailing3 ...`
#
# optparse works well in the latter case:
library(magrittr)
# My title
```{r}
tables <- list(a = iris[1:10, ], b = iris[11:20, ])
```
```{r}
#' When makinig an Rmarkdown, this function will print all the data.frames
#' in a list
#'
@russHyde
russHyde / .lintr
Last active February 6, 2024 17:46
An illustration of how to write a "lintr" config file
linters: with_defaults(
# By using the `with_defaults` function in .lintr to set up the linters for your package / project,
# you are telling {lintr}: start with this named-list of linters (given in the argument `default`,
# see below) and modify any that I specify.
# Here, that means you want to use the list `default_linters`, but modify that list.
#
# See `names(lintr::default_linters)` for the contents of the `default_linters` used here.
#
# You could alternatively
#
@russHyde
russHyde / rscript_template.R
Last active May 19, 2020 14:20
R script template using optparse
#!/usr/bin/env Rscript
###############################################################################
# Package names for non-test dependencies
pkgs <- c(
# CRAN
"magrittr",
"optparse",
# Bioconductor