Skip to content

Instantly share code, notes, and snippets.

View martinctc's full-sized avatar
🔥
Packaging up my workflows

Martin Chan martinctc

🔥
Packaging up my workflows
View GitHub Profile
@martinctc
martinctc / geom jitter labels.R
Created October 20, 2020 21:34
[geom label or text to align with geom_jitter()] #R
library(tidyverse)
library(hkdatasets)
hk_accidents %>% glimpse()
## These charts look ridiculous but at least the jitter points are aligned with the text
hk_accidents %>%
filter(Year == 2017) %>%
ggplot(aes(x = Natural_Light , y = No__of_Vehicles_Involved)) +
@martinctc
martinctc / regex_in_r.R
Created July 17, 2020 09:09
[Regular Expression matching in #R Notes]
# Whole word matching
grepl("\\<apple\\>", "pineapple", ignore.case = TRUE) # No match
grepl("apple", "pineapple", ignore.case = TRUE) # Match
@martinctc
martinctc / aws-polly.txt
Created July 10, 2020 17:37
[AWS Polly Synthesize Speech] #aws
aws polly synthesize-speech ^
--output-format mp3 ^
--voice-id Joanna ^
--text "Whose woods these are I think I know. His house is in the village though; He will not see me stopping here To watch his woods fill up with snow. My little horse must think it queer To stop without a farmhouse near Between the woods and frozen lake The darkest evening of the year. He gives his harness bells a shake To ask if there is some mistake. The only other sound’s the sweep Of easy wind and downy flake. The woods are lovely, dark and deep, But I have promises to keep, And miles to go before I sleep, And miles to go before I sleep." ^
robertfrost.mp3
@martinctc
martinctc / ffmpeg.txt
Created July 8, 2020 19:09
[ffmpeg bash notes - from mp4 to GIF]
# Just convert
ffmpeg -ss 0 -i test.mp4 -f gif output.gif
# Double speed
ffmpeg -ss 0 -i test.mp4 -filter:v "setpts=0.5*PTS" -f gif output.gif
# Quadruple speed
ffmpeg -ss 0 -i test.mp4 -filter:v "setpts=0.25*PTS" -f gif output.gif
@martinctc
martinctc / DC Data Clean.R
Created July 5, 2020 09:26
[Clean up Google Sheets - HKDC Project] #R
library(tidyverse)
library(googlesheets4)
sheet_url <- "https://docs.google.com/spreadsheets/d/1usk9Q-5lA4bL_z6KXpUohc_2x_KhDgLxtm-YEtim_yk/"
# define google sheets ----------------------------------------------------
## may consider using purrr once we get to more sheets
data_master_cnw <- googlesheets4::read_sheet(ss = sheet_url, sheet = "Central & Western")
data_master_wanchai <- googlesheets4::read_sheet(ss = sheet_url, sheet = "Wan Chai")
@martinctc
martinctc / Assigning objects in a list to Global Environment.R
Created June 18, 2020 13:04
[Assigning objects in a list to Global Environment] #R
#### Assigning objects in a list to Global Environment ####
## 1. Put data frames into a named list
p_list <-
list("data1" = iris,
"data2" = mtcars,
"data3" = mtcars)
## 2. Extract the names and save into an object
p_list_names <- names(p_list)
@martinctc
martinctc / pkg_summary.R
Last active July 25, 2020 08:08
[Run a summary of functions and the associated description from a loaded package] #R
#' Run a summary of functions and the associated description
#' for a loaded package
#'
#' @param package_name String providing the name of the loaded package, e.g. surveytoolbox
#'
#' @examples
#' \dontrun{
#' pkg_summary("surveytoolbox")
#' pkg_summary("wpa")
#' pkg_summary("rwa")
@martinctc
martinctc / minimal_html_report.R
Created April 27, 2020 15:48
[Minimal Function to generate HTML report using RMarkdown] #R
## This is a minimal example to show how you can create a
## function that generates HTML reports using RMarkdown.
##
## This uses a list-pmap workflow so you can input lists
## as arguments and dynamically produce a RMarkdown that
## is only limited by the length of your lists.
##
## The example below outputs a table and two plots from
## `iris`.
@martinctc
martinctc / ForceNetwork_example.R
Created April 22, 2020 09:53
[ForceNetwork network example with {networkD3}] #R
library(tidyverse)
library(networkD3)
## Nodes data frame describing all the nodes in the network
## The first entry in nodes dataframe is node 0, the next entry is node 1 and so on.
## The nodes dataframe must be sorted according to this sequence.
## This is the only way to tie the nodes dataframe to the links dataframe.
TestNodes <- data.frame(name = c("Alpha",
"Beta",
"Cat",
@martinctc
martinctc / lolololololol.R
Created April 10, 2020 11:40
[Generate lolololololol in #R]
paste(rep("l", exp(10)), collapse = "o")