Skip to content

Instantly share code, notes, and snippets.

View dwhdai's full-sized avatar
:shipit:

David Dai dwhdai

:shipit:
View GitHub Profile
@dwhdai
dwhdai / docker-help.md
Created February 14, 2020 00:37 — forked from bradtraversy/docker-help.md
Docker Commands, Help & Tips

Docker Commands, Help & Tips

Show commands & management commands

$ docker

Docker version info

@dwhdai
dwhdai / prompt.R
Last active February 3, 2020 22:34
Garrick's R prompt, slightly modified
# Set R prompt to git branch name
# requires: devtools::install_github("gaborcsardi/prompt")
.prompt_grk <- function(...) {
dir <- if (rstudioapi::hasFun("getActiveProject") &&
!is.null(rstudioapi::getActiveProject())) {
basename(rstudioapi::getActiveProject())
} else {
basename(getwd())
}
captcha <- function(x){
#input 1122
#return 3
#input 1111
#return 4
first_digit <- substr(x,1,1)
x <- paste0(x, first_digit)
@dwhdai
dwhdai / percent.R
Created September 30, 2019 16:03
Group percentage of categorical variable
percent <- function(data, group){
group <- enquo(group)
data %>%
group_by(!!group) %>%
summarise(percentage = n()/nrow(data))
}
@dwhdai
dwhdai / send_outlook_mail.R
Last active May 31, 2019 14:42
Sending Outlook email within R
# Install RDCOMClient package
install.packages("RDCOMClient", repos = "http://www.omegahat.net/R")
library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
## configure email parameter
outMail[["To"]] = "destination@email"
@dwhdai
dwhdai / ggplot_scales_labels.R
Created May 17, 2019 21:00
ggplot2 quick axis labels using scales
plot <- ggplot(...)
# Log 10 transformation
plot +
scale_x_continuous(trans = "log10",
breaks = scales::trans_breaks("log10", function(x)10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x)))
@dwhdai
dwhdai / archive_file.R
Created May 16, 2019 22:14
This script can be used to automate moving/archiving files
# List all files in directory which contains the file to be archived
files <- list.files("/path/to/file/")
# A distinguishing substring that allows you to identify the file to be archived
substring <- "some_substring"
# Select the file based on distinguishing substring
old_file <- files[which(stringr::str_detect(files, substring))]
# Move report to archive folder
@dwhdai
dwhdai / taskscheduler.R
Created May 16, 2019 22:09
Schedule an R script to run
# Path to script that should be sourced
rscript <- "path_to_script/script_name.R"
# To create the task
taskscheduleR::taskscheduler_create(taskname = basename(rscript),
rscript = rscript,
schedule = c("DAILY"), # Set schedule frequency
starttime = "08:00", # Set schedule start time
startdate = format(as.Date("2019-04-26"), "%m/%d/%Y") # Set schedule start date
)
@dwhdai
dwhdai / foreach_logger.R
Created April 10, 2019 20:27
Export log file for parallel processing in R
# This is a simple example of how you can create an external log file for monitoring parallel jobs in R, using doSNOW and foreach
library(doSNOW)
library(foreach)
n <- 10
cl <- makePSOCKcluster(5)
registerDoSNOW(cl)
writeLines(c(""), "log.txt")
@dwhdai
dwhdai / read_dir.R
Last active March 29, 2019 16:47
Read a directory of similar data files and merging them in R
files <- dir("/path/to/your/files", full.names = T)
df <- files %>%
map(readr::read_csv) %>%
reduce(bind_rows)