Skip to content

Instantly share code, notes, and snippets.

View dantonnoriega's full-sized avatar

Danton Noriega-Goodwin dantonnoriega

View GitHub Profile
@dantonnoriega
dantonnoriega / remote-ssh-plus-local-cluster-future-example.R
Last active January 26, 2021 17:54
Example setting up a mixed remote / local cluster using the `future` package. Includes simple examples of how to properly execute plan to maximize cores. Note that this does not show how important it is to have the same R version AND package versions across all nodes.
# set up ---------------------------
library(furrr)
# use multiple clusters
ssh_username <- 'drn'
remote_ssh_configs <- c('a', 'b', 'e') # names for remote server (found in ~/.ssh/config e.g. Host a)
local_comp <- Sys.info()[["nodename"]] # get local computer name
# build cluster ----------------------------------------------------------------
system(command = "ps -axc | grep ssh | awk '{print $1}' | sort -u | xargs kill")
@dantonnoriega
dantonnoriega / dirichletreg-greta-imultilogit-ex.R
Last active January 16, 2019 23:07
translate the stan code from https://arxiv.org/pdf/1808.06399.pdf into greta code (https://greta-dev.github.io/greta/index.html) but use new imultilogit() function over simplex_mat() --- about 15% speed increase.
# recreate the code from https://arxiv.org/pdf/1808.06399.pdf using greta (https://greta-dev.github.io/greta/)
library("DirichletReg")
Bld <- BloodSamples
Bld <- na.omit(Bld)
Bld$Smp <- DR_data(Bld[, 1:4])
# using greta
# !! requires the development version to run!
# devtools::install_github("greta-dev/greta@dev")
# convert data to matrix then greta data
@dantonnoriega
dantonnoriega / dark_widgetframe.R
Created January 9, 2019 04:27
my attempt to create a widgetframe with a dark background. hackish. seems to shift xaringan presentation oddly.
# remove any old tmp files
old_tmp_files <- list.files(pattern = '^tmp', include.dirs = TRUE, full.names = TRUE)
invisible(unlink(old_tmp_files, recursive = TRUE))
# create a dark widget frame via work around
dark_widgetframe <-
function(widget, background = '#666666FF', width = '100%', height = 420) {
file = tempfile(pattern = "tmp", tmpdir = '.', fileext = ".html")
selfcontained = FALSE
libdir = NULL
@dantonnoriega
dantonnoriega / epoch-time.R
Created July 24, 2019 23:13
convert epoch time to days, minutes, hh, mm
# e.g. epoch_time = 1562791262
as.POSIXct(1562791262, origin = '1970-01-01', tz = 'GMT')
#> [1] "2019-07-10 20:41:02 GMT"
as.numeric(as.Date(as.POSIXct(1562791262, origin = '1970-01-01', tz = 'GMT')))
#> [1] 18087
day = (epoch_time) %/% (3600 * 24) # day since 1970-01-01
min = (epoch_time %% (3600 * 24)) %/% 60 # minute of the day
@dantonnoriega
dantonnoriega / name-value-NA-search-reprex.R
Last active August 1, 2019 18:43
reprex of very slow named vector search when searching with NA
# NA key names using character vector
k1 <- c('a', 'b', NA_character_)
names(k1) <- k1
v1 <- sample(k1, 1e5, replace = TRUE, prob = c(.2, .2, .6))
# search with "null", but return NA value
k2 <- k1
names(k2) <- c('a', 'b', 'null') # new key name
# use "null" instead of NA;
v2 <- v1
@dantonnoriega
dantonnoriega / cool-bash-code.sh
Last active April 21, 2024 20:00
a collection of cool bash scripts
# cool bash codes
# search a directory for all lines that match a pattern (not perfect but useful) ------
## e.g. grep is searching for all lines matching "::" in `R/` to determine package calls
## -h hides the file names; -i ignores case
## sed -E uses regular expressions to search and match groups;
## we then sort and use -u
grep -hi :: -R R/* | sed -E 's/(.*)([ ]+[a-z]+::)(.*)/\2/g' | sort -u
# COUNT COLUMNS -----------------
@dantonnoriega
dantonnoriega / est-income-tax.R
Last active January 10, 2023 18:48
quick function to estimate income tax for 2022
est_income_tax <- function(x, jointly = F) {
# SOURCE
# https://www.irs.gov/newsroom/irs-provides-tax-inflation-adjustments-for-tax-year-2022
brackets <- c(0,10275,41775,89075,170050,215950,539900)
if(jointly) {
# brackets are double for jointly except for the last one
brackets <- 2 * brackets
brackets[length(brackets)] <- 647850
}
@dantonnoriega
dantonnoriega / quick-md2html.sh
Created April 21, 2020 00:15
a simple and quick pandoc function to convert an .md to .html
# quick convert any .md to .html
function md2html () {
/usr/local/bin/pandoc --standalone --template=https://raw.githubusercontent.com/tajmone/pandoc-goodies/master/templates/html5/github/GitHub.html5 --highlight-style=pygments --css=https://bootswatch.com/3/lumen/bootstrap.min.css --metadata pagetitle=$1 $1 -o ${1%.*}.html
}
@dantonnoriega
dantonnoriega / docker-start-stop.sh
Last active July 16, 2020 19:54
A script to launch an Rstudio docker container in Safari. Also, aliases to stop docker containers. Designed to go into ~/.zshrc files.
# start `n` docker containers in safari (default 1)
# usage `docker-start [-n num_session] <some_container>`
function docker-start () {
docker-start-usage() { echo "docker-start: [-n <number of session>] [<container>]" 1>&2; }
local OPTIND n o
while getopts "n:" o; do
case "${o}" in
n)
@dantonnoriega
dantonnoriega / Makevars
Created December 18, 2020 02:02
Makevars for installing data.table on Big Sur + Apple Silicon (M1; ARM)
# brew --prefix llvm
LLVM_LOC=/usr/local/opt/llvm
# brew --prefix gettext
GETTEXT=/usr/local/opt/gettext
# xcrun --show-sdk-path
XCBASE=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CC=$(LLVM_LOC)/bin/clang -fopenmp
CXX=$(LLVM_LOC)/bin/clang++ -fopenmp