Skip to content

Instantly share code, notes, and snippets.

View daigotanaka's full-sized avatar
😎
https://handoff.cloud

Daigo Tanaka daigotanaka

😎
https://handoff.cloud
View GitHub Profile
@daigotanaka
daigotanaka / cfn.yml
Created June 23, 2020 22:57
Fixing AWS Fargate FailedInvocation error
# My Fargate tasks suddenly stopped working around 6/20/2020 after many months of stable executions.
# I tried using the following role and it worked.
# Reference: https://stackoverflow.com/a/51536083
#
# You can look for the ARN for the generated role by running:
# ROLE=aws iam list-roles --query 'Roles[?contains(RoleName, `CloudWatchEventECSRole`)].Arn | [0]'
#
# You can use the role when scheduling the task on Fargate
# aws events put-targets --profile $AWS_PROFILE \
# --rule $SCHEDULE_RULE_NAME \
@daigotanaka
daigotanaka / histogram.sql
Last active July 5, 2019 02:04
SQL histogram template
/* Histogram template
Edit Config and Input sections at the top of this query.
Config:
- bin_size: Width of the bin
- min_bin
- max_bin
Input:
@daigotanaka
daigotanaka / sprintf_.r
Last active January 11, 2017 05:26
sprintf that takes key-value inputs in the format
# %format% by G. Grothendieck from http://stackoverflow.com/a/17476306/3423480
library(gsubfn)
`%format%` <- function(fmt, list) {
pat <- "%\\(([^)]*)\\)"
fmt2 <- gsub(pat, "%", fmt)
list2 <- list[strapplyc(fmt, pat)[[1]]]
do.call("sprintf", c(fmt2, list2))
}
sprintf_ <- function(format, ...) {
@daigotanaka
daigotanaka / uninstallpkg
Last active March 6, 2016 19:10
Uninstall things you installed with a pkg on Mac
#!/bin/bash
# Uninstall things you installed with a pkg on Mac
# Tested on El Capitan
# Please run this at your own risk
# Read: http://superuser.com/questions/36567/how-do-i-uninstall-any-apple-pkg-package-file
if [ "$EUID" -ne 0 ]
then
echo "Please run as root"
@daigotanaka
daigotanaka / stepwise_vif.R
Created December 1, 2015 17:26
Stepwise VIF function
#stepwise VIF function
stepwiseVIF <- function(in_frame,thresh=10,trace=T){
require(fmsb)
if(class(in_frame) != "data.frame") in_frame <- data.frame(in_frame)
#get initial vif value for all comparisons of variables
vif_init <- NULL
for(val in names(in_frame)){
form_in <- formula(paste(val," ~ ."))
@daigotanaka
daigotanaka / multi-plot.R
Created November 25, 2015 00:45
Multi-plot
# Adopted from http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)
# And uploaded as gist for my personal use.
#
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
@daigotanaka
daigotanaka / cached_psql_query.r
Last active November 24, 2015 16:47
R: cache the values from PostgreSQL into RDS files for secondary accesses
# Caches the query result in rds file under `getwd()`/psql_cache.
# It assumes pg_host, pg_port, pg_dbname, pg_user, pg_password variables in the env.
# Remove the cache files if the update value is needed.
# Expire:
# positive integer n: Expire if the existing cache is older than n minutes
# 0: Expire now
# -1: Always use the existing cache if it exists
cachedPsqlQuery <- function(queryName, query, expire=-1, cachePath="./psql_cache") {
require(RPostgreSQL)
@daigotanaka
daigotanaka / subscriber-macro-example.clj
Last active August 29, 2015 14:27
Macro to define subscriber functions in ClojureScript
; the macro source
; To understand pub sub in clojure and ClojureScript, read this:
; https://yobriefca.se/blog/2014/06/04/publish-and-subscribe-with-core-dot-asyncs-pub-and-sub/
; define this in my-app/src/my-app/macros.clj
(ns my-app.macros)
(defmacro defsubfn
"Macro for subscriber function in pub-sub pattern"
[fnname channel body]
`(do
@daigotanaka
daigotanaka / rchart-helper.R
Last active August 29, 2015 14:14
rChart helper
# Modify stdout by rChart so I can customize tooltip and etc
require(rCharts)
renderChart = function (ct, chartId, include_assets = F, ...)
{
add_ext_widgets(ct$lib)
assetHTML <- ifelse(include_assets, paste(paste(add_lib_assets(ct$lib,
...), collapse = "\n"), "\n", add_style_(ct$params$width,
ct$params$height), collapse = "\n"), "")
chartDiv = render_template(ct$templates$chartDiv, list(chartId = chartId,
lib = ct$LIB$name, container = ct$container))
@daigotanaka
daigotanaka / rmd_caption_helper.R
Last active August 29, 2015 14:14
Rmarkdown caption tool
require(knitr)
render_caption = function(caption) {
paste('<p class="caption">', caption, "</p>", sep="")
}
knit_hooks$set(html.cap = function(before, options, envir) {
if(!before) {
render_caption(options$html.cap)
} else {
# Do nothing (or set isTable flag to render_caption at the top?