-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.R
142 lines (126 loc) · 3.43 KB
/
app.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
134
135
136
137
138
139
140
141
142
### Keep this line to manually test this shiny application. Do not edit this line; shinycoreci::::is_manual_app
library(shiny)
library(ggplot2)
library(dplyr)
m <- mpg %>%
select(fl, cty, drv) %>%
distinct(cty, drv, .keep_all = TRUE) %>%
mutate(key = dplyr::row_number())
ui <- basicPage(
p("Brushing these plots should return the correct number of data points"),
p(
a("Issue #1433", href = "https://github.com/rstudio/shiny/issues/1433"), "/",
a("PR #2410", href = "https://github.com/rstudio/shiny/pull/2410")
),
plotOutput("plot1", brush = "brush1"),
uiOutput("res1"),
br(),
plotOutput("plot2", brush = "brush2"),
uiOutput("res2"),
plotOutput("plot3", brush = "brush3"),
uiOutput("res3")
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(m) +
geom_point(aes(fl, cty)) +
facet_wrap(~drv, scales = "free_x") +
geom_rect(
data = data.frame(
x1 = 0.8,
x2 = 1.2,
y1 = 28,
y2 = 36,
drv = "f"
),
aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2),
alpha = 0, color = "black", lty = 2
)
})
brush1key <- reactive({
if (is.null(input$brush1)) return(NULL)
brushedPoints(m, input$brush1)$key
})
output$res1 <- renderPrint({
if (is.null(brush1key())) {
return(tags$b("Brush the points outlined above"))
}
actual <- brush1key()
expected <- filter(m, drv == "f", fl == "d") %>% pull(key)
if (identical(sort(expected), sort(actual))) {
tags$b("Test passed!", style = "color: green")
} else {
tags$b("Test failed", style = "color: red")
}
})
brush2key <- reactive({
if (is.null(input$brush2)) return(NULL)
brushedPoints(m, input$brush2)$key
})
output$plot2 <- renderPlot({
ggplot(m) +
geom_point(aes(fl, cty)) +
geom_rect(
data = data.frame(
x1 = 0.8,
x2 = 1.2,
y1 = 8,
y2 = 12
),
aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2),
alpha = 0, color = "black", lty = 2
) +
scale_x_discrete(limits = c("e", "p"))
})
output$res2 <- renderPrint({
if (is.null(brush2key())) {
return(tags$b("Brush the points outlined above"))
}
actual <- brush2key()
expected <- filter(m, fl == "e") %>% pull(key)
if (identical(sort(expected), sort(actual))) {
tags$b("Test passed!", style = "color: green")
} else {
tags$b("Test failed", style = "color: red")
}
})
dat <- data.frame(
x = c("a", "b", NA, NA),
y = c(1, 2, 3, 2),
key = c("a", "b", "c", "d"),
stringsAsFactors = FALSE
)
output$plot3 <- renderPlot({
ggplot(dat) +
geom_point(aes(x, y)) +
facet_wrap(~x, scales = "free") +
geom_rect(
data = data.frame(
x1 = 0.8,
x2 = 1.2,
y1 = 2.9,
y2 = 3.1,
x = NA
),
aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2),
alpha = 0, color = "black", lty = 2
) +
ylim(1, 4)
})
brush3key <- reactive({
if (is.null(input$brush3)) return(NULL)
brushedPoints(dat, input$brush3)$key
})
output$res3 <- renderPrint({
if (is.null(brush3key())) {
return(tags$b("Brush the points outlined above"))
}
actual <- brush3key()
if (identical(actual, "c")) {
tags$b("Test passed!", style = "color: green")
} else {
tags$b("Test failed", style = "color: red")
}
})
}
shinyApp(ui, server)