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 / Developer functions in R
Last active April 14, 2021 08:55
[List of R developer tools] #R
# Regular R CMD check
devtools::check()
# Check R-hub
devtools::check_rhub()
# Cross-platform R CRAN check
rhub::check_for_cran()
# Return a character vector with the names of dependencies - CRAN only
@martinctc
martinctc / date expansion.R
Created February 23, 2021 23:33
[Date Expansion using Sundays] #R
## Date expansion
validweeks <-
unique(clean_wbq_totalview$Date) %>%
purrr::map(function(x){
seq(x, x + 6, by = "days")
}) %>%
purrr::reduce(c)
@martinctc
martinctc / import_to_fst.R
Last active February 10, 2021 12:53
[Import to FST] #R
import_to_fst <- function(path){
temp_df <- import_wpa(path)
path_fst <- gsub(pattern = "csv$",
replacement = "fst",
x = path)
fst::write_fst(temp_df, path_fst)
@martinctc
martinctc / jittered_ordinal_points.R
Created January 3, 2021 17:54
[Visualize ordinal data over time with jittered points] #R
library(tidyverse)
10:30 %>%
map(function(x){
prob_v <-
c(x,
100 - 2*x,
100 - x - (100 - 2*x))
@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)