|
| 1 | + |
| 2 | +#' Export results to saved files. |
| 3 | +#' |
| 4 | +#' Option to export grouped survey datasets, the GCF/OPUE Summary Table, and REP summaries in |
| 5 | +#' seperate data files. |
| 6 | +#' |
| 7 | +#' @details These following datalist objects will be written to the location, defined by \code{outdir}. |
| 8 | +#' \itemize{ |
| 9 | +#' \item The Grouped survey dataset object will be as saved as \code{reeffish_grouped.Rds}. |
| 10 | +#' \item The GCF and OPUE summaries will be saved as \code{summary_table.csv}. |
| 11 | +#' \item The \code{REP} count by species group summary will be saved as \code{REP_summary.csv}. |
| 12 | +#' } |
| 13 | +#' |
| 14 | +#' If the user doesn't specify a output directory, then a new directory will be generated from the user's HOME location. |
| 15 | +#' |
| 16 | +#' @param datalist List containing the Grouped dataset and summaries from \code{\link{load_dataset}} |
| 17 | +#' @param outdir Location to export data and summaries to. Default location is user's \code{HOME} directory |
| 18 | +#' |
| 19 | +#' @importFrom utils write.csv |
| 20 | +#' |
| 21 | +#' @export |
| 22 | +export_results <- function(datalist,outdir=Sys.getenv("HOME")){ |
| 23 | + |
| 24 | + tryCatch( |
| 25 | + { |
| 26 | + #If dir is default, create an output subdirectory from the user's HOME location. |
| 27 | + if(outdir == Sys.getenv("HOME")){ |
| 28 | + calibr_home <- file.path(outdir, "Calibr") |
| 29 | + |
| 30 | + if(!dir.exists(calibr_home)){ |
| 31 | + message("Creating directory for Calibr runs: ", calibr_home ," ...") |
| 32 | + dir.create(calibr_home) |
| 33 | + } |
| 34 | + |
| 35 | + outdir <- file.path(calibr_home,basename(tempfile(pattern="CalibrRun_"))) |
| 36 | + message("Creating output directory: ", outdir ," ...") |
| 37 | + dir.create(outdir) |
| 38 | + }else{ |
| 39 | + #check if outdir exists in user's system. If not return error message |
| 40 | + if(!(dir.exists(outdir))){ |
| 41 | + stop("Location of output directory was not found or inaccessible : \n",outdir) |
| 42 | + } |
| 43 | + message("Output Directory: ", outdir) |
| 44 | + } |
| 45 | + }, |
| 46 | + error=function(cond){ |
| 47 | + message(cond) |
| 48 | + return(NULL) |
| 49 | + } |
| 50 | + ) |
| 51 | + |
| 52 | + message("Exporting grouped data and summary tables to output directory ...") |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + #Check datalist is a list |
| 59 | + if(class(datalist) != "list"){ |
| 60 | + stop("Parameter datalist not a list object.") |
| 61 | + } |
| 62 | + |
| 63 | + #Check datalist contents have the appropriate data structures. |
| 64 | + str.datalist <- list("list", |
| 65 | + c("data.table","data.frame"), |
| 66 | + "data.frame") |
| 67 | + if(!all(sapply(datalist,class) %in% str.datalist)){ |
| 68 | + index_mismatch_classes <- !(sapply(datalist,class) %in% str.datalist) |
| 69 | + mismatched_classes <- sapply(datalist,class)[index_mismatch_classes] |
| 70 | + count_mismatches<- length(which(index_mismatch_classes)) |
| 71 | + |
| 72 | + mismatch_datalist_obj <- names(datalist)[index_mismatch_classes] |
| 73 | + stop("Datalist object(s) ", paste(mismatch_datalist_obj, collapse=", ") |
| 74 | + , "has an invalid object class:", paste(mismatched_classes,collapse=", ")) |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + #Check if datalist has the names "LGROUP", "SUMMARY", and "REP_SUMMARY" in datalist |
| 79 | + if(!all(names(datalist) %in% c("LGROUP", "SUMMARY", "REP_SUMMARY" ))){ |
| 80 | + missing_listNames <-names(datalist)[!(names(datalist) %in% c("LGROUP", "SUMMARY", "REP_SUMMARY" ))] |
| 81 | + stop("Invalid reef datalist fields: ", paste(missing_listNames,collapse = ", ")) |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + #LGROUP |
| 86 | + message("Saving Grouped Coral Reef Data ...") |
| 87 | + reeffish_grouped_datalist <- datalist[["LGROUP"]] |
| 88 | + saveRDS(reeffish_grouped_datalist, file = file.path(outdir,"reeffish_grouped.Rds")) |
| 89 | + |
| 90 | + #SUMMARY |
| 91 | + message("Saving GCF and OPUE Summaries to file ...") |
| 92 | + write.csv(datalist[["SUMMARY"]], file = file.path(outdir,"summary_table.csv"), row.names = FALSE) |
| 93 | + |
| 94 | + #REP_SUMMARY |
| 95 | + message("Saving REP Summary Table to file ...") |
| 96 | + write.csv(datalist[["REP_SUMMARY"]], file = file.path(outdir,"REP_summary.csv"), row.names = FALSE) |
| 97 | + |
| 98 | + message("Done.") |
| 99 | + message("Grouped data and summary tables have been to:\n", outdir) |
| 100 | + |
| 101 | +} |
0 commit comments