forked from daranzolin/ViewPipeSteps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cases.R
134 lines (107 loc) · 3.45 KB
/
test_cases.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Needed for some examples
library(tidyverse)
# Garabage (should throw parsing error)
This is no valid R code.
# Valid expression that contains no call (should throw error)
variable
#Call that contains no assignment or pipe (should throw error)
sleep(1000)
#Call containing assignment but no RHS call (should throw error)
y <- 42
#Call containing assignment but no pipe (should throw error)
y <- 6 * 7
#Simple pipe
mtcars %>% filter(am == 1)
# One line pipe
mtcars %>% filter(am == 1) %>% select(qsec)
# One line pipe with assignment
mtcars %>% filter(am == 1) %>% select(qsec) -> result
# Multiple line pipe with end assignment
mtcars %>%
filter(am == 1) %>%
select(qsec) -> result
# Multiple line pipe with comments and normal assignment
iris_long <-
iris %>%
#gather this here data frame
gather(measure, value, -Species) %>%
arrange(-value) #sort by value descending
# Mix of one line and multiple line pipe
iris %>% group_by(Species) %>% summarize_if(is.numeric, mean) %>%
ungroup() %>% gather(measure, value, -Species) %>%
arrange(value)
# Pipe with command spanning multiple lines
iris %>%
group_by(Species) %>%
summarise(Sepal.Length.min = min(Sepal.Length),
Sepal.Width.min = min(Sepal.Width),
Petal.Length.min = min(Petal.Length),
Petal.Width.min = min(Petal.Width)) -> min_iris
# Example from issue #14
diamonds %>%
select(carat, cut, color, clarity, price) %>%
group_by(color) %>%
summarise(n = n(), price = mean(price)) %>%
arrange(desc(color))
# Assign with pipe (esoteric, works)
assign("result", mtcars %>% filter(am == 1) %>% select(qsec))
# do.call with pipe (too esoteric, throws an error)
do.call("%>%", list(quote(mtcars), quote(filter(am == 1))))
# Non-standard pipes (won't work)
diamonds %>%
select(carat, cut, color, clarity, price) %>%
group_by(color) %>%
summarise(n = n(), price = mean(price)) %T>%
print() %>%
arrange(desc(color))
my_diamonds <- diamonds
my_diamonds %<>%
select(carat, cut, color, clarity, price) %>%
group_by(color) %>%
summarise(n = n(), price = mean(price))
arrange(desc(color))
iris %>%
subset(Sepal.Length > mean(Sepal.Length)) %$%
cor(Sepal.Length, Sepal.Width)
# Using the new print_pipe_step_function at the end
diamonds %>%
select(carat, cut, color, clarity, price) %>%
group_by(color) %>%
summarise(n = n(), price = mean(price)) %>%
arrange(desc(color)) %>%
print_pipe_steps() -> result
# ... and within
diamonds %>%
select(carat, cut, color, clarity, price) %>%
group_by(color) %>%
print_pipe_steps() %>%
summarise(n = n(), price = mean(price)) %>%
arrange(desc(color))
# Use non-standard commands to print
diamonds %>%
select(carat, cut, color, clarity, price) %>%
group_by(color) %>%
print_pipe_steps("View(ps%d, title = title)") %>%
summarise(n = n(), price = mean(price)) %>%
arrange(desc(color))
my_print_cmd <- c(
"message(title);",
"skimr::skim_tee(.data = ps%d)"
)
diamonds %>%
select(carat, cut, color, clarity, price) %>%
group_by(color) %>%
print_pipe_steps(my_print_cmd, all = TRUE) %>%
summarise(n = n(), price = mean(price)) %>%
arrange(desc(color))
# Using the _very_ experimental new pipe type
diamonds %>%
select(carat, cut, color, clarity, price) %P>%
group_by(color) %>%
summarise(n = n(), price = mean(price)) %>%
arrange(desc(color))
diamonds %>%
select(carat, cut, color, clarity, price) %P>%
group_by(color) %>%
summarise(n = n(), price = mean(price)) %P>%
arrange(desc(color))