Skip to content

Commit

Permalink
Merge pull request #30 from NorwegianVeterinaryInstitute/dev
Browse files Browse the repository at this point in the history
OKplan v0.8.0
  • Loading branch information
PetterHopp authored Dec 19, 2024
2 parents 94a25b0 + f6fcb2b commit 3404705
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ vignettes/*.pdf
.Renviron

inst/doc
!vignettes/OKplan.pdf

11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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,
Expand All @@ -38,6 +38,7 @@ Suggests:
knitr,
purrr,
rmarkdown,
R.rsp,
testthat,
usethis,
utils,
Expand All @@ -53,5 +54,5 @@ Remotes:
NorwegianVeterinaryInstitute/NVIrpackages,
NorwegianVeterinaryInstitute/OKcheck
RoxygenNote: 7.3.1
VignetteBuilder: knitr
VignetteBuilder: knitr, R.rsp
Language: en-GB
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 16 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions R/check_ok_selection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#'
Expand Down
88 changes: 88 additions & 0 deletions R/save_okplan.R
Original file line number Diff line number Diff line change
@@ -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,
...)
}
90 changes: 55 additions & 35 deletions R/write_ok_selection_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
Binary file modified data/OK_column_standards.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/check_ok_selection.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions man/save_okplan.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3404705

Please sign in to comment.