| 1 |
rlang::check_installed("reshape2") |
| 2 |
library(reshape2) |
| 3 |
|
| 4 |
data(tips, package = "reshape2") |
| 5 |
|
| 6 |
tipsUI <- function(id) { |
| 7 |
ns <- NS(id) |
| 8 |
|
| 9 |
layout_sidebar( |
| 10 |
sidebar = sidebar( |
| 11 |
title = tooltip( |
| 12 |
span( |
| 13 |
"Restaurant tipping", |
| 14 |
bsicons::bs_icon("info-circle-fill"), |
| 15 |
class = "sidebar-title" |
| 16 |
), |
| 17 |
"One waiter recorded information about each tip he received", |
| 18 |
"over a period of a few months working in one restaurant." |
| 19 |
), |
| 20 |
sliderInput( |
| 21 |
ns("total_bill"), |
| 22 |
"Bill amount", |
| 23 |
min(tips$total_bill), |
| 24 |
max(tips$total_bill), |
| 25 |
value = range(tips$total_bill), |
| 26 |
pre = "$" |
| 27 |
), |
| 28 |
checkboxGroupInput( |
| 29 |
ns("time"), |
| 30 |
"Food service", |
| 31 |
c("Lunch", "Dinner"), |
| 32 |
c("Lunch", "Dinner"), |
| 33 |
inline = TRUE |
| 34 |
), |
| 35 |
actionButton(ns("reset"), "Reset filter"), |
| 36 |
), |
| 37 |
layout_column_wrap( |
| 38 |
width = 1 / 3, |
| 39 |
fill = FALSE, |
| 40 |
value_box( |
| 41 |
"Total tippers", |
| 42 |
uiOutput(ns("total_tippers"), container = h2), |
| 43 |
showcase = bsicons::bs_icon("person") |
| 44 |
), |
| 45 |
value_box( |
| 46 |
"Average tip", |
| 47 |
uiOutput(ns("average_tip"), container = h2), |
| 48 |
showcase = bsicons::bs_icon("wallet2") |
| 49 |
), |
| 50 |
value_box( |
| 51 |
"Average bill", |
| 52 |
uiOutput(ns("average_bill"), container = h2), |
| 53 |
showcase = bsicons::bs_icon("currency-dollar") |
| 54 |
) |
| 55 |
), |
| 56 |
layout_column_wrap( |
| 57 |
width = 1 / 2, |
| 58 |
class = "mt-3", |
| 59 |
card( |
| 60 |
full_screen = TRUE, |
| 61 |
card_header( |
| 62 |
"Total bill vs tip", |
| 63 |
popover( |
| 64 |
bsicons::bs_icon("gear"), |
| 65 |
radioButtons( |
| 66 |
ns("scatter_color"), |
| 67 |
NULL, |
| 68 |
inline = TRUE, |
| 69 |
c("none", "sex", "smoker", "day", "time") |
| 70 |
), |
| 71 |
title = "Add a color variable", |
| 72 |
placement = "top" |
| 73 |
), |
| 74 |
class = "d-flex justify-content-between align-items-center" |
| 75 |
), |
| 76 |
plotOutput(ns("scatterplot")) |
| 77 |
), |
| 78 |
card( |
| 79 |
full_screen = TRUE, |
| 80 |
class = "bslib-card-table-sm", |
| 81 |
card_header("Tips data"), |
| 82 |
DT::dataTableOutput(ns("table")) |
| 83 |
), |
| 84 |
), |
| 85 |
card( |
| 86 |
full_screen = TRUE, |
| 87 |
class = "mt-3", |
| 88 |
card_header( |
| 89 |
"Tip percentages", |
| 90 |
popover( |
| 91 |
bsicons::bs_icon("gear"), |
| 92 |
radioButtons( |
| 93 |
ns("tip_perc_y"), |
| 94 |
"Split by:", |
| 95 |
inline = TRUE, |
| 96 |
c("sex", "smoker", "day", "time"), |
| 97 |
"day" |
| 98 |
), |
| 99 |
radioButtons( |
| 100 |
ns("tip_perc_facet"), |
| 101 |
"Facet by:", |
| 102 |
inline = TRUE, |
| 103 |
c("none", "sex", "smoker", "day", "time"), |
| 104 |
"none" |
| 105 |
), |
| 106 |
title = "Add a color variable" |
| 107 |
), |
| 108 |
class = "d-flex justify-content-between align-items-center" |
| 109 |
), |
| 110 |
plotOutput(ns("tip_perc")) |
| 111 |
) |
| 112 |
) |
| 113 |
} |
| 114 |
|
| 115 |
tipsServer <- function(id) { |
| 116 |
moduleServer(id, function(input, output, session) { |
| 117 |
tips_data <- reactive({ |
| 118 |
d <- tips |
| 119 |
d <- d[ |
| 120 |
d$total_bill >= input$total_bill[1] & |
| 121 |
d$total_bill <= input$total_bill[2], |
| 122 |
] |
| 123 |
d <- d[d$time %in% input$time, ] |
| 124 |
d |
| 125 |
}) |
| 126 |
|
| 127 |
# TODO: maybe use reactable if and when this lands? |
| 128 |
# https://github.com/glin/reactable/pull/96 |
| 129 |
output$table <- DT::renderDataTable({ |
| 130 |
DT::datatable(tips_data(), fillContainer = TRUE, rownames = FALSE) |
| 131 |
}) |
| 132 |
|
| 133 |
output$scatterplot <- renderPlot({ |
| 134 |
validate( |
| 135 |
need( |
| 136 |
nrow(tips_data()) > 0, |
| 137 |
"No tips match the current filter. Try adjusting your filter settings." |
| 138 |
) |
| 139 |
) |
| 140 |
color <- if (input$scatter_color != "none") sym(input$scatter_color) |
| 141 |
ggplot(tips_data(), aes(x = total_bill, y = tip, color = !!color)) + |
| 142 |
geom_point() + |
| 143 |
geom_smooth() + |
| 144 |
labs(x = NULL, y = NULL) |
| 145 |
}) |
| 146 |
|
| 147 |
output$tip_perc <- renderPlot({ |
| 148 |
validate( |
| 149 |
need( |
| 150 |
requireNamespace("ggridges", quietly = TRUE), |
| 151 |
"Please install the ggridges package to see this plot." |
| 152 |
) |
| 153 |
) |
| 154 |
validate( |
| 155 |
need( |
| 156 |
requireNamespace("ggridges", quietly = TRUE), |
| 157 |
"Please install the ggridges package to see this plot." |
| 158 |
) |
| 159 |
) |
| 160 |
p <- ggplot( |
| 161 |
tips_data(), |
| 162 |
aes(x = tip / total_bill, y = !!sym(input$tip_perc_y)) |
| 163 |
) + |
| 164 |
ggridges::geom_density_ridges(scale = 0.9) + |
| 165 |
coord_cartesian(clip = "off") + |
| 166 |
labs(x = NULL, y = NULL) |
| 167 |
|
| 168 |
if (input$tip_perc_facet != "none") { |
| 169 |
p <- p + facet_wrap(vars(!!sym(input$tip_perc_facet))) |
| 170 |
} |
| 171 |
|
| 172 |
p |
| 173 |
}) |
| 174 |
|
| 175 |
output$total_tippers <- renderUI({ |
| 176 |
nrow(tips_data()) |
| 177 |
}) |
| 178 |
|
| 179 |
output$average_bill <- renderUI({ |
| 180 |
if (nrow(tips_data()) == 0) { |
| 181 |
return(HTML("–")) |
| 182 |
} |
| 183 |
scales::dollar(mean(tips_data()$total_bill)) |
| 184 |
}) |
| 185 |
|
| 186 |
output$average_tip <- renderUI({ |
| 187 |
if (nrow(tips_data()) == 0) { |
| 188 |
return(HTML("–")) |
| 189 |
} |
| 190 |
d <- tips_data() |
| 191 |
scales::percent(mean(d$tip / d$total_bill)) |
| 192 |
}) |
| 193 |
|
| 194 |
observeEvent(input$reset, { |
| 195 |
updateSliderInput(session, "total_bill", value = range(tips$total_bill)) |
| 196 |
updateCheckboxGroupInput(session, "time", selected = c("Lunch", "Dinner")) |
| 197 |
}) |
| 198 |
}) |
| 199 |
} |