Skip to content

Instantly share code, notes, and snippets.

@adilapapaya
Forked from skranz/s_dplyr
Last active August 29, 2015 14:19
Show Gist options
  • Save adilapapaya/2ff9178499f8b50c36e3 to your computer and use it in GitHub Desktop.
Save adilapapaya/2ff9178499f8b50c36e3 to your computer and use it in GitHub Desktop.

Revisions

  1. adilapapaya revised this gist Apr 26, 2015. 1 changed file with 34 additions and 0 deletions.
    34 changes: 34 additions & 0 deletions examples.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # Examples
    library(dplyr)

    source("s_dplyr.R");
    # Original usage of dplyr
    mtcars %>%
    filter(gear == 3,cyl == 8) %>%
    select(mpg, cyl, hp:vs)

    # Select user specified cols.
    # Note that you can have a vector of strings
    # or a single string separated by ',' or a mixture of both
    cols = c("mpg","cyl, hp:vs")
    mtcars %>%
    filter(gear == 3,cyl == 8) %>%
    s_select(cols)

    # Filter using a string
    col = "gear"
    mtcars %>%
    s_filter(paste0(col,"==3"), "cyl==8" ) %>%
    select(mpg, cyl, hp:vs)

    # Arrange without using %>%
    s_arrange(mtcars, "-mpg, gear, carb")

    # group_by and summarise with strings
    mtcars %>%
    s_group_by("cyl") %>%
    s_summarise("mean(disp), max(disp)")

    mtcars %>%
    s_group_by("cyl") %>%
    s_summarise(paste(paste0("mean(", colnames(mtcars),")"), collapse=","))
  2. adilapapaya revised this gist Apr 26, 2015. 1 changed file with 6 additions and 35 deletions.
    41 changes: 6 additions & 35 deletions s_dplyr.R
    Original file line number Diff line number Diff line change
    @@ -6,37 +6,37 @@
    #' Modified version of dplyr's filter that uses string arguments
    #' @export
    s_filter = function(.data, ...) {
    eval.string.dplyr(.data,"filter", ...)
    eval.string.dplyr(.data,"dplyr::filter", ...)
    }

    #' Modified version of dplyr's select that uses string arguments
    #' @export
    s_select = function(.data, ...) {
    eval.string.dplyr(.data,"select", ...)
    eval.string.dplyr(.data,"dplyr::select", ...)
    }

    #' Modified version of dplyr's arrange that uses string arguments
    #' @export
    s_arrange = function(.data, ...) {
    eval.string.dplyr(.data,"arrange", ...)
    eval.string.dplyr(.data,"dplyr::arrange", ...)
    }

    #' Modified version of dplyr's arrange that uses string arguments
    #' @export
    s_mutate = function(.data, ...) {
    eval.string.dplyr(.data,"mutate", ...)
    eval.string.dplyr(.data,"dplyr::mutate", ...)
    }

    #' Modified version of dplyr's summarise that uses string arguments
    #' @export
    s_summarise = function(.data, ...) {
    eval.string.dplyr(.data,"summarise", ...)
    eval.string.dplyr(.data,"dplyr::summarise", ...)
    }

    #' Modified version of dplyr's group_by that uses string arguments
    #' @export
    s_group_by = function(.data, ...) {
    eval.string.dplyr(.data,"group_by", ...)
    eval.string.dplyr(.data,"dplyr::group_by", ...)
    }

    #' Internal function used by s_filter, s_select etc.
    @@ -48,34 +48,5 @@ eval.string.dplyr = function(.data, .fun.name, ...) {
    df
    }

    # Examples
    library(dplyr)

    # Original usage of dplyr
    mtcars %>%
    filter(gear == 3,cyl == 8) %>%
    select(mpg, cyl, hp:vs)

    # Select user specified cols.
    # Note that you can have a vector of strings
    # or a single string separated by ',' or a mixture of both
    cols = c("mpg","cyl, hp:vs")
    mtcars %>%
    filter(gear == 3,cyl == 8) %>%
    s_select(cols)

    # Filter using a string
    col = "gear"
    mtcars %>%
    s_filter(paste0(col,"==3"), "cyl==8" ) %>%
    select(mpg, cyl, hp:vs)

    # Arrange without using %>%
    s_arrange(mtcars, "-mpg, gear, carb")

    # group_by and summarise with strings
    mtcars %>%
    s_group_by("cyl") %>%
    s_summarise("mean(disp), max(disp)")


  3. adilapapaya renamed this gist Apr 26, 2015. 1 changed file with 11 additions and 9 deletions.
    20 changes: 11 additions & 9 deletions s_dplyr → s_dplyr.R
    Original file line number Diff line number Diff line change
    @@ -52,28 +52,30 @@ eval.string.dplyr = function(.data, .fun.name, ...) {
    library(dplyr)

    # Original usage of dplyr
    mtcars %.%
    filter(gear == 3,cyl == 8) %.%
    mtcars %>%
    filter(gear == 3,cyl == 8) %>%
    select(mpg, cyl, hp:vs)

    # Select user specified cols.
    # Note that you can have a vector of strings
    # or a single string separated by ',' or a mixture of both
    cols = c("mpg","cyl, hp:vs")
    mtcars %.%
    filter(gear == 3,cyl == 8) %.%
    mtcars %>%
    filter(gear == 3,cyl == 8) %>%
    s_select(cols)

    # Filter using a string
    col = "gear"
    mtcars %.%
    s_filter(paste0(col,"==3"), "cyl==8" ) %.%
    mtcars %>%
    s_filter(paste0(col,"==3"), "cyl==8" ) %>%
    select(mpg, cyl, hp:vs)

    # Arrange without using %.%
    # Arrange without using %>%
    s_arrange(mtcars, "-mpg, gear, carb")

    # group_by and summarise with strings
    mtcars %.%
    s_group_by("cyl") %.%
    mtcars %>%
    s_group_by("cyl") %>%
    s_summarise("mean(disp), max(disp)")


  4. @skranz skranz created this gist Mar 21, 2014.
    79 changes: 79 additions & 0 deletions s_dplyr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    # Helper functions that allow string arguments for dplyr's data modification functions like arrange, select etc.
    # Author: Sebastian Kranz

    # Examples are below

    #' Modified version of dplyr's filter that uses string arguments
    #' @export
    s_filter = function(.data, ...) {
    eval.string.dplyr(.data,"filter", ...)
    }

    #' Modified version of dplyr's select that uses string arguments
    #' @export
    s_select = function(.data, ...) {
    eval.string.dplyr(.data,"select", ...)
    }

    #' Modified version of dplyr's arrange that uses string arguments
    #' @export
    s_arrange = function(.data, ...) {
    eval.string.dplyr(.data,"arrange", ...)
    }

    #' Modified version of dplyr's arrange that uses string arguments
    #' @export
    s_mutate = function(.data, ...) {
    eval.string.dplyr(.data,"mutate", ...)
    }

    #' Modified version of dplyr's summarise that uses string arguments
    #' @export
    s_summarise = function(.data, ...) {
    eval.string.dplyr(.data,"summarise", ...)
    }

    #' Modified version of dplyr's group_by that uses string arguments
    #' @export
    s_group_by = function(.data, ...) {
    eval.string.dplyr(.data,"group_by", ...)
    }

    #' Internal function used by s_filter, s_select etc.
    eval.string.dplyr = function(.data, .fun.name, ...) {
    args = list(...)
    args = unlist(args)
    code = paste0(.fun.name,"(.data,", paste0(args, collapse=","), ")")
    df = eval(parse(text=code,srcfile=NULL))
    df
    }

    # Examples
    library(dplyr)

    # Original usage of dplyr
    mtcars %.%
    filter(gear == 3,cyl == 8) %.%
    select(mpg, cyl, hp:vs)

    # Select user specified cols.
    # Note that you can have a vector of strings
    # or a single string separated by ',' or a mixture of both
    cols = c("mpg","cyl, hp:vs")
    mtcars %.%
    filter(gear == 3,cyl == 8) %.%
    s_select(cols)

    # Filter using a string
    col = "gear"
    mtcars %.%
    s_filter(paste0(col,"==3"), "cyl==8" ) %.%
    select(mpg, cyl, hp:vs)

    # Arrange without using %.%
    s_arrange(mtcars, "-mpg, gear, carb")

    # group_by and summarise with strings
    mtcars %.%
    s_group_by("cyl") %.%
    s_summarise("mean(disp), max(disp)")