-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.R
161 lines (135 loc) · 5.28 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Load packages used by the app
library(shiny)
library(bslib)
library(thematic)
library(tidyverse)
library(gitlink)
source("setup.R")
# Set the default theme for ggplot2 plots
ggplot2::theme_set(ggplot2::theme_minimal())
# Apply the CSS used by the Shiny app to the ggplot2 plots
thematic_shiny()
# Define the Shiny UI layout
ui <- page_sidebar(
# Add github link (removed for connect cloud)
# ribbon_css("https://github.com/rstudio/demo-co/tree/main/evals-analysis-app"),
# Set CSS theme
theme = bs_theme(bootswatch = "darkly",
bg = "#222222",
fg = "#86C7ED",
success ="#86C7ED"),
# Add title
title = "Effectiveness of DemoCo App Free Trial by Customer Segment",
# Add sidebar elements
sidebar = sidebar(title = "Select a segment of data to view",
class ="bg-secondary",
selectInput("industry", "Select industries", choices = industries, selected = "", multiple = TRUE),
selectInput("propensity", "Select propensities to buy", choices = propensities, selected = "", multiple = TRUE),
selectInput("contract", "Select contract types", choices = contracts, selected = "", multiple = TRUE),
"This app compares the effectiveness of two types of free trials, A (30-days) and B (100-days), at converting users into customers.",
tags$img(src = "logo.png", width = "100%", height = "auto")),
# Layout non-sidebar elements
layout_columns(card(card_header("Conversions over time"),
plotOutput("line")),
card(card_header("Conversion rates"),
plotOutput("bar")),
value_box(title = "Recommended Trial",
value = textOutput("recommended_eval"),
theme_color = "secondary"),
value_box(title = "Customers",
value = textOutput("number_of_customers"),
theme_color = "secondary"),
value_box(title = "Avg Spend",
value = textOutput("average_spend"),
theme_color = "secondary"),
card(card_header("Conversion rates by subgroup"),
tableOutput("table")),
col_widths = c(8, 4, 4, 4, 4, 12),
row_heights = c(4, 1.5, 3))
)
# Define the Shiny server function
server <- function(input, output) {
# Provide default values for industry, propensity, and contract selections
selected_industries <- reactive({
if (is.null(input$industry)) industries else input$industry
})
selected_propensities <- reactive({
if (is.null(input$propensity)) propensities else input$propensity
})
selected_contracts <- reactive({
if (is.null(input$contract)) contracts else input$contract
})
# Filter data against selections
filtered_expansions <- reactive({
expansions |>
filter(industry %in% selected_industries(),
propensity %in% selected_propensities(),
contract %in% selected_contracts())
})
# Compute conversions by month
conversions <- reactive({
filtered_expansions() |>
mutate(date = floor_date(date, unit = "month")) |>
group_by(date, evaluation) |>
summarize(n = sum(outcome == "Won")) |>
ungroup()
})
# Retrieve conversion rates for selected groups
groups <- reactive({
expansion_groups |>
filter(industry %in% selected_industries(),
propensity %in% selected_propensities(),
contract %in% selected_contracts())
})
# Render text for recommended trial
output$recommended_eval <- renderText({
recommendation <-
filtered_expansions() |>
group_by(evaluation) |>
summarise(rate = mean(outcome == "Won")) |>
filter(rate == max(rate)) |>
pull(evaluation)
as.character(recommendation[1])
})
# Render text for number of customers
output$number_of_customers <- renderText({
sum(filtered_expansions()$outcome == "Won") |>
format(big.mark = ",")
})
# Render text for average spend
output$average_spend <- renderText({
x <-
filtered_expansions() |>
filter(outcome == "Won") |>
summarise(spend = round(mean(amount))) |>
pull(spend)
str_glue("${x}")
})
# Render line plot for conversions over time
output$line <- renderPlot({
ggplot(conversions(), aes(x = date, y = n, color = evaluation)) +
geom_line() +
theme(axis.title = element_blank()) +
labs(color = "Trial Type")
})
# Render bar plot for conversion rates by subgroup
output$bar <- renderPlot({
groups() |>
group_by(evaluation) |>
summarise(rate = round(sum(n * success_rate) / sum(n), 2)) |>
ggplot(aes(x = evaluation, y = rate, fill = evaluation)) +
geom_col() +
guides(fill = "none") +
theme(axis.title = element_blank()) +
scale_y_continuous(limits = c(0, 100))
})
# Render table for conversion rates by subgroup
output$table <- renderTable({
groups() |>
select(industry, propensity, contract, evaluation, success_rate) |>
pivot_wider(names_from = evaluation, values_from = success_rate)
},
digits = 0)
}
# Create the Shiny app
shinyApp(ui = ui, server = server)