R/mod_npds_notifications_report.R

Defines functions npds_notifications_report_Server npds_notifications_report_UI

#' NPDS notifications report UI function
#'
#' @description A shiny UI module for the NPDS notifications report
#'
#' @param id
#'
#' @noRd
#'
#' @importFrom plotly renderPlotly plotlyOutput

npds_notifications_report_UI <- function(id) {

  ns <- NS(id)

  fluidPage(
    tags$head(tags$style(".butt{margin-bottom:5px;}")),
    downloadButton(ns("create_report"), "Create MS Word report", class = "butt"),
    fluidRow(
      valueBoxOutput(ns("n")),
      valueBoxOutput(ns("median_start_to_sent"))
    ),
    fluidRow(
      bs4Card(plotlyOutput(ns("n_by_year")), title = "Number of notifications sent per year", width = 4),
      bs4Card(plotlyOutput(ns("defintion_id")), title = "Number of notifications by defintion ID", width = 4),
      bs4Card(plotlyOutput(ns("data_completness")), title = "Percent of missing values per variable", width = 4)
      # column(4, box(plotlyOutput(ns("n_by_year"))),
      # column(4, plotlyOutput(ns("defintion_id"))),
      # column(4, plotlyOutput(ns("data_completness")))
    ),
    plotlyOutput(ns("sent_to_state_epi"), height = "700px"),
    plotlyOutput(ns("sent_to_state_epi_by_year"), height = "700px")
  )
}

#' NPDS Notifications Report Server function
#'
#' @description A shiny server module for the NPDS notifications report
#'
#' @param id
#'
#' @noRd

npds_notifications_report_Server <- function(id, database) {
  moduleServer(
    id,
    function(input, output, session) {

      summary_database <- reactive({summarize_npds_db(database())})

      plots <- reactive({
        plot_summary(summary_database(), type = "plotly", n_years = 2)
      })

      output$n <- renderValueBox({
        valueBox(
          summary_database()[["n"]],
          subtitle = "Total number of notifications",
          color = "primary",
          icon = icon("envelope-square")
        )
      })

      output$median_start_to_sent <- renderValueBox({
        valueBox(
          paste(summary_database()[["median_start_to_sent"]], "days"),
          subtitle = "Median time to complete notification",
          color = "success",
          icon = icon("stopwatch")
        )
      })

      output$n_by_year <- renderPlotly(plots()[["n_by_year"]])

      output$sent_to_state_epi <- renderPlotly(plots()[["sent_to_state_epi"]])

      output$sent_to_state_epi_by_year <- renderPlotly(plots()[["sent_to_state_epi_by_year"]])

      output$defintion_id <- renderPlotly(plots()[["defintion_id"]])

      output$data_completness <- renderPlotly(plots()[["data_completness"]])

      output$create_report <- downloadHandler(
        # For PDF output, change this to "report.pdf"
        filename = "npds_report.doc",
        content = function(file) {
          # Copy the report file to a temporary directory before processing it, in
          # case we don't have write permissions to the current working dir (which
          # can happen when deployed).

          reportDir <- system.file("reports", package = "DERTtools")

          tempReport <- file.path(reportDir, "npds_report.Rmd")

          # Set up parameters to pass to Rmd document
          params <- list(db = database())

          # Knit the document, passing in the `params` list, and eval it in a
          # child of the global environment (this isolates the code in the
          # document from the code in this app).
          rmarkdown::render(tempReport, output_file = file,
                            params = params,
                            envir = new.env(parent = globalenv())
          )
        }
      )

    }
  )
}
renejuan/DERTtools documentation built on March 19, 2022, 7:20 a.m.