Skip to content

Commit 72170f9

Browse files
committed
clean-up implementation of 146
1 parent 1c32995 commit 72170f9

File tree

5 files changed

+106
-183
lines changed

5 files changed

+106
-183
lines changed

146-ames-explorer/app.R

+35-42
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,52 @@ source("modules/data_modules.R")
2020
# user interface
2121
ui <- fluidPage(
2222

23-
titlePanel("Ames Housing Data Explorer"),
23+
titlePanel("Ames Housing Data Explorer"),
2424

25-
fluidRow(
26-
column(
27-
width = 3,
28-
wellPanel(
29-
varselect_mod_ui("plot1_vars")
30-
)
31-
),
32-
column(
33-
width = 6,
34-
scatterplot_mod_ui("plots")
35-
),
36-
column(
37-
width = 3,
38-
wellPanel(
39-
varselect_mod_ui("plot2_vars")
40-
)
41-
)
42-
),
25+
fluidRow(
26+
column(
27+
width = 3,
28+
wellPanel(
29+
varselect_mod_ui("plot1_vars")
30+
)
31+
),
32+
column(
33+
width = 6,
34+
scatterplot_mod_ui("plots")
35+
),
36+
column(
37+
width = 3,
38+
wellPanel(
39+
varselect_mod_ui("plot2_vars")
40+
)
41+
)
42+
),
4343

44-
fluidRow(
45-
column(
46-
width = 12,
47-
checkboxInput("highlight_ind", "Highlight records selected?", value = FALSE),
48-
dataviewer_mod_ui("dataviewer")
49-
)
50-
)
44+
fluidRow(
45+
column(
46+
width = 12,
47+
checkboxInput("label", "Label brushed points?", value = FALSE),
48+
dataviewer_mod_ui("dataviewer")
49+
)
50+
)
5151
)
5252

5353
# server logic
5454
server <- function(input, output, session) {
5555

56-
plotdf <- reactive({
57-
brushedPoints(ames, res$brush(), allRows = TRUE)
58-
})
59-
60-
61-
# execute plot variable selection modules
6256
plot1vars <- callModule(varselect_mod_server, "plot1_vars")
6357
plot2vars <- callModule(varselect_mod_server, "plot2_vars")
6458

65-
# execute scatterplot module
66-
res <- callModule(scatterplot_mod_server,
67-
"plots",
68-
dataset = plotdf,
69-
plot1vars = plot1vars,
70-
plot2vars = plot2vars,
71-
highlight_ind = reactive({ input$highlight_ind }),
72-
highlight_rows = dt_highlight)
59+
dat <- callModule(
60+
scatterplot_mod_server,
61+
"plots",
62+
dataset = ames,
63+
plot1vars = plot1vars,
64+
plot2vars = plot2vars,
65+
label = reactive({ input$label })
66+
)
7367

74-
# execute dataviewer module
75-
dt_highlight <- callModule(dataviewer_mod_server, "dataviewer", dataset = res$processed)
68+
callModule(dataviewer_mod_server, "dataviewer", dat)
7669
}
7770

7871
# Run the application

146-ames-explorer/helpers/data_functions.R

