Last active
August 29, 2015 13:56
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here’s my unsolicited code review.