Skip to content

Instantly share code, notes, and snippets.

View tmasjc's full-sized avatar
Strange Loop

Thomas Jc tmasjc

Strange Loop
  • Kuala Lumpur
  • 03:49 (UTC +08:00)
View GitHub Profile
@tmasjc
tmasjc / gcv_http.R
Last active January 24, 2018 13:24
A Simple Post Request to Google Cloud Vision API in R #rstats #google
#-- BASED ON https://cloud.google.com/vision/docs/request#json_request_format --#
# image url on Google Cloud Storage
imgURI <- "gs://vacation_room/dublin-001.jpg"
# Requests are POST requests to
endpoint <- "https://vision.googleapis.com/v1/images:annotate"
#### remember to set api token in ``options``
# options(gcv_api_token = "YOUR_API_TOKEN")
@tmasjc
tmasjc / reactiveVal.R
Last active January 24, 2018 09:27
Reactive value in Shiny #shiny
library(shiny)
ui <- fluidPage(
column(12,
# toggle between normal or uniform distribution
actionButton("rnorm", "Normal"),
actionButton("runif", "Uniform")
),
column(12,
plotOutput("plot")
@tmasjc
tmasjc / get_rolling_sum.R
Last active February 20, 2019 14:06
Calculate sum for a rolling time window in R #rstats #zoo
# Generate dummy data -----------------------------------------------------
library(dplyr)
library(ggplot2)
library(lubridate)
start_day <- as.Date("2018-01-01")
# 2 Columns - Date & n
dat <- data.frame(date = start_day + days(1:90), n = round(runif(90, 1, 10)))
@tmasjc
tmasjc / repo_overview
Last active January 26, 2018 06:02
Common git commands put into a script to give an repo overview. #git #bash
#!/bin/bash
# Header
echo `date`
if [ -d .git ];then
echo -e "\n\033[0;42m **** Current Status ****\033[0m\n"
git status -s
echo -e "\n\033[0;43m **** Pending Issue ****\033[0m\n"
@tmasjc
tmasjc / awsFunctions
Created January 29, 2018 10:18
Simple shortcut to replace common AWS commands.
#!/bin/bash
function _awsSwitchProfile() {
# Check if argument exists
if [ -z $1 ]; then echo "Usage: _awsSwitchProfile <profilename>"; return; fi
# Check if profile exists
exists="$(aws configure get aws_access_key_id --profile $1)"
@tmasjc
tmasjc / rbucket
Last active December 10, 2018 05:07
Queue item onto AWS S3. #aws #bash
#!/bin/bash
BUCKET="rdata"
if [[ -z $1 ]]; then
aws s3 ls s3://$BUCKET
elif [ $1 = "-u" ];
then
echo "Encrypting data..."
OBJ=$(ccrypt -e $2 -K "qwer" -f -q)
@tmasjc
tmasjc / unwto.R
Created February 2, 2018 13:27
Explore chinese outbound tourism data from UNWTO. #rstats #unwto
# China Outbound Tourism
library(readxl)
library(tidyr)
library(ggplot2)
library(broom)
# read data
cot <- read_excel("~/Downloads/outbound_tourism_china.xlsx", skip = 5)
# mod columns name
colnames(cot) <- c("country", "series", 1995:2016, "pct_chg_16_15")
@tmasjc
tmasjc / xrates.R
Last active June 27, 2018 16:29
Iterate over x rates to scrap currency. #purrr
# Reference
# http://www.x-rates.com/average/?from=CNY&to=EUR&amount=100&year=2017
library(rvest)
#
base <- "http://www.x-rates.com/average/?amount=100"
# Currency
cur <- c("USD", "CNY")
@tmasjc
tmasjc / rename_files
Created February 8, 2018 05:05
Rename multiple files at once using regex. #bash
#!/usr/bin/env Rscript --vanilla
args <- commandArgs(trailingOnly = TRUE)
# List all files
items <- list.files(path = ".")
# See which matches argument (regex)
old <- items[grep(items, pattern = args[1])]
@tmasjc
tmasjc / cot_animate.R
Created February 8, 2018 12:02
Animate foreign exchange rate x outbound Chinese tourisism. #rstats #plotly
library(readxl)
library(tidyr)
library(ggplot2)
library(broom)
library(rvest)
library(plotly)
# Import data -------------------------------------------------------------