-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_display_plot.R
72 lines (70 loc) · 2.03 KB
/
mod_display_plot.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
#' Plot variables UI module
#'
#' @param id module id
#'
#' @return shiny UI module
#' @export mod_display_plot_ui
#'
#' @importFrom shiny NS tagList tags
#' @importFrom shiny plotOutput verbatimTextOutput
#' @import ggplot2
mod_display_plot_ui <- function(id) {
ns <- shiny::NS(id)
shiny::tagList(
shiny::tags$br(),
shiny::tags$blockquote(
shiny::tags$em(
shiny::tags$h6("The code for this application comes from the ",
shiny::tags$a("Building web applications with Shiny",
href = "https://rstudio-education.github.io/shiny-course/"),
"tutorial"))),
shiny::plotOutput(outputId = ns("scatterplot"))
)
}
#' Plot variables Server module
#'
#' @param id module id
#' @param var_inputs inputs from mod_var_input
#'
#' @return shiny server module
#' @export mod_display_plot_server
#'
#' @importFrom shiny NS moduleServer reactive
#' @importFrom tools toTitleCase
#' @importFrom shiny renderPlot
#' @importFrom stringr str_replace_all
#' @import ggplot2
mod_display_plot_server <- function(id, var_inputs) {
shiny::moduleServer(id, function(input, output, session) {
movies <- duap::movies
inputs <- shiny::reactive({
plot_title <- tools::toTitleCase(var_inputs$plot_title())
list(
x = var_inputs$x(),
y = var_inputs$y(),
z = var_inputs$z(),
alpha = var_inputs$alpha(),
size = var_inputs$size(),
plot_title = plot_title
)
})
output$scatterplot <- shiny::renderPlot({
plot <- point_plot(
df = movies,
x_var = inputs()$x,
y_var = inputs()$y,
col_var = inputs()$z,
alpha_var = inputs()$alpha,
size_var = inputs()$size
)
plot +
ggplot2::labs(
title = inputs()$plot_title,
x = stringr::str_replace_all(tools::toTitleCase(inputs()$x), "_", " "),
y = stringr::str_replace_all(tools::toTitleCase(inputs()$y), "_", " ")
) +
ggplot2::theme_minimal() +
ggplot2::theme(legend.position = "bottom")
})
})
}