# Module UI
#' @title mod_trend_plot_ui and mod_trend_plot_server
#' @description A shiny Module.
#'
#' @param id shiny id
#' @param input internal
#' @param output internal
#' @param session internal
#'
#' @rdname mod_trend_plot
#'
#' @keywords internal
#' @import dplyr
#' @importFrom dygraphs dygraph dygraphOutput renderDygraph dyAxis dyLegend dyOptions
#' @importFrom shinydashboard box
#' @importFrom shiny NS tagList fillCol HTML textOutput
#' @importFrom tidyr spread
#' @export
mod_trend_plot_ui <- function(id){
ns <- NS(id)
tagList(
fillCol(mod_select_substances_ui(ns("nombre")),
HTML("<br>"),
dygraphOutput(ns("trend")),
box(title = "\n",
textOutput(ns("legend"))),
width = "95%", height = 600,
flex = c(.75, .05, 4, 1.5))
)
}
# Module Server
#' @rdname mod_trend_plot
#' @export
#' @keywords internal
mod_trend_plot_server <- function(input, output, session,
data, selection, main = "", ylab = ""){
x_formatter <- "function(value){return value.toFixed(0);}"
y_formatter_axis <- "function(value) {
return value.toFixed(0).toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, \" \");
}"
y_formatter_value <- "function(value) {
return value.toFixed(0).toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, \" \") + \" tonnes\";
}"
ns <- session$ns
nombre_substance <- callModule(mod_select_substances_server, "nombre",
data = data, selection = selection)
output$trend <- renderDygraph({
data_plot <- filter(data, departement %in% selection(),
rang <= nombre_substance()) %>%
arrange(rang)
substances <- pull(data_plot, substance) %>%
unique()
data_plot <- data_plot %>%
select(annee, substance, quantite) %>%
spread(key = substance, value = quantite, fill = 0) %>%
select_at(c("annee", substances))
dygraph(data = data_plot,
main = main,
ylab = ylab) %>%
dyAxis("x", drawGrid = FALSE, axisLabelFormat = x_formatter) %>%
dyAxis("y", drawGrid = FALSE, axisLabelWidth = 75, axisLabelFormatter = y_formatter_axis, valueFormatter = y_formatter_value) %>%
dyOptions(stackedGraph = TRUE, connectSeparatedPoints = TRUE,
sigFig = 0) %>%
dyLegend(show = "always", labelsDiv = ns("legend"),
labelsSeparateLines = TRUE)
})
}
## To be copied in the UI
# mod_trend_plot_ui("trend_plot_ui_1")
## To be copied in the server
# callModule(mod_trend_plot_server, "trend_plot_ui_1")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.