-14
This file was deleted.
+31-31
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
1-
plot_labeller <- function(l, varname) {
2-
if (varname == "Sale_Price") {
3-
res <- dollar(l)
4-
} else {
5-
res <- comma(l)
6-
}
7-
return(res)
8-
}
9-
10-
is_brushed <- function(dataset, brush_colname = "selected_") {
11-
brush_colname %in% names(dataset)
12-
}
13-
141
#' Produce scatterplot with sales data and a single continuous variable
152
#'
163
#' @param data data frame with variables necessary for scatterplot.
174
#' @param xvar variable (string format) to be used on x-axis.
185
#' @param yvar variable (string format) to be used on y-axis.
196
#' @param facetvar optional variable (string format) to use for facetted version of plot.
20-
#' @param highlight_ind boolean indicating whether to perform annotation of data points
21-
#' on the plot. Default is \code{FALSE}.
22-
#' @param highlight_rows optional vector of row ids corresponding to which data point(s)
23-
#' to highlight in the scatterplot. Default value is \code{NULL}.
7+
#' @param label whether or not to label brushed points.
248
#'
259
#' @return {\code{ggplot2} object for the scatterplot.
2610
#' @export
2711
#'
2812
#' @examples
2913
#' plot_obj <- scatter_sales(data = ames, xvar = "Lot_Frontage")
3014
#' plot_obj
31-
scatter_sales <- function(dataset,
32-
xvar,
33-
yvar,
34-
facetvar = NULL,
35-
highlight_ind = FALSE,
36-
highlight_rows = NULL,
15+
scatter_sales <- function(dataset,
16+
xvar,
17+
yvar,
18+
facetvar = NULL,
19+
label = FALSE,
3720
point_colors = c("black", "#66D65C")) {
38-
21+
3922
x <- rlang::sym(xvar)
4023
y <- rlang::sym(yvar)
41-
42-
p <- ggplot(dataset, aes(x = !!x, y = !!y))
43-
24+
25+
p <- ggplot(dataset, aes(x = !!x, y = !!y))
26+
4427
p <- p +
4528
geom_point(aes(color = selected_)) +
4629
scale_color_manual(values = point_colors, guide = FALSE) +
4730
scale_x_continuous(labels = function(l) plot_labeller(l, varname = xvar)) +
4831
scale_y_continuous(labels = function(l) plot_labeller(l, varname = yvar)) +
4932
theme(axis.title = element_text(size = rel(1.2)),
5033
axis.text = element_text(size = rel(1.1)))
51-
34+
5235
if (!is.null(facetvar)) {
5336
fvar <- rlang::sym(facetvar)
54-
37+
5538
p <- p +
5639
facet_wrap(fvar)
5740
}
58-
59-
return(p)
41+
42+
if (label && any(dataset$selected_)) {
43+
p <- p + geom_label_repel(
44+
data = filter(dataset, selected_),
45+
aes(label = Sale_Price)
46+
)
47+
}
48+
49+
p
50+
}
51+
52+
53+
54+
plot_labeller <- function(l, varname) {
55+
if (varname == "Sale_Price") {
56+
dollar(l)
57+
} else {
58+
comma(l)
59+
}
6060
}

146-ames-explorer/modules/data_modules.R

+9-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @return a \code{shiny::\link[shiny]{tagList}} containing UI elements
66
varselect_mod_ui <- function(id) {
77
ns <- NS(id)
8-
8+
99
# define choices for X and Y variable selection
1010
var_choices <- list(
1111
`Sale price` = "Sale_Price",
@@ -20,7 +20,7 @@ varselect_mod_ui <- function(id) {
2020
`Above grade living area square feet` = "Gr_Liv_Area",
2121
`Garage area square feet` = "Garage_Area"
2222
)
23-
23+
2424
# assemble UI elements
2525
tagList(
2626
selectInput(
@@ -68,11 +68,11 @@ varselect_mod_server <- function(input, output, session) {
6868
list(
6969
xvar = reactive({ input$xvar }),
7070
yvar = reactive({ input$yvar }),
71-
facetvar = reactive({
71+
facetvar = reactive({
7272
if (input$groupvar == "") {
7373
return(NULL)
7474
} else {
75-
return(input$groupvar)
75+
return(input$groupvar)
7676
}
7777
})
7878
)
@@ -98,20 +98,15 @@ dataviewer_mod_ui <- function(id) {
9898
#' @param dataset data frame (reactive) used in scatterplots as produced by
9999
#' the \code{brushedPoints} function in the scatterplot module
100100
#'
101-
#' @return reactive vector of row IDs corresponding to the current view in the
101+
#' @return reactive vector of row IDs corresponding to the current view in the
102102
#' datatable widget.
103103
dataviewer_mod_server <- function(input, output, session, dataset) {
104-
105-
cols_select <- c("Year_Built", "Year_Sold", "Sale_Price", "Sale_Condition", "Lot_Frontage", "House_Style",
104+
105+
cols_select <- c("Year_Built", "Year_Sold", "Sale_Price", "Sale_Condition", "Lot_Frontage", "House_Style",
106106
"Lot_Shape", "Overall_Cond", "Overall_Qual")
107-
107+
108108
output$table <- renderDT({
109109
filter(dataset(), selected_) %>%
110110
select(one_of(cols_select))
111-
},
112-
filter = 'top',
113-
selection = "none")
114-
115-
# return highlight indicator and vector of row IDs selected by datatable filters
116-
reactive({ input$table_rows_all })
111+
}, filter = 'top', selection = 'none')
117112
}

0 commit comments

Comments
 (0)