-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.R
65 lines (55 loc) · 1.76 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
library(DT)
library(shiny)
library(tidyverse)
library(lubridate)
# read in and clean the dataset
movies <- read_csv("https://raw.githubusercontent.com/krisrs1128/stat479_s22/main/_posts/2022-02-10-week04-03/apps/data/movies.csv") %>%
mutate(
id = row_number(),
date = as_date(Release_Date, format = "%b %d %Y"),
year = year(date),
Major_Genre = fct_explicit_na(Major_Genre),
MPAA_Rating = fct_explicit_na(MPAA_Rating),
)
# helper functions used to generate the selection and plots
reset_selection <- function(x, brush) {
brushedPoints(x, brush, allRows = TRUE)$selected_
}
histogram <- function(movies) {
movies %>%
count(year) %>%
ggplot() +
geom_bar(aes(year, n), stat = "identity", width = 1) +
scale_y_continuous(expand = c(0, 0))
}
scatterplot <- function(movies, selected_) {
movies %>%
mutate(selected_ = selected_) %>%
ggplot() +
geom_point(aes(Rotten_Tomatoes_Rating, IMDB_Rating, alpha = as.numeric(selected_))) +
scale_alpha(range = c(0.05, 0.6))
}
data_table <- function(movies, selected_) {
movies %>%
filter(selected_) %>%
select(Title, Major_Genre, Worldwide_Gross, Director, Release_Date)
}
# create the app
ui <- fluidPage(
fluidRow(
column(6, plotOutput("histogram", brush = brushOpts("plot_brush", direction = "x"))),
column(6, plotOutput("scatterplot"))
),
dataTableOutput("table")
)
server <- function(input, output) {
selected <- reactiveVal(rep(TRUE, nrow(movies)))
observeEvent(
input$plot_brush,
selected(reset_selection(movies, input$plot_brush))
)
output$histogram <- renderPlot(histogram(movies))
output$scatterplot <- renderPlot(scatterplot(movies, selected()))
output$table <- renderDataTable(data_table(movies, selected()))
}
shinyApp(ui, server)