Skip to content

Instantly share code, notes, and snippets.

View dantonnoriega's full-sized avatar

Danton Noriega-Goodwin dantonnoriega

View GitHub Profile
@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 / 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 / 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 / gitlfs-github-api-content-extraction.sh
Created September 2, 2021 19:00
Steps to use the git-lfs Batch API to download content from GitHub (enterprise) when the raw content is actually on git-lfs
# github + git lfs api contenct extraction
# GOAL: pull a large file that was sent to git lfs (cannot download directly from github.com)
## git-lfs API: https://github.com/git-lfs/git-lfs/tree/main/docs/api
## github API: https://docs.github.com/en/rest/reference/repos#get-repository-content
OWNER=dantonnoriega
REPO=some-repo
BRANCH=develop
# -----------------
@dantonnoriega
dantonnoriega / carryforward.R
Created September 2, 2021 19:02
base R way to carry forward data
carryforward <- function(x, blank = is.na) {
# SOURCE: https://stackoverflow.com/a/32536507/3987905
# Find the values
if (is.function(blank)) {
isnotblank <- !blank(x)
} else {
isnotblank <- x != blank
}
# Fill down
x[which(isnotblank)][cumsum(isnotblank)]
@dantonnoriega
dantonnoriega / random-datatable-if_any-testing.R
Created February 10, 2022 23:09
a simple script that tests out different was of implementing the `if_any` logic in native data.table
library(tidyverse)
dat <- as_tibble(mtcars) %>%
mutate(vs = as.character(vs),
am = as.character(am)) #just to make some non-numeric
dd0 <- dat %>%
select(where(is_numeric)) %>%
filter(if_any(disp:wt, ~ .x > 100))
dd0
library(data.table)
@dantonnoriega
dantonnoriega / rsc_user_activity_reprex.R
Last active February 15, 2022 20:53
a script for determining the number of active users in some RSC instance
# The CONNECT_SERVER URL must have a trailing slash.
connectServer <- "https://some-rsc-url.com/"
# Save RSC API key to ~/.Renviron as RSC_API_KEY
# - 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 / rsc_app_specific_total_and_unique_user_visits_reprex.R
Created February 15, 2022 20:52
an example of pulling total and unique visits for a specific Rstudio Connect application Raw
# The CONNECT_SERVER URL must have a trailing slash.
connectServer <- "https://some-rsc-endpont.com"
connectAPIKey <- Sys.getenv("RSC_API_KEY")
# the content GUID of interest
content_guid_3pc <- "0a0b1c2d-1234-4321-a1c2-5678aaa9d9bb" # I made this one up
resp <- httr::GET(
paste0(connectServer, "__api__/v1/instrumentation/shiny/usage?limit=100"),
httr::add_headers(Authorization = paste("Key", connectAPIKey)),
@dantonnoriega
dantonnoriega / databricks-jdbc-instructions.md
Created March 5, 2022 02:09
instructions for using JDBC SIMBA drivers in R to connect to Databricks

Databricks - JDBC

This is the preferred method if you're collecting small objects from the Spark Cluster because it is otherwise easy to set up, doesn't appear to cause many "out of memory" errors, and requires fewer dependencies.

Setup

  1. Install and configure Java.
    • Install Java 8

brew tap AdoptOpenJDK/openjdk

@dantonnoriega
dantonnoriega / zsh_history_cat.md
Last active October 26, 2022 05:45
how to append old zsh_history files into a single one, keeping things in order, and updating $HISTFILE

Helpful Resources

WARNING: using fc -p will replace your current history file with a blank one. Tread carefully.

GOAL: merge (or concat or join or append) a set of old zsh_history files into a single one.

Imagine you have 3 zsh history files, with timestamps from oldest to newest: zsh_history0, zsh_history1, zsh_history2.