1

I want to make my R script prettier with the use of functions.

My R script:

 library(tidyverse) #contains dplyr
Data <- data.frame(date = rep(as.Date(c('2018-06-18', '2018-06-19', '2018-06-20')), 4),
                   quantity = rep(c(1, 2, 3), each = 4),
                   article =  rep(c('insurance', 'pizza'), 6))

        D <- Data %>%

    select(date, quantity, article) %>%
    filter(str_detect(article,"pizza")) %>%
    group_by_(date) %>%
    summarise(quantity=sum(quantity))

I want to make something like this. Could anyone guide me on the right track?

    library(tidyverse) 
    library(lazyeval)

    f <- function(name){
    D <- Data %>%
    select_(~date, ~quantity, ~article) %>%
    filter_(~str_detect(~article,"name")) %>%
    group_by_(~date) %>%
    summarise_(quantity=~sum(quantity))
    return(D) 
    }

Thank you in advance.

RCP9
  • 89
  • 5
  • don't put quotes around name (`function(name)` instead of `function("name")`) – Melissa Key Jun 20 '18 at 17:01
  • Please explain more what you are trying to do and include sample data so others can work with. See more here [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Tung Jun 20 '18 at 17:03
  • What exactly do you want the function to do? I imagine you want it to take a data frame, group by a column that you supply to the function, then sum another column that you supply to the function. In this case, the arguments would be `Data`, `date`, and `quantity`. Is that correct? – camille Jun 20 '18 at 18:19
  • yes, that is correct. I have a data frame with a lot of orders and want to build the daily number of sales for each product. – RCP9 Jun 20 '18 at 18:33

2 Answers2

0

Does this work?

Data <- data.frame(date = rep(as.Date(c('2018-06-18', '2018-06-19', '2018-06-20')), 4),
                   quantity = rep(c(1, 2, 3), each = 4),
                   article =  rep(c('insurance', 'pizza'), 6)) 

f <- function(name){
  D <- Data %>%
    select(date, quantity, article) %>%
    filter(str_detect(article, name)) %>%
    group_by(date) %>%
    summarize(quantity = sum(quantity))

  return(D)
}

Is there a specific reason to use the deprecated functions?

zack
  • 5,205
  • 1
  • 19
  • 25
  • `D <- Data %>%` `select(date, quantity, article) %>%` `filter(str_detect(article, "pizza")) %>%` `group_by(date) %>%` `summarize(quantity = sum(quantity))` works, but not if i make a function. dplyr functions use non standard evaluation(NSE). I've read if i try to write a function i need standard evaluation (SE). Thats why i try to work with the lazyeval package. There is no reason to use these functions. I dont rightly know how to do it better. – RCP9 Jun 20 '18 at 18:12
0

You can use enquo and quo_name to build your function in tidyeval framework

library(tidyverse)

Data <- data.frame(date = rep(as.Date(c('2018-06-18', '2018-06-19', '2018-06-20')), 4),
                   quantity = rep(c(1, 2, 3), each = 4),
                   article =  rep(c('insurance', 'pizza'), 6)) 

daily_sales1 <- function(df, product){

  output <- df %>%
    select(date, quantity, article) %>%
    filter(str_detect(article, product)) %>%
    group_by(date) %>%
    summarise(quantity = sum(quantity, na.rm = TRUE))

  return(output)
}

daily_sales1(Data, 'pizza')
#> # A tibble: 3 x 2
#>   date       quantity
#>   <date>        <dbl>
#> 1 2018-06-18        4
#> 2 2018-06-19        3
#> 3 2018-06-20        5

daily_sales2 <- function(df, product){

  product <- enquo(product)

  output <- df %>%
    select(date, quantity, article) %>%
    filter(str_detect(article, !! quo_name(product))) %>%
    group_by(date) %>%
    summarise(quantity = sum(quantity, na.rm = TRUE))

  return(output)
}

daily_sales2(Data, pizza)
#> # A tibble: 3 x 2
#>   date       quantity
#>   <date>        <dbl>
#> 1 2018-06-18        4
#> 2 2018-06-19        3
#> 3 2018-06-20        5

Created on 2018-06-20 by the reprex package (v0.2.0).

Tung
  • 26,371
  • 7
  • 91
  • 115