Skip to content

Instantly share code, notes, and snippets.

@mschubert
Last active August 29, 2015 13:56
Show Gist options
  • Save mschubert/9061206 to your computer and use it in GitHub Desktop.
Save mschubert/9061206 to your computer and use it in GitHub Desktop.
A unix-like pipe operator in R to perform CLI operations (or call R functions)
# a unix-like pipe operator in R to perform CLI operations (or call R functions)
`%|%` = function(x, command) {
if (class(command) == 'function') {
command(x)
} else {
stopifnot(class(x) %in% c('character', 'numeric', 'integer'))
system(command, input=as.character(x), intern=TRUE)
}
}
# examples:
paths %|% "grep -o [^/]+$" # get file names from directories
mywords %|% "sed /foo/d" # remove entries containing the word 'foo'
c(1:5) %|% sum # use the R sum function
@klmr
Copy link

klmr commented Feb 18, 2014

Here’s my unsolicited code review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment