Skip to content

Instantly share code, notes, and snippets.

@mschubert
Last active August 29, 2015 13:56

Revisions

  1. mschubert revised this gist Feb 26, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion unix.R
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,4 @@
    # 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
    c(1:5) %|% sum # use the R sum function
  2. mschubert revised this gist Feb 26, 2014. 1 changed file with 11 additions and 10 deletions.
    21 changes: 11 additions & 10 deletions unix.R
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,14 @@
    # a 'unix' operator in R to perform CLI operations similar to a pipe
    %unix% = function(x, command) {
    stopifnot(class(x) == 'character' || class(x) == 'numeric')
    system(command, input=x, intern=TRUE)
    # 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:

    # get file names from directories
    paths %unix% "grep -o [^/]+$"

    # remove entries containing the word 'foo'
    mywords %unix% "sed /foo/d"
    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
  3. mschubert revised this gist Feb 23, 2014. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions unix.R
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,7 @@
    # a 'unix' operator in R to perform CLI operations similar to a pipe
    `%unix%` = function(x, command) {
    %unix% = function(x, command) {
    stopifnot(class(x) == 'character' || class(x) == 'numeric')
    tryCatch(
    system(paste0(command, " <<< '", paste(x, collapse="\n"), "'"), intern=TRUE),
    error = function(e) NULL,
    warning = function(w) NULL)
    system(command, input=x, intern=TRUE)
    }

    # examples:
  4. mschubert revised this gist Feb 20, 2014. 1 changed file with 12 additions and 35 deletions.
    47 changes: 12 additions & 35 deletions unix.R
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,16 @@
    sed = function(x, pattern) {
    mynames = names(x)
    x = sapply(x, function(i) {
    type = class(i)
    result = system(paste("echo ", i, " | sed ", pattern, sep=""), intern=TRUE)
    class(result) = type
    result
    })
    names(x) = mynames
    x
    # a 'unix' operator in R to perform CLI operations similar to a pipe
    `%unix%` = function(x, command) {
    stopifnot(class(x) == 'character' || class(x) == 'numeric')
    tryCatch(
    system(paste0(command, " <<< '", paste(x, collapse="\n"), "'"), intern=TRUE),
    error = function(e) NULL,
    warning = function(w) NULL)
    }

    '%sed%' = function(x, pattern) {
    sed(x, pattern)
    }
    # examples:

    egrep = function(x, pattern) {
    stopifnot(class(pattern) == 'character' || class(pattern) == 'numeric')
    type = class(x)
    result = list()
    for (i in as.list(x)) {
    tryCatch({
    result = c(result,
    system(paste("echo ", i, " | grep ", pattern, sep=""), intern=TRUE))
    },
    error = function(e) {
    NULL
    },
    warning = function(w) {
    NULL
    })
    }
    class(result) = type
    result
    }
    # get file names from directories
    paths %unix% "grep -o [^/]+$"

    '%grep%' = function(x, pattern) {
    egrep(x, pattern)
    }
    # remove entries containing the word 'foo'
    mywords %unix% "sed /foo/d"
  5. mschubert created this gist Feb 17, 2014.
    39 changes: 39 additions & 0 deletions unix.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    sed = function(x, pattern) {
    mynames = names(x)
    x = sapply(x, function(i) {
    type = class(i)
    result = system(paste("echo ", i, " | sed ", pattern, sep=""), intern=TRUE)
    class(result) = type
    result
    })
    names(x) = mynames
    x
    }

    '%sed%' = function(x, pattern) {
    sed(x, pattern)
    }

    egrep = function(x, pattern) {
    stopifnot(class(pattern) == 'character' || class(pattern) == 'numeric')
    type = class(x)
    result = list()
    for (i in as.list(x)) {
    tryCatch({
    result = c(result,
    system(paste("echo ", i, " | grep ", pattern, sep=""), intern=TRUE))
    },
    error = function(e) {
    NULL
    },
    warning = function(w) {
    NULL
    })
    }
    class(result) = type
    result
    }

    '%grep%' = function(x, pattern) {
    egrep(x, pattern)
    }