Skip to content

Commit 92f6318

Browse files
authored
Merge pull request #6 from PetterHopp/main
OKplan v0.4.0 2021-12-09
2 parents 397c1dd + 7e90353 commit 92f6318

15 files changed

+427
-39
lines changed

DESCRIPTION

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: OKplan
22
Title: Tools to facilitate the Planning of the annual Surveillance Programmes
3-
Version: 0.3.0
4-
Date: 2021-11-29
3+
Version: 0.4.0
4+
Date: 2021-12-09
55
Authors@R:
66
c(person(given = "Petter",
77
family = "Hopp",
@@ -20,9 +20,11 @@ Encoding: UTF-8
2020
LazyData: true
2121
Imports:
2222
checkmate,
23-
poorman,
23+
dplyr,
24+
openxlsx,
2425
stats,
25-
NVIdb (>= 0.3.0)
26+
NVIdb (>= 0.3.0),
27+
NVIpretty
2628
Suggests:
2729
covr,
2830
devtools,
@@ -33,9 +35,11 @@ Suggests:
3335
styler,
3436
testthat,
3537
usethis,
38+
utils,
3639
NVIpackager
3740
Remotes:
3841
NorwegianVeterinaryInstitute/NVIdb,
42+
NorwegianVeterinaryInstitute/NVIpretty,
3943
NorwegianVeterinaryInstitute/NVIpackager
4044
RoxygenNote: 7.1.1
4145
VignetteBuilder: knitr

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export(append_date_generated_line)
55
export(append_sum_line)
66
export(check_OK_selection)
77
export(make_random)
8+
export(style_sum_line)
9+
export(write_ok_selection_list)

NEWS

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
OKplan 0.4.0 - (2021-12-09)
2+
---------------------------
3+
4+
New features:
5+
6+
- style_sum_line styles the row with sum of samples.
7+
8+
- write_ok_selection_list writes the selection list based on selected data from okplan file and uses standardize_columns to select, format and style columns.
9+
10+
11+
Other changes:
12+
13+
- extended OK_column_standards with tables for samples taken at abbatoirs and the surveillance in pigs.
14+
15+
116
OKplan 0.3.0 - (2021-11-29)
217
---------------------------
318

R/append_sum_line.R

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#' \dontrun{
1919
#' # Append row with sum
2020
#' gris_blodprover_slakteri <- append_sum_line(data = gris_blodprover_slakteri,
21-
#' col_name = c("ant_prover"),
21+
#' column = c("ant_prover"),
2222
#' pretext = "Sum",
2323
#' position = "first")
2424
#' }
@@ -27,6 +27,25 @@
2727

2828
append_sum_line <- function(data, column, pretext = "Sum", position = "left") {
2929

30+
# ARGUMENT CHECKING ----
31+
# Object to store check-results
32+
checks <- checkmate::makeAssertCollection()
33+
34+
# Perform assertions
35+
# data
36+
checkmate::assert_data_frame(data, add = checks)
37+
# column
38+
checkmate::assert_names(column, type = "unique", subset.of = colnames(data), add = checks)
39+
# pretext
40+
checkmate::assert_character(pretext, add = checks)
41+
# position
42+
checkmate::assert_choice(position, choices = c("first", "left"), add = checks)
43+
44+
# Report errors
45+
checkmate::reportAssertions(checks)
46+
47+
# APPEND SUM LINE ----
48+
3049
# Removes tibble as tibble will not accept the the pretext (character variable) in a numeric variable
3150
data <- as.data.frame(data)
3251

@@ -40,10 +59,10 @@ append_sum_line <- function(data, column, pretext = "Sum", position = "left") {
4059
# Append a line with the sum. The pretext is placed in accord with position
4160
if (position == "none") {
4261
data[dim(data)[1] + 1, c(column)] <- c(sum_column)
43-
}
62+
}
4463
if (position == "first") {
4564
data[dim(data)[1] + 1, c(colnames(data)[1], column)] <- c(pretext, sum_column)
46-
}
65+
}
4766
if (position == "left") {
4867
data[dim(data)[1] + 1, c((colnames(data)[which(colnames(data) == column[1]) - 1]), column)] <- c(pretext, sum_column)
4968
}

R/check_OK_selection.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ check_OK_selection <- function(data) {
4040

4141
print("Totalt antall besetninger og prover som skal testes")
4242
ktr <- data %>%
43-
poorman::group_by(ok_artkode, statuskode) %>%
44-
poorman::summarise(antall = poorman::n(), ant_prover = sum(ant_prover, na.rm = TRUE), .groups = "keep") %>%
45-
poorman::ungroup()
43+
dplyr::group_by(ok_artkode, statuskode) %>%
44+
dplyr::summarise(antall = dplyr::n(), ant_prover = sum(ant_prover, na.rm = TRUE), .groups = "keep") %>%
45+
dplyr::ungroup()
4646
print(ktr)
4747

4848
print("Antall utvalgte besetninger med mer enn en registrering per prodnr8")
4949
ktr <- data %>%
50-
poorman::add_count(ok_hensiktkode, eier_lokalitetnr) %>%
51-
poorman::ungroup() %>%
52-
poorman::filter(n > 1) %>%
53-
poorman::select(eier_lokalitetnr, eier_lokalitet, postnr, poststed)
50+
dplyr::add_count(ok_hensiktkode, eier_lokalitetnr) %>%
51+
dplyr::ungroup() %>%
52+
dplyr::filter(n > 1) %>%
53+
dplyr::select(eier_lokalitetnr, eier_lokalitet, postnr, poststed)
5454
print(ktr)
5555

5656
print("Utvalgte besetninger med missing prodnr8 eller missing navn")

R/style_sum_line.R

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#' @title Style row with "Sum" in Excel sheet
2+
#' @description Style the font with bold for the line with the text "Sum" in on cell.
3+
#' It is possible to use other text decoration, see \code{openxlxs::createStyle}.
4+
#' A line with the text "Sum" or another text as given by text will be styled.
5+
#'
6+
#' @details The whole line will be styled.
7+
#'
8+
#' @param workbook The workbook object.
9+
#' @param sheet The Excel sheet name.
10+
#' @param data The data frame that have been exported to the Excel sheet. Used to
11+
#' find column number and row number for the pretext for which the row should be styled.
12+
#' @param text The text in the cell for which the row should be styled.
13+
#' Defaults to "Sum".
14+
#' @param text_decoration The text decoration style that should be used, see \code{openxlsx::createStyle}.
15+
#' Defaults to "bold".
16+
#' @param \dots Other arguments to be passed.
17+
#'
18+
#'
19+
#' @return None. One row in the workbook object is styled.
20+
#'
21+
#' @author Petter Hopp Petter.Hopp@@vetinst.no
22+
#' @export
23+
24+
25+
style_sum_line <- function(workbook = workbook,
26+
sheet = sheet,
27+
data,
28+
text = "Sum",
29+
text_decoration = "bold",
30+
...) {
31+
32+
# ARGUMENT CHECKING ----
33+
# Object to store check-results
34+
checks <- checkmate::makeAssertCollection()
35+
36+
# Perform checks
37+
checkmate::assert_class(workbook, classes = "Workbook", add = checks)
38+
checkmate::assert_character(sheet, len = 1, min.chars = 1, add = checks)
39+
checkmate::assert_data_frame(data, add = checks)
40+
checkmate::assert_character(text, len = 1, add = checks)
41+
checkmate::assert_character(text_decoration, len = 1, add = checks)
42+
43+
# Report check-results
44+
checkmate::reportAssertions(checks)
45+
46+
# STYLING ----
47+
# Style a row in the Excel sheet with the given text in a cell
48+
openxlsx::addStyle(wb = workbook,
49+
sheet = sheet,
50+
style = openxlsx::createStyle(textDecoration = text_decoration),
51+
cols = 1:dim(data)[2],
52+
rows = which(data == text, arr.ind = TRUE)[1] + 1)
53+
54+
}

R/write_ok_selection_list.R

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#' @title Writes an Excel file with the selection list.
2+
#'
3+
#' @description The selection list based on selected data from okplan file and
4+
#' uses standardize_columns to select, format and style columns.
5+
#'
6+
#' @param data The data with units that should be tested.
7+
#' @param filename The name of the Excel file that should be written.
8+
#' @param filepath The path to the Excel file that should be written.
9+
#' @param sheet The name of the Excel sheet with the list.
10+
#' @param calculate_sum \[logical\] Should a line with the sum be appended. Defaults to TRUE.
11+
#' @param dbsource The name of the dbtable in OK_column_standards that should
12+
#' be used for standardizing the columns.
13+
#' @export
14+
15+
16+
write_ok_selection_list <- function(data,
17+
sheet,
18+
filename,
19+
filepath,
20+
calculate_sum = TRUE,
21+
dbsource) {
22+
# ARGUMENT CHECKING ----
23+
# Object to store check-results
24+
checks <- checkmate::makeAssertCollection()
25+
26+
# Perform checks
27+
# for (i in 1:length(data)) {
28+
checkmate::assert_data_frame(data, max.rows = (1048576 - 1), max.cols = 16384, add = checks)
29+
# }
30+
checkmate::assert_character(sheet, min.chars = 1, min.len = 1, max.len = length(data), unique = TRUE, add = checks)
31+
checkmate::assert_character(filename, min.chars = 1, len = 1, add = checks)
32+
checkmate::assert_directory_exists(filepath, add = checks)
33+
checkmate::assert_logical(calculate_sum, any.missing = FALSE, min.len = 1, add = checks)
34+
checkmate::assert_character(dbsource, min.len = 1, add = checks)
35+
checkmate::assert_choice(dbsource,
36+
choices = unique(OKplan::OK_column_standards[, "table_db"]),
37+
add = checks)
38+
39+
# Report check-results
40+
checkmate::reportAssertions(checks)
41+
42+
# GENERATE EXCEL WORKBOOK ----
43+
okwb <- openxlsx::createWorkbook()
44+
45+
# for (i in 1:length(data)) {
46+
# i <- 1
47+
# STANDARDIZE COLUMNS ----
48+
# column names
49+
okdata <- NVIdb::standardize_columns(data,
50+
standards = OKplan::OK_column_standards,
51+
dbsource = dbsource,
52+
property = "colnames")
53+
54+
# order columns and keep only designated columns
55+
okdata <- NVIdb::standardize_columns(data = okdata,
56+
standards = OKplan::OK_column_standards,
57+
dbsource = dbsource,
58+
property = "colorder", exclude = TRUE)
59+
60+
# INCLUDE EXTRA INFORMATION ----
61+
# Append sum
62+
if(isTRUE(calculate_sum)) {
63+
okdata <- append_sum_line(data = okdata, column = c("ant_prover"), position = "left")
64+
}
65+
66+
# Append date generated
67+
okdata <- append_date_generated_line(okdata)
68+
69+
70+
# STYLE EXCEL SHEET ----
71+
NVIpretty::add_formatted_worksheet(data = okdata,
72+
workbook = okwb,
73+
sheet = sheet,
74+
wrapHeadlineText = TRUE,
75+
collabels = TRUE,
76+
colwidths = TRUE,
77+
standards = OKplan::OK_column_standards,
78+
dbsource = dbsource)
79+
80+
81+
if(isTRUE(calculate_sum)) {
82+
style_sum_line(workbook = okwb, sheet = sheet, data = okdata)
83+
}
84+
# }
85+
# SAVE EXCEL WORKBOOK ----
86+
openxlsx::saveWorkbook(wb = okwb, file = paste0(filepath, filename), overwrite = TRUE)
87+
88+
}

data-raw/generate_OK_column_standards.R

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,14 @@ db_tables <- as.data.frame(unique(OK_column_standards$table_db)) %>%
4747
dplyr::mutate_if(is.list, purrr::simplify_all) %>% # flatten each list element internally
4848
tidyr::unnest(cols = "table")# expand
4949

50-
# Started alternative code without dplyr, purr and tidyr
51-
# # Generate data frame with all table names
52-
# db_tables <- as.data.frame(unique(OK_column_standards$table_db))
53-
# colnames(db_tables) <- "tables"
54-
# db_tables$table <- strsplit(db_tables$tables, split = ",")
55-
#
56-
# dplyr::mutate(table = %>%
57-
# dplyr::mutate_if(is.list, purrr::simplify_all) %>% # flatten each list element internally
58-
# tidyr::unnest(cols = "table")# expand
59-
6050
# Generate table with each table name on one line
6151
OK_column_standards <- OK_column_standards %>%
6252
dplyr::left_join(db_tables, by = c("table_db" = "tables")) %>%
6353
dplyr::mutate(table_db = trimws(table)) %>%
64-
dplyr::select(!table)
65-
66-
# unique_colnames <- unique(column_standards[, c("colname_db", "colname")]) %>%
67-
# poorman::add_count(colname_db, name = "unique_colnames") %>%
68-
# poorman::mutate(unique_colnames = poorman::case_when(unique_colnames == 1 ~ 1,
69-
# TRUE ~ 0))
70-
#
71-
# OK_column_standards <- OK_column_standards %>%
72-
# poorman::left_join(unique_colnames, by = c("colname_db" = "colname_db", "colname" = "colname"))
54+
dplyr::select(!table) %>%
55+
dplyr::mutate(table_db = tolower(table_db))
7356

7457
# SAVE IN PACKAGE DATA ----
7558
usethis::use_data(name = OK_column_standards, overwrite = TRUE, internal = FALSE)
7659

77-
# write.csv2(OK_column_standards,
78-
# file = paste0(set_dir_NVI("ProgrammeringR"), "standardization/OK_column_standards.csv"),
79-
# row.names = FALSE,
80-
# fileEncoding = "UTF-8")
8160

data/OK_column_standards.rda

419 Bytes
Binary file not shown.

man/append_sum_line.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)