## Collection of my shared R snippets:
# Author: https://dchakro.com
# Github: https://github.com/dchakro
# Code us provided as is, without any warranty.

snippet beginr
	## Description: ${1:Describe}
	`r paste("# ----", format(Sys.time(), "%a, %b %d, %Y @ %H:%M"), "----\n")`
	rm(list = ls())
	gc()
	library(data.table)
	today <- format(Sys.Date(),format = "%Y-%m-%d")
	slug <- "${2:SubFolderName}"
	workingDirectory <- paste0(today,"_",slug)
	setwd("~/ANALYSIS/")
	ifelse(test = dir.exists(workingDirectory), 
		yes = message(paste0(workingDirectory," exists...")),
		no = dir.create(workingDirectory))
	setwd(workingDirectory)
	rm(today,workingDirectory,slug)
	${0}

snippet comment_date
	`r paste("#", date(), "----------------------------\n")`
	${0}

snippet rmObject
	rm(list=ls()[!ls() %in% c("${1:Obj1}","${2:Fun2}")])
	${0}

snippet DFrmColumn
	${1:DF}[,!colnames(${1}) %in% ${2:columnsToRemove}]
	${0}
	
snippet DTrmColumn
	${1:DF}[, c("${2:Col1}","${3:Col2}") := NULL]
	${0}
	
snippet changeColumnType
	# define columns to change
	changeCols  <- c("${1:col1}", "${1:col2}") 
	${3:DT}[,(changeCols):= lapply(.SD, as.factor), .SDcols = changeCols]

snippet mat
	matrix(${1:data}, nrow = ${2:rows}, ncol = ${3:cols})
	${0}

snippet elif
	if (${1:condition}) {
		${0}
	}	else if (${2:condition}) {
		${0}
	}	else {
		${0}
	}

snippet fun
	${1:name} <- function(${2:variables}) {
		## Description:		
		## Input:
		## Output:
		
		${0}
	}

snippet readTable
	myDT <- data.table::fread(file = ${1:fileName},
				sep = "${2:delimiter}",
				skip = 0,
				header = F,
				stringsAsFactors = F,
				showProgress = T,
				nThread = `r as.integer(system(command = "sysctl -n hw.physicalcpu", intern = T))`)
	${0}

snippet writeTable
	write.table(
	  x = ${1:TableVar},
	  file = ${2:fileName},
	  quote = F,
	  sep = "${3:delimiter}",
	  row.names = F,
	  col.names = T
	)${0}   

snippet gsubstitute
	gsub("${1:Find}","${2:Replace}",${3:variable}, fixed = T)
	${0}

snippet replaceText
	regmatches(${1:String}, gregexpr(pattern="${2:Find}", text =${1}, fixed = T) <- "${3:newText}"
	${0}

snippet not%
	'%nin%' <- Negate('%in%')
	${1:var1} %nin% ${2:var2}

snippet split
	data.table::as.data.table(stringi::stri_split_fixed(str = ${1:String}, 
								pattern = "${2:pattern}", 
								simplify = T))
	${0}

snippet find_matches
	regmatches(${1:String}, gregexpr(pattern="${2:Find}",text =${1}, fixed = T)
	${0}

snippet roundUp 
	source("https://raw.githubusercontent.com/dchakro/shared_Rscripts/master/roundUp.R")
	roundUp(x = ${1:999}, to = ${2:10})
	${0}

snippet ggtheme
	library(ggplot2)
	source('https://raw.githubusercontent.com/dchakro/ggplot_themes/master/DC_theme_generator.R')
	customtheme <- DC_theme_generator(type = "L")
	
	${0}

snippet ggarea
	ggplot(data=${1:longDF}, aes(x=${2:orderedVar2}, y=${3:plottingValue})) + 
		geom_area(alpha=1, color="black", aes(fill=${4:fill.order}), position=position_fill(reverse = T)) + 
		ylab("Percentage of total")
	
	${0}

snippet percentScale
	scale_y_continuous(breaks = seq(0, 1, by=0.25),labels = scales::percent,limits = c(0,1))

snippet foreach
	library(doParallel)
	myCluster <- makeCluster(4, type = "FORK",useXDR=F,.combine=cbind)
	print(myCluster)
	registerDoParallel(myCluster)
	
	results <- foreach(mut = ${1:items},.combine = cbind, .inorder = F) %dopar% {
	  # Description: ${2:Describe}
	  # Code for process
	  
	  # Returning values
	  return(list(var1, var2))
	}
	stopCluster(myCluster)
	
	${0}

snippet mclApply_block
	parallel::mclapply(
	  X = ${1:listVar},
	  FUN = function(X)
	    ${2:functionName}(X),
	  mc.cores = parallel::detectCores()
	)
	${0}

snippet multiplePlots
	## Multiple plots made in parallel and compiled into a grid
	myplots <-
	parallel::mclapply(
	  X = ${1:parameters},
	  FUN = function(i)
	    plottingFunction(subDT = DF[, c(1, i)]),
	  mc.cores = parallel::detectCores()
	)
	
	ggplot2::ggsave(
	filename = "${2:fileName}.pdf",
	plot = gridExtra::marrangeGrob(myplots, 
				layout_matrix = matrix(data = 1:16, nrow = 4, ncol = 4,	byrow = T),
				as.table = F),
	width = ${3:width},
	height = ${4:height} )
	
	${0}

# Customized tryCatch block
snippet tryc
	tryCatch(expr = {
		# Code that can throw and error
	
	}, error = function (e){
		message("Error Message")
		# Actions to take
	
	}, finally = {
		print("Great Success!!")	
	}) 

snippet installPackages
	to_install <- c("${1:pack1}")	## Set packages to install here and run
	if (unname(Sys.info()["sysname"]) == "Darwin") {
		CPU_cores <- as.integer(system(command = "sysctl -n hw.physicalcpu", intern = T))
		## For logical cores:
		# CPU_cores <- system("sysctl -n hw.ncpu")
	} else if (unname(Sys.info()["sysname"]) == "Linux") {
		CPU_cores <- as.integer(system(command = "nproc", intern = T))
	}
	utils::setRepositories(ind = c(1, 2, 3))
	missing_packages <-
		to_install[!(to_install %in% installed.packages()[, "Package"])]
	if (length(missing_packages))
		install.packages(missing_packages, type = "source", INSTALL_opts = "--byte-compile", Ncpus = CPU_cores)
	rm(missing_packages, to_install, CPU_cores)
	
	${0}
	
snippet bench
	bmark <- microbenchmark(
	"${1:method1}" = {
	  	# Describe actions
	 }
	 },
	"${2:method2}" = {
	  # Describe actions
	  }
	}, times = 5 )
	source("https://raw.githubusercontent.com/dchakro/shared_Rscripts/master/summarySE.R")
	DF <- summarySE(bmark,measurevar = "time",groupvars = "expr",statistic = "mean")
	
	${0}


snippet parallelRDS
	source('https://gist.githubusercontent.com/dchakro/8b1e97ba6853563dd0bb5b7be2317692/raw/parallelRDS.R')
	${0}
	rm(readRDS.gz,saveRDS.gz,writeRDS,loadRDS)
	
snippet objectSize
	format(object.size(${1:Var1}), units = "MB", standard = "SI")
	
snippet unMelt
	mat <- data.table::dcast.data.table(${1:myDT}, ${2:row} ~ ${3:col}, value.var = "${4:value}")
	${0}
		
snippet gptAPI
	Sys.setenv(OPENAI_API_KEY = "sk-YOURKEYHERE")
	options(gptstudio.max_tokens = 200)
	${0}