tbl_l <<- fetch(con = con,
              schema = schema,
              tbl = tbl,
              column = column,
              entry = entry,
              verbose = verbose)
meta_dt <<- meta(con = con,
               schema = schema,
               tbl = tbl,
               plot_type = plot_type,
               plot_limit = plot_limit,
               plot_limit_text = plot_limit_text,
               output_dir = output_dir,
               output_file = output_file)
if (!is.null(report_text)) {
  if (file.exists(report_text)) {
    readLines(report_text)
  } else {
    cat(report_text)
  }
}

Meta table

knitr::kable(meta_dt)

Summary table

summary_dt = tbl_l$summary
idx = grep('^distinct$|n_', names(summary_dt), value = TRUE)
n_row = meta_dt[ variable == 'table rows', as.numeric(value) ]
summary_dt[ ,
            (idx) := lapply(.SD, function(x) perc_value(x, n_row)),
            .SDcols = idx ]

knitr::kable(summary_dt)

Column plots

if (plot_distinct) {
  verbose_message(verbose, '..Plotting.')
  for (i in seq_along(tbl_l$distinct)) {
    col = names(tbl_l$distinct)[i]
    dat = tbl_l$distinct[[i]]
    # TODO height = ceiling(length(unique(dat$var20)) / 4)
    height = 10
    # header
    cat('  \n###', col, '\n') # OPTION pander::pandoc.header(paste0('Column: ', col), level = 3)
    # plots
    # TODO one function - would have to include subchunkify
    if (plot_type == 'lollipop') {
      subchunkify(
        print(
          pl_lollipop(
            dat = dat,
            col = col,
            plot_limit = plot_limit,
            plot_limit_text = plot_limit_text
          )
        ),
        fig.height = height,
        fig.width = 15,
        chunk_name = paste0('plot-', i)
      )
    }
    if (plot_type == 'bar_horiz') {
      print(
        pl_bar_horiz(
          dat = dat,
          col = col,
          plot_limit = plot_limit,
          plot_limit_text = plot_limit_text
        )
      )
    }
    if (plot_type == 'treemap') {
      print(
        pl_treemap(
          dat = dat,
          col = col,
          plot_limit = plot_limit,
          plot_limit_text = plot_limit_text
        )
      )
    }
    # OLD print(pl_gg(dat = dat, x = 'var20', y = 'n_distinct', plot_type = plot_type))
    cat('<a href="#top">Back to top</a>')
    cat('  \n') # OPTION # pander::pandoc.p(''); pander::pandoc.p('')
  }
} else {
  print('Plots should not be printed.')
}
if (!is.null(file)) {
  verbose_message(verbose, '..Writing files.')
  write_to_dir(l = tbl_l$distinct,
               output_dir = output_dir,
               output_file = output_file,
               file_format = file_format,
               file_type = file_type)
} else {
  print('Files should not be written.')
}


andschar/dbreport documentation built on Dec. 8, 2022, 3:29 p.m.