This file contains hidden or 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
# 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") |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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 ----------------- |
This file contains hidden or 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
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 | |
} | |
This file contains hidden or 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
# 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 | |
} |
This file contains hidden or 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
# 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) |
This file contains hidden or 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
# 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 |