Skip to content

Instantly share code, notes, and snippets.

View bfatemi's full-sized avatar
🎯
In my zone

Bobby Fatemi bfatemi

🎯
In my zone
View GitHub Profile
@bfatemi
bfatemi / rowConcat.R
Created March 14, 2016 23:15
Concat pairwise (across rows "horizontally")
names <- list(a = c("firstname1","firstname2"),
b = c("lastname1","lastname2"))
eval( substitute( paste(a, b), names ))
@bfatemi
bfatemi / trapWindowSize.sh
Created March 16, 2016 05:07
Snippet to continuously grab and display the size of the console window
#!/bin/bash
trap 'get_window_size' WINCH # trap when a user has resized the window
get_window_size() {
_WINDOW_X=`tput lines`
_WINDOW_Y=`tput cols`
echo "X: $_WINDOW_X"
echo "Y: $_WINDOW_Y"
return 0
@bfatemi
bfatemi / ReadOneKey.sh
Created March 16, 2016 05:08
Snippet to trap a keystroke. Press any key to continue...
#!/bin/bash
readOne () {
tput smso
echo "Press any key to return \c"
tput rmso
oldstty=`stty -g`
stty -icanon -echo min 1 time 0
dd bs=1 count=1 >/dev/null 2>&1
stty "$oldstty"
@bfatemi
bfatemi / dowhileactive.sh
Created March 16, 2016 05:09
Snippet to do something while a particular process is active
apt-get update |pv -tN "Updating" > /dev/null & pid=$!
trap "kill $pid 2> /dev/null" EXIT
while kill -0 $pid 2> /dev/null; do
tput cup 10 10
done
trap - EXIT
@bfatemi
bfatemi / getSizeandDepends.sh
Created March 16, 2016 05:11
Snippet to get the dependencies and size of those dependencies for any package install
#!/bin/bash
LIST=$1
PACKAGE_LIST=$(dpkg --get-selections | awk '{ print $1 }' | grep -v -e "-dbg" | cut -f1 -d":")
getsize () {
size=$(apt-cache --no-all-versions show $1 | grep Installed-Size | awk '{ print $2 }')
((NEEDED+=$size))
}
@bfatemi
bfatemi / matchArg.R
Created March 22, 2016 22:29
Example of match.arg()
require(stats)
## Extends the example for 'switch'
center <- function(x, type = c("mean", "median", "trimmed")) {
type <- match.arg(type)
switch(type,
mean = mean(x),
median = median(x),
trimmed = mean(x, trim = .1))
}
@bfatemi
bfatemi / accessFnStack.R
Created March 23, 2016 17:45
Example of accessing the function call stack
### Example of accessing the function call stack
IsHungry <- function(){
e <- substitute(cat(sub("\\(.*\\)", " ", deparse(sys.calls()[[i]]))))
Bob <- function(n){
Loves <- function(n){
Burgers <- function(n){
TheEnd <- function(n){
invisible(sapply(1:n, function(i) eval(e, list(i))))
}
TheEnd(sys.nframe())
@bfatemi
bfatemi / getDays.R
Created March 27, 2016 20:16
Get a vector containing the sequence of days beginning with today, and ending with year end.
today <- Sys.Date()
yearEnd <- as.Date(paste0(year(today), "-12-31"))
daysVec <- seq(today, yearEnd, by=1)
@bfatemi
bfatemi / Namemasking.R
Last active April 11, 2016 17:47
Name masking - Preserving definition environment
j <- function(x) {
y <- 2 # Define y
# j returns this function
function() {
c(x, y)
}
}
@bfatemi
bfatemi / RemoveNAallCols
Created April 29, 2016 00:44
One line to keep rows such that there are no NAs across all columns
# One line to keep rows such that there are no NAs across all columns
dt[Reduce("&", lapply(dt, function(i) !is.na(i)))]