diff --git a/.gitignore b/.gitignore index 191a0a7..b3695b2 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,5 @@ vignettes/*.pdf .Renviron inst/doc +!vignettes/OKplan.pdf + diff --git a/DESCRIPTION b/DESCRIPTION index b4bc9f7..26014d4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: OKplan Title: Tools to facilitate the Planning of the annual Surveillance Programmes -Version: 0.7.1 -Date: 2024-11-12 +Version: 0.8.0 +Date: 2024-12-19 Authors@R: c(person(given = "Petter", family = "Hopp", @@ -26,9 +26,9 @@ Imports: stats, NVIbatch (>= 0.4.0), NVIcheckmate (>= 0.7.3), - NVIdb, + NVIdb (>= 0.13.1), NVIpjsr, - NVIpretty (>= 0.4.0), + NVIpretty (>= 0.4.2), OKcheck Suggests: covr, @@ -38,6 +38,7 @@ Suggests: knitr, purrr, rmarkdown, + R.rsp, testthat, usethis, utils, @@ -53,5 +54,5 @@ Remotes: NorwegianVeterinaryInstitute/NVIrpackages, NorwegianVeterinaryInstitute/OKcheck RoxygenNote: 7.3.1 -VignetteBuilder: knitr +VignetteBuilder: knitr, R.rsp Language: en-GB diff --git a/NAMESPACE b/NAMESPACE index 84dc409..dd9c0ad 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,7 @@ export(check_ok_selection) export(get_holiday) export(get_tested_herds) export(make_random) +export(save_okplan) export(style_sum_line) export(write_ok_selection_list) importFrom(rlang,.data) diff --git a/NEWS.md b/NEWS.md index 0d87610..055e8d1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,19 @@ +# OKplan 0.8.0 - (2024-12-19) + +## New features: + +- created `save_okplan` to save the selection information in a standard format. + +- `write_ok_selection_list` now accepts `data.frame`, `list` and csv-file as input to the argument `column_standards`. + + +## Other changes: + +- `OK_column_standards` is updated with column standards used in 2025, i.e. including orgnr in selection lists and generating "ok_ai_art_fjorfe". + +- created pdf reference manual "OKplan.pdf" as a vignette. + + # OKplan 0.7.1 - (2024-11-12) ## Bug fixes diff --git a/R/check_ok_selection.R b/R/check_ok_selection.R index 12c3d60..64b35fa 100644 --- a/R/check_ok_selection.R +++ b/R/check_ok_selection.R @@ -43,8 +43,8 @@ #' Set "browser" for the default browser or "viewer" for the R studio #' viewer. `TRUE` equals "browser". If `FALSE`, don't display #' the results file. Defaults to "browser". -#' @param \dots Other arguments to be passed to -#' \ifelse{html}{\code{\link[NVIbatch]{output_rendered}}}{\code{NVIbatch::output_rendered}}. +#' @param \dots Other arguments to be passed to +#' \ifelse{html}{\code{\link[NVIbatch:output_rendered]{NVIbatch::output_rendered}}}{\code{NVIbatch::output_rendered}}. #' #' @return Generates an html-file with the results of the checks to be displayed in the browser. #' diff --git a/R/save_okplan.R b/R/save_okplan.R new file mode 100644 index 0000000..c608cf2 --- /dev/null +++ b/R/save_okplan.R @@ -0,0 +1,88 @@ +#' @title Writes the surveillance selection to a standardised file. +#' +#' @description The surveillance selection is written to a standardised file. +#' The file is standardised to include a standard set of columns and have +#' the records in a standard order. +#' @details The data is saved as an "okplan" file that will be used the source +#' file when the selection list is generated. +#' +#' The function uses +#' \ifelse{html}{\code{\link[NVIdb:standardize_columns]{NVIdb::standardize_columns}}}{\code{NVIdb::standardize_columns}}. +#' to select and order the columns. The formatting information is taken from +#' \code{\link{OK_column_standards}}. +#' +#' @param data [\code{data.frame}]\cr +#' The sampling plan with the units to be reported. +#' @param filename [\code{character(1)}]\cr +#' The name of the csv file including extension. +#' @param filepath [\code{character(1)}]\cr +#' The path to the csv file. +#' @param sortvar [\code{character}]\cr +#' The sort order for the records in the csv-file. Defaults to +#' c("ok_hensiktkode", "ok_driftsformkode", "statuskode", +#' "prioritet_av_reserve", "eier_lokalitetnr") +#' @param \dots Other arguments to be passed to +#' \ifelse{html}{\code{\link[utils:write.csv2]{utils::write.csv2}}}{\code{utils::write.csv2}}. +#' +#' @return None. Saves a data frame with the selection in a standard csv file. +#' +#' @author Petter Hopp Petter.Hopp@@vetinst.no +#' @export +#' @examples +#' \dontrun{ +#' library(OKplan) +#' td <- tempdir() +#' okplan <- as.data.frame(list("ok_hensiktkode" = c("01002", "01002"), +#' "ok_driftsformkode" = c("010202", "010202"), +#' "statuskode" = c(1, 1), +#' "prioritet_av_reserve" = c(NA, NA), +#' "eier_lokalitetnr" = c("1101123456", "1102123456"))) +#' write_okplan(data = okplan, +#' filename = "okplan_species_disease", +#' filepath = td) +#' } +save_okplan <- function(data, + filename, + filepath, + sortvar = c("ok_hensiktkode", "ok_driftsformkode", + "statuskode", "prioritet_av_reserve", + "eier_lokalitetnr"), + ...) { + + # PREPARE ARGUMENTS BEFORE ARGUMENT CHECKING ---- + ## Remove trailing backslash or slash before testing path + filepath <- sub("\\\\{1,2}$|/{1,2}$", "", filepath) + + # ARGUMENT CHECKING ---- + ## Object to store check-results + checks <- checkmate::makeAssertCollection() + ## Perform checks + ### data + checkmate::assert_data_frame(data, add = checks) + checkmate::assert_character(filename, min.chars = 1, len = 1, add = checks) + ### filename and filepath + checkmate::assert_character(filename, min.chars = 1, len = 1, add = checks) + checkmate::assert_directory_exists(filepath) + ### order + checkmate::assert_subset(sortvar, choices = colnames(data), add = checks) + ## Report check-results + checkmate::reportAssertions(checks) + + + # STANDARDISE AND SAVE DATA TO csv-FILE ---- + ## Standardise column order, exclude columns + data <- NVIdb::standardize_columns(data, + dbsource = "okplan", + standards = OKplan::OK_column_standards, + property = "colorder", + exclude = TRUE) + + ## Sort data in the order given by sortvar + data <- data[do.call(base::order, data[, sortvar]), ] + + ## write the data to the csv-file + utils::write.csv2(data, + file = file.path(filepath, filename), + row.names = FALSE, + ...) +} diff --git a/R/write_ok_selection_list.R b/R/write_ok_selection_list.R index 3efa166..2c88d46 100644 --- a/R/write_ok_selection_list.R +++ b/R/write_ok_selection_list.R @@ -5,20 +5,21 @@ #' without further formatting. #' @details The data must originate from an "okplan" file and #' the function uses -#' \ifelse{html}{\code{\link[NVIdb]{standardize_columns}}}{\code{NVIdb::standardize_columns}} -#' to select, order, format and style the columns. The formatting +#' \ifelse{html}{\code{\link[NVIdb:standardize_columns]{NVIdb::standardize_columns}}}{\code{NVIdb::standardize_columns}}. +#' to select, order, format and style the columns. The formatting #' information is either taken from \code{\link{OK_column_standards}} or -#' can be input as a list. +#' can be input as a \code{list} #' #' When using \code{\link{OK_column_standards}}, the formatting information is #' taken in accord with the argument \code{dbsource}. If the formatting #' needs to be edited, it must be edited in the general source file for #' column standards and thereafter, build it into a new version of \code{OKplan}. #' As this can be a tedious process, there is a possibility to input the -#' formatting information as a list. +#' formatting information as a \code{list} or as a csv-file that can be transformed +#' to a \code{data.frame} with the same columns as \code{\link{OK_column_standards}}. #' #' The list input to column_standards must follow a specific format. -#' It is a list with at least three named vectors: +#' It is a \code{list} with at least three named vectors: #' \itemize{ #' \item \code{colname}: a vector of all columns in in the source file that #' should be included in the Excel report with the selection list. @@ -61,11 +62,15 @@ #' The name of the Excel file. #' @param filepath [\code{character(1)}]\cr #' The path to the Excel file. -#' @param column_standards [\code{data.frame} | \code{list}]\cr -#' The column standards to be used as input for +#' @param column_standards [\code{data.frame} | \code{list} | \code{character(1)}]\cr +#' The column standards to be used as input for #' \ifelse{html}{\code{\link[NVIdb]{standardize_columns}}}{\code{NVIdb::standardize_columns}} -#' when formatting the sampling plan for -#' output, see details. Defaults to \code{\link{OK_column_standards}}. +#' when formatting the sampling plan for output, see details. Defaults to +#' \code{\link{OK_column_standards}}. +#' For giving alternatives to the standard table for column_standards using +#' different formats, see details. Defaults to +#' file.path(NVIdb::set_dir_NVI("ProgrammeringR", slash = FALSE), +#' "standardization", "colnames", "column_standards.csv"). #' @param calculate_sum [\code{logical(1)}]\cr #' Should a line with the sum be appended? Defaults to \code{TRUE}. #' @param footnote [\code{character(1)}]\cr @@ -103,33 +108,52 @@ write_ok_selection_list <- function(data, checks <- checkmate::makeAssertCollection() # Perform checks - # for (i in 1:length(data)) { + # data checkmate::assert_data_frame(data, max.rows = (1048576 - 1), max.cols = 16384, add = checks) - # } + # filename and filepath checkmate::assert_character(sheet, min.chars = 1, min.len = 1, max.len = length(data), unique = TRUE, add = checks) checkmate::assert_character(filename, min.chars = 1, len = 1, add = checks) checkmate::assert_directory_exists(filepath, add = checks) if (isTRUE(add_worksheet)) { - checkmate::assert_file_exists(file.path(filepath, filename), access = "r") + checkmate::assert_file_exists(file.path(filepath, filename), access = "r", add = checks) } # column_standards + # checkmate::assert(checkmate::check_class(column_standards, classes = c("data.frame")), + # checkmate::check_class(column_standards, classes = c("list")), + # add = checks) + # if (inherits(column_standards, what = "list")) { + # lengths_standard <- lengths(column_standards) + # NVIcheckmate::assert_integer(lengths_standard, lower = lengths_standard[1], upper = lengths_standard[1], + # min.len = 3, max.len = 6, + # comment = "When input as a list, all elements must have the same length", + # add = checks) + # + # checkmate::assert_subset(names(column_standards), choices = c("table_db", "colname_db", "colname", "collabel", "colwidth", "colorder"), + # add = checks) + # } + # if (inherits(column_standards, what = "data.frame")) { + # checkmate::assert_data_frame(column_standards, min.rows = 1, min.cols = 6, add = checks) + # } checkmate::assert(checkmate::check_class(column_standards, classes = c("data.frame")), checkmate::check_class(column_standards, classes = c("list")), + checkmate::check_class(column_standards, classes = c("character")), add = checks) + if (inherits(column_standards, what = "character")) { + checkmate::assert_file_exists(column_standards, add = checks) + } if (inherits(column_standards, what = "list")) { lengths_standard <- lengths(column_standards) NVIcheckmate::assert_integer(lengths_standard, lower = lengths_standard[1], upper = lengths_standard[1], min.len = 3, max.len = 6, comment = "When input as a list, all elements must have the same length", add = checks) - checkmate::assert_subset(names(column_standards), choices = c("table_db", "colname_db", "colname", "collabel", "colwidth", "colorder"), add = checks) } if (inherits(column_standards, what = "data.frame")) { checkmate::assert_data_frame(column_standards, min.rows = 1, min.cols = 6, add = checks) } - + # calculate_sum checkmate::assert_flag(calculate_sum, add = checks) checkmate::assert_string(footnote, min.chars = 1, null.ok = TRUE, add = checks) checkmate::assert_character(dbsource, min.len = 1, add = checks) @@ -144,27 +168,23 @@ write_ok_selection_list <- function(data, checkmate::reportAssertions(checks) # TRANSFORM column_standards FROM list TO data.frame - # column_standards = list("colname" = c("mt_region", "mt_avdeling"), - # "collabel" = c("MT region", "MT avdeling"), - # "colwidth" = c(35, 35)) - - if (inherits(column_standards, what = "list")) { - column_standards <- as.data.frame((column_standards)) - - if (!"table_db" %in% colnames(column_standards)) { - column_standards$table_db <- dbsource - } - - if (!"colname_db" %in% colnames(column_standards)) { - column_standards$colname_db <- column_standards$colname - } - - if (!"colorder" %in% colnames(column_standards)) { - column_standards$colorder <- c(1:dim(column_standards)[1]) - } - colnames(column_standards)[which(colnames(column_standards) == "collabel")] <- "label_1_no" - colnames(column_standards)[which(colnames(column_standards) == "colwidth")] <- "colwidth_Excel" - } + # if (inherits(column_standards, what = "list")) { + # column_standards <- as.data.frame((column_standards)) + # + # if (!"table_db" %in% colnames(column_standards)) { + # column_standards$table_db <- dbsource + # } + # + # if (!"colname_db" %in% colnames(column_standards)) { + # column_standards$colname_db <- column_standards$colname + # } + # + # if (!"colorder" %in% colnames(column_standards)) { + # column_standards$colorder <- c(1:dim(column_standards)[1]) + # } + # colnames(column_standards)[which(colnames(column_standards) == "collabel")] <- "label_1_no" + # colnames(column_standards)[which(colnames(column_standards) == "colwidth")] <- "colwidth_Excel" + # } # GENERATE EXCEL WORKBOOK ---- # create or load workbook diff --git a/data/OK_column_standards.rda b/data/OK_column_standards.rda index f512739..85a9773 100644 Binary files a/data/OK_column_standards.rda and b/data/OK_column_standards.rda differ diff --git a/man/check_ok_selection.Rd b/man/check_ok_selection.Rd index 753ec41..ce30958 100644 --- a/man/check_ok_selection.Rd +++ b/man/check_ok_selection.Rd @@ -43,7 +43,7 @@ Set "browser" for the default browser or "viewer" for the R studio the results file. Defaults to "browser".} \item{\dots}{Other arguments to be passed to -\ifelse{html}{\code{\link[NVIbatch]{output_rendered}}}{\code{NVIbatch::output_rendered}}.} +\ifelse{html}{\code{\link[NVIbatch:output_rendered]{NVIbatch::output_rendered}}}{\code{NVIbatch::output_rendered}}.} } \value{ Generates an html-file with the results of the checks to be displayed in the browser. diff --git a/man/save_okplan.Rd b/man/save_okplan.Rd new file mode 100644 index 0000000..b054cd7 --- /dev/null +++ b/man/save_okplan.Rd @@ -0,0 +1,67 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/save_okplan.R +\name{save_okplan} +\alias{save_okplan} +\title{Writes the surveillance selection to a standardised file.} +\usage{ +save_okplan( + data, + filename, + filepath, + sortvar = c("ok_hensiktkode", "ok_driftsformkode", "statuskode", + "prioritet_av_reserve", "eier_lokalitetnr"), + ... +) +} +\arguments{ +\item{data}{[\code{data.frame}]\cr +The sampling plan with the units to be reported.} + +\item{filename}{[\code{character(1)}]\cr +The name of the csv file including extension.} + +\item{filepath}{[\code{character(1)}]\cr +The path to the csv file.} + +\item{sortvar}{[\code{character}]\cr +The sort order for the records in the csv-file. Defaults to +c("ok_hensiktkode", "ok_driftsformkode", "statuskode", + "prioritet_av_reserve", "eier_lokalitetnr")} + +\item{\dots}{Other arguments to be passed to +\ifelse{html}{\code{\link[utils:write.csv2]{utils::write.csv2}}}{\code{utils::write.csv2}}.} +} +\value{ +None. Saves a data frame with the selection in a standard csv file. +} +\description{ +The surveillance selection is written to a standardised file. + The file is standardised to include a standard set of columns and have + the records in a standard order. +} +\details{ +The data is saved as an "okplan" file that will be used the source + file when the selection list is generated. + + The function uses + \ifelse{html}{\code{\link[NVIdb:standardize_columns]{NVIdb::standardize_columns}}}{\code{NVIdb::standardize_columns}}. + to select and order the columns. The formatting information is taken from + \code{\link{OK_column_standards}}. +} +\examples{ +\dontrun{ +library(OKplan) +td <- tempdir() +okplan <- as.data.frame(list("ok_hensiktkode" = c("01002", "01002"), + "ok_driftsformkode" = c("010202", "010202"), + "statuskode" = c(1, 1), + "prioritet_av_reserve" = c(NA, NA), + "eier_lokalitetnr" = c("1101123456", "1102123456"))) +write_okplan(data = okplan, + filename = "okplan_species_disease", + filepath = td) +} +} +\author{ +Petter Hopp Petter.Hopp@vetinst.no +} diff --git a/man/write_ok_selection_list.Rd b/man/write_ok_selection_list.Rd index 36872bf..2147ac0 100644 --- a/man/write_ok_selection_list.Rd +++ b/man/write_ok_selection_list.Rd @@ -31,11 +31,15 @@ The name of the Excel file.} \item{filepath}{[\code{character(1)}]\cr The path to the Excel file.} -\item{column_standards}{[\code{data.frame} | \code{list}]\cr +\item{column_standards}{[\code{data.frame} | \code{list} | \code{character(1)}]\cr The column standards to be used as input for -\ifelse{html}{\code{\link[NVIdb]{standardize_columns}}}{\code{NVIdb::standardize_columns}} -when formatting the sampling plan for -output, see details. Defaults to \code{\link{OK_column_standards}}.} + \ifelse{html}{\code{\link[NVIdb]{standardize_columns}}}{\code{NVIdb::standardize_columns}} + when formatting the sampling plan for output, see details. Defaults to + \code{\link{OK_column_standards}}. +For giving alternatives to the standard table for column_standards using + different formats, see details. Defaults to + file.path(NVIdb::set_dir_NVI("ProgrammeringR", slash = FALSE), + "standardization", "colnames", "column_standards.csv").} \item{calculate_sum}{[\code{logical(1)}]\cr Should a line with the sum be appended? Defaults to \code{TRUE}.} @@ -64,20 +68,21 @@ The sampling plan is output to an Excel sheet. The list with \details{ The data must originate from an "okplan" file and the function uses - \ifelse{html}{\code{\link[NVIdb]{standardize_columns}}}{\code{NVIdb::standardize_columns}} - to select, order, format and style the columns. The formatting + \ifelse{html}{\code{\link[NVIdb:standardize_columns]{NVIdb::standardize_columns}}}{\code{NVIdb::standardize_columns}}. + to select, order, format and style the columns. The formatting information is either taken from \code{\link{OK_column_standards}} or - can be input as a list. + can be input as a \code{list} When using \code{\link{OK_column_standards}}, the formatting information is taken in accord with the argument \code{dbsource}. If the formatting needs to be edited, it must be edited in the general source file for column standards and thereafter, build it into a new version of \code{OKplan}. As this can be a tedious process, there is a possibility to input the - formatting information as a list. + formatting information as a \code{list} or as a csv-file that can be transformed + to a \code{data.frame} with the same columns as \code{\link{OK_column_standards}}. The list input to column_standards must follow a specific format. - It is a list with at least three named vectors: + It is a \code{list} with at least three named vectors: \itemize{ \item \code{colname}: a vector of all columns in in the source file that should be included in the Excel report with the selection list. diff --git a/tests/testthat/test_OK_column_standards.R b/tests/testthat/test_OK_column_standards.R index b68808d..027fefd 100644 --- a/tests/testthat/test_OK_column_standards.R +++ b/tests/testthat/test_OK_column_standards.R @@ -51,7 +51,7 @@ test_that("Standard collabels for OK selections", { # Make a vector with correct column names after translation correct_result <- c("\u00C5r", "Rapport", "MT regionnr", "MT region", "MT avdelingsnr", "MT avdeling", "Produsentnr", - "Viksomhet", "Postnr", "Poststed", "Antall pr\u00F8ver", "Pr\u00F8vetakingsuke") + "Virksomhet", "Postnr", "Poststed", "Antall pr\u00F8ver", "Pr\u00F8vetakingsuke") # Compare Add fylke, current fylkenr and current fylke with correct result expect_equal(standardize_columns(data = df, diff --git a/tests/testthat/test_write_ok_selection_list.R b/tests/testthat/test_write_ok_selection_list.R index 7e0a0cd..000b8bd 100644 --- a/tests/testthat/test_write_ok_selection_list.R +++ b/tests/testthat/test_write_ok_selection_list.R @@ -1,4 +1,5 @@ -library(OKplan) +# library(OKplan) +library(NVIdb) library(testthat) test_that("write_ok_selection_list", { @@ -7,9 +8,9 @@ test_that("write_ok_selection_list", { # Make example data x <- as.data.frame(cbind("ok_aar" = 2021, "rapport" = "Brucellose hos geit, utvalgsliste", - "mt_regionnr" = "M21000", "mt_ region" = "Region Stor-Oslo", + "mt_regionnr" = "M21000", "mt_region" = "Region Stor-Oslo", "mt_avdelingnr" = "M21150", "mt_avdeling" = "Romerike", - "eier_lokalitetnr" = "30303030", "eier_lokalitet" = "XXX XXXXX", + "eier_lokalitetnr" = "30303030", "orgnr" = "999999999", "eier_lokalitet" = "XXX XXXXX", "postnr" = "0468", "poststed" = "OSLO", "ant_prover" = 26)) # Probably first makes a matrix, therefore "Antall prøver" is character. x$ant_prover <- as.numeric(x$ant_prover) @@ -21,12 +22,100 @@ test_that("write_ok_selection_list", { calculate_sum = TRUE, dbsource = "ok_avlsgris") + expect_true(checkmate::check_file_exists(file.path(td, "oktest.xlsx"))) + + expect_identical(openxlsx::getSheetNames(file.path(td, "oktest.xlsx")), + "ok_test_data") + + y <- openxlsx::read.xlsx(xlsxFile = file.path(td, "oktest.xlsx")) + expect_identical(colnames(y), + c("År", "Rapport", "MT.regionnr", "MT.region", + "MT.avdelingsnr", "MT.avdeling", "Produsentnr", + "Foretaksnr", "Virksomhet", "Postnr", "Poststed", + "Antall.prøver")) + expect_identical(y[2, "Antall.prøver"], "26") + expect_identical(y[3, 1], paste("Datauttrekket er gjort", format(Sys.Date(), "%d/%m/%Y"))) + +}) + +test_that("write_ok_selection_list, column_standards is list", { + + td <- tempdir() + + # Make example data + x <- as.data.frame(cbind("ok_aar" = 2021, "rapport" = "Brucellose hos geit, utvalgsliste", + "mt_regionnr" = "M21000", "mt_region" = "Region Stor-Oslo", + "mt_avdelingnr" = "M21150", "mt_avdeling" = "Romerike", + "eier_lokalitetnr" = "30303030", "orgnr" = "999999999", "eier_lokalitet" = "XXX XXXXX", + "postnr" = "0468", "poststed" = "OSLO", "ant_prover" = 26)) + # Probably first makes a matrix, therefore "Antall prøver" is character. + x$ant_prover <- as.numeric(x$ant_prover) + + write_ok_selection_list(data = x, + sheet = "ok_test_data", + filename = "oktest.xlsx", + filepath = td, + column_standards = + list("colname" = c("ok_aar", "rapport", "mt_regionnr", "mt_region", "mt_avdelingnr", + "mt_avdeling", "eier_lokalitetnr", "orgnr", "eier_lokalitet", + "postnr", "poststed", + "ant_prover"), + "collabel" = c("År", "Rapport", "MT regionnr", "MT region", "MT avdelingsnr", + "MT avdeling", "Produsentnr", "Foretaksnr", "Virksomhet", "Postnr", "Poststed", + "Antall prøver"), + "colwidth" = c(5, 9, 12.5, 16, 13, 30, 12, 12, 30, 8, 15, 8.5)), + calculate_sum = TRUE, + dbsource = "ok_brucella") + expect_true(checkmate::check_file_exists(paste0(td, "/oktest.xlsx"))) expect_identical(openxlsx::getSheetNames(paste0(td, "/oktest.xlsx")), "ok_test_data") + y <- openxlsx::read.xlsx(xlsxFile = file.path(td, "oktest.xlsx")) + expect_identical(colnames(y), + c("År", "Rapport", "MT.regionnr", "MT.region", + "MT.avdelingsnr", "MT.avdeling", "Produsentnr", + "Foretaksnr", "Virksomhet", "Postnr", "Poststed", + "Antall.prøver")) + expect_identical(y[2, "Antall.prøver"], "26") + expect_identical(y[3, 1], paste("Datauttrekket er gjort", format(Sys.Date(), "%d/%m/%Y"))) - # testwb <- openxlsx::loadWorkbook(xlsxFile = paste0(td, "/oktest.xlsx")) +}) + +test_that("write_ok_selection_list, column_standards is file", { + + td <- tempdir() + + # Make example data + x <- as.data.frame(cbind("ok_aar" = 2021, "rapport" = "Brucellose hos geit, utvalgsliste", + "mt_regionnr" = "M21000", "mt_region" = "Region Stor-Oslo", + "mt_avdelingnr" = "M21150", "mt_avdeling" = "Romerike", + "eier_lokalitetnr" = "30303030", "orgnr" = "999999999", "eier_lokalitet" = "XXX XXXXX", + "postnr" = "0468", "poststed" = "OSLO", "ant_prover" = 26)) + # Probably first makes a matrix, therefore "Antall prøver" is character. + x$ant_prover <- as.numeric(x$ant_prover) + + write_ok_selection_list(data = x, + sheet = "ok_test_data", + filename = "oktest.xlsx", + filepath = td, + column_standards = file.path(NVIdb::set_dir_NVI("programmeringR", slash = FALSE), + "standardization", "colnames", "OK_column_standards.csv"), + calculate_sum = TRUE, + dbsource = "ok_avlsgris") + + expect_true(checkmate::check_file_exists(paste0(td, "/oktest.xlsx"))) + + expect_identical(openxlsx::getSheetNames(paste0(td, "/oktest.xlsx")), + "ok_test_data") + y <- openxlsx::read.xlsx(xlsxFile = file.path(td, "oktest.xlsx")) + expect_identical(colnames(y), + c("År", "Rapport", "MT.regionnr", "MT.region", + "MT.avdelingsnr", "MT.avdeling", "Produsentnr", + "Foretaksnr", "Virksomhet", "Postnr", "Poststed", + "Antall.prøver")) + expect_identical(y[2, "Antall.prøver"], "26") + expect_identical(y[3, 1], paste("Datauttrekket er gjort", format(Sys.Date(), "%d/%m/%Y"))) }) diff --git a/vignettes/OKplan.pdf b/vignettes/OKplan.pdf new file mode 100644 index 0000000..765904b Binary files /dev/null and b/vignettes/OKplan.pdf differ diff --git a/vignettes/OKplan.pdf.asis b/vignettes/OKplan.pdf.asis new file mode 100644 index 0000000..45730bf --- /dev/null +++ b/vignettes/OKplan.pdf.asis @@ -0,0 +1,3 @@ +%\VignetteIndexEntry{OKplan reference manual} +%\VignetteEngine{R.rsp::asis} +%\VignetteKeyword{PDF}