Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/read_dataset.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
read_dataset <- function(year, type) {
filename <- list.files(
glue::glue("submissions/{test_flag}{year}/{type}"),
glue::glue("{year}/submissions/{type}"),
full.names = TRUE
)

Expand All @@ -12,6 +12,6 @@ read_dataset <- function(year, type) {
stop(glue::glue("No file detected in {type} folder for {year}"))
}

imported_table <- readr::read_csv(filename)
imported_table <- readr::read_csv(filename, show_col_types = FALSE)
return(imported_table)
}
78 changes: 61 additions & 17 deletions R/render_report.R
Original file line number Diff line number Diff line change
@@ -1,45 +1,89 @@
render_validation_report <- function(res, file_inventory, year, output_dir = ".", run_date = NULL) {
render_validation_report <- function(
res,
file_inventory,
year,
include_plots,
output_dir = ".",
run_date = NULL
) {
if (is.null(quarto::quarto_path())) {
stop("Quarto CLI not found. Install it from https://quarto.org", call. = FALSE)
stop(
"Quarto CLI not found. Install it from https://quarto.org",
call. = FALSE
)
}

if (is.null(run_date)){
run_date = as.character(Sys.Date() |> format("%Y-%m-%d"))
if (is.null(run_date)) {
run_date <- as.character(Sys.Date() |> format("%Y-%m-%d"))
}

work_dir <- tempfile("validation_report_")
dir.create(work_dir)
on.exit(unlink(work_dir, recursive = TRUE), add = TRUE)

template_dir <- system.file("report", package = "SciDataCheck", mustWork = TRUE)
file.copy(list.files(template_dir, full.names = TRUE), work_dir, recursive = TRUE)
template_dir <- system.file(
"report",
package = "SciDataCheck",
mustWork = TRUE
)
file.copy(
list.files(template_dir, full.names = TRUE),
work_dir,
recursive = TRUE
)

# everything the report needs goes in work_dir
results_path <- file.path(work_dir, "results.rds")
inventory_path <- file.path(work_dir, "file_inventory.rds")
results_path <- file.path(work_dir, "results.rds")
inventory_path <- file.path(work_dir, "file_inventory.rds")
error_desc_path <- file.path(work_dir, "error_descriptions.rds")

saveRDS(res, results_path)
saveRDS(file_inventory, inventory_path)
saveRDS(get_check_descriptions(), error_desc_path)

if (include_plots) {
# this saves the ace plots (for all gears) to the temp working dir

plot_oceancodes(work_dir = work_dir)
# runs all ace plots.
ace_byspp_byfleet(year = year, primary_path = work_dir)
# run all aggregate plots
map_agg_byfleet(year = year, primary_path = work_dir)
# run all logbook plots
map_log_byfleet(year = year, primary_path = work_dir)
}

quarto::quarto_render(
input = file.path(work_dir, "validation_report.qmd"),
execute_params = list(results_path = results_path,
inventory_path = inventory_path,
error_desc_path = error_desc_path,
submission_year = as.character(year),
run_date = run_date),
quiet = FALSE
input = file.path(work_dir, "validation_report.qmd"),
execute_params = list(
work_path = work_dir,
results_path = results_path,
inventory_path = inventory_path,
error_desc_path = error_desc_path,
submission_year = as.character(year),
run_date = run_date,
include_plots = include_plots
),
quiet = FALSE
)

# move the output to submissions folder
dir.create(output_dir, recursive = TRUE, showWarnings = FALSE)
out_path <- file.path(
output_dir,
paste0("validation_report_", year, "_", format(Sys.time(), "%Y%m%d"), ".html")
paste0(
"validation_report_",
year,
"_",
format(Sys.time(), "%Y%m%d"),
".html"
)
)
file.copy(
file.path(work_dir, "validation_report.html"),
out_path,
overwrite = TRUE
)
file.copy(file.path(work_dir, "validation_report.html"), out_path, overwrite = TRUE)

invisible(normalizePath(out_path))
}
51 changes: 32 additions & 19 deletions R/validate_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,60 @@ validate_data <- function(year = NULL) {
}

# TODO: folder structure should be {year}/submissions/{data_types} and {year}/reports/
dir_submission <- paste0(year, "/submissions")
dir_submission <- paste0(year)

if (dir.exists(dir_submission)) {
print("folder structure exists")

# check number of csv files available for validation if any
all_files <- list.files(dir_submission, pattern = ".csv$", recursive = T, full.names = T)
all_files <- list.files(
dir_submission,
pattern = ".csv$",
recursive = T,
full.names = T
)

# NOTE: If more than one csv in the data type folder only the most recently created will be validated
file_inventory <- .build_file_inventory(submission_year = year,
all_files = all_files)
file_inventory <- .build_file_inventory(
submission_year = year,
all_files = all_files
)

if(is.null(file_inventory)){
if (is.null(file_inventory)) {
return(invisible())
}

# Validate files - save output into a temp file
res <- validate_files(file_inventory)


if (nrow(res |> filter(!(status == "Validation failed"))) > 1){
# second level validation in files that passed the first level
if (nrow(res |> filter(!(status == "Validation failed"))) > 1) {
# second level validation in files that passed the first level
}

# TODO
# this should be changed to if-else statement at some point
# if passes all the second-checks then show plots??
inc_plots_bool <- TRUE

# Build report
report <- render_validation_report(res,
file_inventory,
year = year,
output_dir = dir_submission
report <- render_validation_report(
res,
file_inventory,
year = year,
include_plots = inc_plots_bool,
output_dir = dir_submission
)

invisible(list(results = res, report = report))


} else {
.create_folder_structure(submission_year = year)
message(paste0("The folder structure for the ", year, " scidata submissions was created.",
" Run `validate_data()` again after adding the files you would like to",
" validate in the appropriate data type folders."))
message(paste0(
"The folder structure for the ",
year,
" scidata submissions was created.",
" Run `validate_data()` again after adding the files you would like to",
" validate in the appropriate data type folders."
))
return(invisible())
}


}
Loading