-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplotHist.R
133 lines (125 loc) · 3.6 KB
/
plotHist.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
# This module was created during the St Jude Bio-Hackathon of May 2023 by the team 13.
# author: Max Qiu (ytqiuhaowen@gmail.com)
# author: Louis Le Nézet (louislenezet@gmail.com)
# author: Alyssa Obermayer (aobermayer4@gmail.com)
#### Library needed #### ----------
usethis::use_package("shiny")
usethis::use_package("ggplot2")
#### Function needed to work #### ----------
#' Plot ggplot histogram
#'
#' @param data A numeric vector.
#' @param breaks Numeric. Number of bins. Default is 50
#' @param title Character. Plot title.
#'
#' @return A ggplot histogram with a title
#' @keywords hplot
#' @export ggplot_truehist
ggplot_truehist <- function(data, breaks = 50, title) {
data <- as.numeric(data)
ggplot2::ggplot() +
ggplot2::aes(data) +
ggplot2::geom_histogram(ggplot2::aes(y = ggplot2::after_stat(stats::density)),
bins = breaks,
fill = "cornflowerblue", color = "gray30"
) +
ggplot2::labs(title = title) +
ggplot2::theme_classic() +
ggplot2::theme(
plot.title = ggplot2::element_text(hjust = 0.5),
aspect.ratio = 1
)
}
#### UI function of the module #### ----------
#' Histogram plot ui module
#'
#' @description R Shiny module UI to generate an hitogram plot.
#'
#' @details This module create a histogram plot from a given
#' dataframe.
#' The number of bins is selectable by the user.
#'
#' @param id A string.
#' @returns A Shiny UI.
#' @examples
#' \dontrun{
#' plotHist_demo()
#' }
#' @keywords hplot
#' @export plotHist_ui
plotHist_ui <- function(id) {
ns <- shiny::NS(id)
shiny::tagList(
shiny::numericInput(ns("bins"), "bins", 20, min = 1, step = 1),
shiny::plotOutput(ns("hist"))
)
}
#### Server function of the module #### ----------
#' Histogram plot server module
#'
#' @description R Shiny module server to generate an histogram plot.
#'
#' @details This module create a histogram plot from a given
#' dataframe.
#' The number of bins is selectable by the user.
#'
#' @param id A string.
#' @param data A reactive vector of numeric values
#' @param title A reactive string to be used as a title
#' @returns Histogram plot.
#' @examples
#' \dontrun{
#' plotHist_demo()
#' }
#' @keywords hplot
#' @export plotHist_server
plotHist_server <- function(id, data, title = shiny::reactive("Histogram")) {
stopifnot(shiny::is.reactive(data))
stopifnot(shiny::is.reactive(title))
shiny::moduleServer(id, function(input, output, session) {
hist_plot <- shiny::reactive({
shiny::req(is.numeric(data()))
main <- paste0(title(), " [", input$bins, "]")
ggplot_truehist(data(), breaks = input$bins, title = main)
})
output$hist <- shiny::renderPlot({
hist_plot()
})
return(hist_plot)
})
}
#### Demo function of the module #### ----------
#' Histogram plot demo app
#'
#' @description R Shiny module server to generate an histogram plot.
#'
#' @details This module create a histogram plot app.
#' The user can select the dataframe and the variable to use,
#' as well as the number of bins for the histogramm.
#'
#' @returns A shiny app
#' @examples
#' \dontrun{
#' plotHist_demo()
#' }
#' @keywords hplot
#' @export plotHist_demo
plotHist_demo <- function() {
ui <- shiny::fluidPage(
shiny::sidebarLayout(
shiny::sidebarPanel(
dataImport_ui("datafile", "User data"),
selectVar_ui("var", "Choose a column"),
),
shiny::mainPanel(
plotHist_ui("hist")
)
)
)
server <- function(input, output, session) {
data <- dataImport_server("datafile")
var <- selectVar_server("var", data, filter = is.numeric)
plotHist_server("hist", var)
}
shiny::shinyApp(ui, server)
}