forked from gertstulp/ggplotgui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
static_report_complete.R
68 lines (54 loc) · 2.26 KB
/
static_report_complete.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#'
#' Wrapper function to generate complete HTML report: entity-level plus variable-level reports for one entity.
#'
#' @param entity_list (list) A list object containing information on a single data entity in metajam output format.
#' @param output_path (character) Path to save complete HTML report to. If "ask", a Rstudio pop-up window appears allowing choice of save directory (requires RStudio >= 1.1.287).
#'
#' @return A HTML file with complete static on the chosen data entity.
#'
#' @export
static_report_complete <- function(entity_list, output_path, shiny = F) {
# append "File_Name" in summary metadata to report name
report_filename <-
paste0("report_", entity_list[["summary_metadata"]][1, 2], ".html")
# make entity-level report
entity_df <- entity_list[["data"]]
entity_report <- static_report_entity(entity_df)
# detect spatial information
space_cols <- space_detective(entity_list)
# loop over columns in entity data and make variable-level reports
var_report <- lapply(colnames(entity_df), static_report_variable, entity_df = entity_df, space_cols = space_cols)
names(var_report) <- colnames(entity_df)
# create environment for RMarkdown
envir <-
list(
entity_list = entity_list,
report_title = paste("Summary report for", entity_list[["summary_metadata"]][1, 2]),
df = entity_df,
space_cols = space_cols,
entity_report = entity_report,
var_report = var_report
)
# set template path. Use the first one once this is in a package.
if (shiny == F) {
template_path <- system.file("rmd", "static_report_template.Rmd", package = "datapie")
# template_path <- "./inst/rmd/static_report_template.Rmd"
} else if (shiny == T) {
template_path <- system.file("rmd", "static_report_template_shiny.Rmd", package = "datapie")
# template_path <- "./inst/rmd/static_report_template_shiny.Rmd"
}
# set report output path.
if (output_path == "ask") {
output_path <- rstudioapi::selectDirectory(caption = "Select directory to save report to.")
}
try(rmarkdown::render(
input = template_path,
#output_format = "html_document",
output_dir = output_path,
output_file = report_filename,
envir = envir
))
if (shiny == T) {
return(report_filename)
}
}