R/render_report.R

Defines functions render_report

Documented in render_report

#' Render report
#'
#' Generate .html reports from pre-prepared Rmarkdown templates. 
#' 
#' @param report_name string denoting which pre-specified report to generate
#' @param pgres_host string defining the server hostname - all reports default to the 
#'     environment variable `PGRES_HOST`
#' @param pgres_user string defining the server user name - all reports default to the 
#'     environment variable `PGRES_USER`
#' @param pgres_pwd string defining the server password - all reports default to the 
#'     environment variable `PGRES_PWD`
#' @param pgres_dbname string defining the server database name - all reports default 
#'     to the environment variable `PGRES_DBNAME`
#' @param ... additional pass through variables for the rmarkdown::render function
#' 
#' @section Optional arguments:
#' 
#'   * `output_dir`: string representing the absolute path for generated output files
#'   * `params`: passed to the Rmarkdown report template to configure the 
#'   generated report. The following parameters are available:
#'     + `reporting_month`: a vector of integers representing the month(s) being reported. 
#'     Where no value is defined, the current month is automatically used. 
#'     + `reporting_year`: vector of integers representing the year(s) being reported. A 4 
#'     digit format is required. Where no value is defined, the current year is 
#'     automatically used. 
#'
#' @return This function will return a .html report file and save it to the path 
#' described in the `output_dir` argument.
#' @author Jay Achar 
#' @seealso \code{\link{hisreportr}}
#' 
#' @importFrom rmarkdown render
#' 
#' @examples
#' \dontrun{
#' # Generate the monthly TB report from Jan-March 2018
#' # Specify the server password as being stored in an environment variable
#' 
#'   render_report(report_name = "tb_month", 
#'                 output_dir = "~/Documents",
#'                 params = list(reporting_month = 1:3,
#'                               reporting_year = 2018,
#'                               pgres_pwd = Sys.getenv("PGRES_PWD")))
#' }
#' @export

render_report <- function(report_name = c("tb_month", "hiv_month", "hcv_month"),
                          pgres_host = Sys.getenv("PGRES_HOST"),
                          pgres_user = Sys.getenv("PGRES_USER"),
                          pgres_pwd = Sys.getenv("PGRES_PWD"),
                          pgres_dbname = Sys.getenv("PGRES_DBNAME"),
                          ...) {
  
  # check report_name arg
  report_name <- match.arg(report_name)
  
  # create input string
  input <- paste0(report_name, ".Rmd")
  
  # download data from server
  data_raw <- access_postgresql(host = pgres_host,
                                username = pgres_user,
                                password = pgres_pwd,
                                db_name = pgres_dbname,
                                report_name = report_name)
  
  # prepare data for report  
  if (report_name == "tb_month") {
    report_data <- prepare_tb_report_data(data_raw)
  }
  
  if (report_name == "hcv_month") {
    report_data <- prepare_hcv_report_data(data_raw)
  }
  
  if (report_name == "hiv_month") {
    report_list <- prepare_hiv_report_data(data_raw)
  }
  
  # TODO add assertr tests based on data being pulled from server
  
  # render requested report
  rmarkdown::render(input = system.file("rmarkdown", input,
                                        package = "hisreportr"),
                    ...)
}
JayAchar/hisreportr documentation built on March 18, 2020, 5:57 a.m.