#' donut UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_donut_ui <- function(id){
ns <- NS(id)
tagList(
semantic.dashboard::box(
title = 'Prevalence of Headache Symptom',
width = 16,
shiny.semantic::dropdown_input(ns('groupvar'),
choices = c('Grouping variable: No selection',names(dataset)[2:18]),
value = 'Grouping variable: No selection'),
shiny::plotOutput(ns('donutchart'))
),
shiny::tableOutput(ns('crosstab'))
)
}
#' donut Server Functions
#'
#' @noRd
mod_donut_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns
output$donutchart <- shiny::renderPlot({
if(input[['groupvar']] == 'Grouping variable: No selection'){
data <- dataset %>%
dplyr::select(HeadacheOnset) %>%
stats::na.omit() %>%
dplyr::count(HeadacheOnset) %>%
dplyr::mutate(fraction = n / sum(n),
ymax = cumsum(fraction),
ymin = c(0, head(ymax, n=-1)))
# Make the plot
data %>%
ggplot2::ggplot(ggplot2::aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=HeadacheOnset)) +
ggplot2::geom_rect() +
ggplot2::coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
ggplot2::xlim(c(2, 4)) +
ggplot2::theme_void() +
ggplot2::scale_fill_viridis_d() +
ggplot2::theme(legend.position = 'bottom') +
ggplot2::labs(fill = '')
} else {
data <- dataset %>%
dplyr::select(.data[[input[['groupvar']]]],HeadacheOnset) %>%
stats::na.omit() %>%
dplyr::group_by(.data[[input[['groupvar']]]]) %>%
dplyr::count(HeadacheOnset) %>%
dplyr::mutate(fraction = n / sum(n),
ymax = cumsum(fraction),
ymin = c(0, head(ymax, n=-1)))
# Make the plot
data %>%
ggplot2::ggplot(ggplot2::aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=HeadacheOnset)) +
ggplot2::geom_rect() +
ggplot2::coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
ggplot2::xlim(c(2, 4)) +
ggplot2::facet_wrap(~ .data[[input[['groupvar']]]] ,nrow = 1) +
ggplot2::theme_void() +
ggplot2::scale_fill_viridis_d() +
ggplot2::theme(legend.position = 'bottom') +
ggplot2::labs(fill = '')
}
})
output$crosstab <- shiny::renderTable({
if(input[['groupvar']] == 'Grouping variable: No selection'){
table_one <- arsenal::tableby( arsenal::formulize(x = 'HeadacheOnset'),
data = dataset,
control = arsenal::tableby.control(
test = FALSE)
)
as.data.frame(summary(table_one,text = TRUE), text = "html")
} else {
shiny::req(input[['groupvar']])
table_one <- arsenal::tableby( arsenal::formulize(y = input[['groupvar']],
x = 'HeadacheOnset'),
data = dataset,
control = arsenal::tableby.control(
test = FALSE)
)
as.data.frame(summary(table_one,text = TRUE), text = "html")
}
},sanitize.text.function = function(x) x)
})
}
## To be copied in the UI
# mod_donut_ui("donut_ui_1")
## To be copied in the server
# mod_donut_server("donut_ui_1")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.