Skip to content

Instantly share code, notes, and snippets.

View dantonnoriega's full-sized avatar

Danton Noriega-Goodwin dantonnoriega

View GitHub Profile
@dantonnoriega
dantonnoriega / rsc_user_details.R
Created September 2, 2021 18:54
A script for determining user details from RStudio API
# The CONNECT_SERVER URL must have a trailing slash.
connectServer <- "https://some-rsc-instance.com/"
# Save RSC API key to ~/.Renviron as SAFE_RSC_API_KEY_DEV
# - https://docs.rstudio.com/connect/1.7.4/user/cookbook.html
connectAPIKey <- Sys.getenv("RSC_API_KEY")
# Request a page of up to 25 usage records.
resp <- httr::GET(
file.path(connectServer, "__api__/v1/instrumentation/shiny/usage?limit=100"),
httr::add_headers(Authorization = paste("Key", connectAPIKey))
@dantonnoriega
dantonnoriega / docker-start-stop-rstudio.sh
Last active May 18, 2022 01:01
A helpful script to add to your .zshrc file that creates functions to start and stop docker containers and open up Rstudio in a browser.
# >>> DOCKER >>>
# start `n` docker containers in safari (default 1)
# option to start in current working directory `--here`; looks for .Rproj and hooks
# usage `docker-start [-n num_session] [--here] [<some_docker_image>] [<where_your_repos_live>]`
function docker-start () {
docker-start-usage() { echo "docker-start [-n num_session] [--here] [<some_docker_image>] [<where_your_repos_live>]" 1>&2; }
# https://stackoverflow.com/a/26920580
here_flag=0
# https://stackoverflow.com/a/7680682
@dantonnoriega
dantonnoriega / bash-postgres-function.sh
Last active February 10, 2021 04:09
Useful functions for parsing postgres commands
function psql_cmd() {
echo "$1" | psql -h $POSTGRES_HOST -U $POSTGRES_USERNAME -d $POSTGRES_DB -p $POSTGRES_PORT
}
# output data easy to ingest into R or python (includes header; can pass to `tee`)
function psql_psv() {
echo "$1" | psql -h $POSTGRES_HOST -U $POSTGRES_USERNAME -d $POSTGRES_DB -p $POSTGRES_PORT -A -F' | ' | grep -vE "\(\d+ row[s]\)"
}
function psql_csv() {
@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
@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 / 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 / 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 / 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 / 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 / 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