Skip to content

Commit 4ef1648

Browse files
authored
Merge pull request #8 from PetterHopp/main
OKplan v0.4.1
2 parents 92f6318 + 784ab67 commit 4ef1648

21 files changed

+84
-45
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 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.4.0
4-
Date: 2021-12-09
3+
Version: 0.4.1
4+
Date: 2021-12.17
55
Authors@R:
66
c(person(given = "Petter",
77
family = "Hopp",
@@ -21,6 +21,7 @@ LazyData: true
2121
Imports:
2222
checkmate,
2323
dplyr,
24+
magrittr,
2425
openxlsx,
2526
stats,
2627
NVIdb (>= 0.3.0),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export(check_OK_selection)
77
export(make_random)
88
export(style_sum_line)
99
export(write_ok_selection_list)
10+
importFrom(magrittr,"%>%")

NEWS

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
OKplan 0.4.1 - (2021-12-17)
2+
---------------------------
3+
4+
Bug fixes:
5+
6+
- adjust_samples_to_budget genereates the new column 'adjusted_sample' also when initial sample is correct
7+
8+
9+
Other changes:
10+
11+
- Included package level documentation.
12+
13+
114
OKplan 0.4.0 - (2021-12-09)
215
---------------------------
316

R/OKplan-package.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
## usethis namespace: start
5+
## usethis namespace: end
6+
NULL

R/adjust_samples_to_budget.R

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#' sample_to_adjust = "sample",
2828
#' adjusted_sample = "new_sample",
2929
#' adjust_by = 4)
30-
#'
31-
3230
adjust_samples_to_budget <- function(data, budget, sample_to_adjust, adjusted_sample = "justert_ant_prover", adjust_by) {
3331

3432
# ARGUMENT CHECKING ----
@@ -46,13 +44,18 @@ adjust_samples_to_budget <- function(data, budget, sample_to_adjust, adjusted_sa
4644
# INITILIZE VARIABLES ----
4745
total_estimated <- sum(data[, sample_to_adjust], na.rm = TRUE)
4846
n_units <- length(which(data[, sample_to_adjust] > 0))
49-
difference <- c(as.numeric(total_estimated - budget) ,rep(NA, dim(data)[1] - 1))
47+
difference <- c(as.numeric(total_estimated - budget), rep(NA, dim(data)[1] - 1))
5048

5149
# ADJUST SAMPLE NUMBER ----
5250
# Order data with largest sample size first
5351
data <- data[order(data[, sample_to_adjust], decreasing = TRUE), ]
5452

55-
# Only justify sample number when there is disagreement between budget and calculated number of samples
53+
# If total_estimated = budget, make new column adjusted_sample based on sample_to_adjust
54+
if (total_estimated == budget) {
55+
data[, adjusted_sample] <- data[, sample_to_adjust]
56+
}
57+
58+
# Only justify sample number when there is disagreement between budget and calculated number of samples
5659
if (total_estimated != budget) {
5760
# Adjust for each sampled unit with the unit having the largest sample size first
5861
for (i in c(1:dim(data)[1])) {
@@ -65,15 +68,15 @@ adjust_samples_to_budget <- function(data, budget, sample_to_adjust, adjusted_sa
6568
justify <- ifelse(difference[i] > 0, -adjust_by, adjust_by)
6669
} else {
6770
justify <- ifelse(difference[i] > 0,
68-
floor(- difference[i] / (n_units - i)),
69-
ceiling(- difference[i] / (n_units - i)))
71+
floor(-difference[i] / (n_units - i)),
72+
ceiling(-difference[i] / (n_units - i)))
7073
}
7174
if (difference[i] == 0) {justify <- 0}
7275

7376
# Make new column with adjusted number
7477
data[i, adjusted_sample] <- data[i, sample_to_adjust] + justify
7578
if (i < dim(data)[1]) {
76-
difference[i+1] <- difference[i] + justify
79+
difference[i + 1] <- difference[i] + justify
7780
}
7881

7982
}

R/append_date_generated_line.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
#' \dontrun{
1616
#' # Add row with generated date using standard values
1717
#' gris_virus_slaktegris_utvalg <- append_date_generated_line(gris_virus_slaktegris_utvalg)
18-
#'
1918
#' }
2019
#'
21-
append_date_generated_line <- function(data, pretext = "Datauttrekket er gjort", date = format(Sys.Date(),"%d/%m/%Y")) {
20+
append_date_generated_line <- function(data, pretext = "Datauttrekket er gjort", date = format(Sys.Date(), "%d/%m/%Y")) {
2221

2322
# Argument checking
2423
checks <- checkmate::makeAssertCollection()

R/append_sum_line.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#' position = "first")
2424
#' }
2525
#'
26-
27-
2826
append_sum_line <- function(data, column, pretext = "Sum", position = "left") {
2927

3028
# ARGUMENT CHECKING ----

R/check_OK_selection.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#'
1111
#' @param data Data frame with selection for a OK programme.
1212
#'
13+
#' @importFrom magrittr %>%
14+
#'
1315
#' @return Prints results of the control to the output window.
1416
#'
1517
#' @author Petter Hopp Petter.Hopp@@vetinst.no
@@ -29,10 +31,8 @@
2931
#'
3032
#' # Control
3133
#' check_OK_selection(okplan_MRSA)
32-
#'
3334
#' }
3435
#'
35-
#'
3636
check_OK_selection <- function(data) {
3737

3838
# Number of herds and samples that should be tested distributed on groups
@@ -92,5 +92,3 @@ check_OK_selection <- function(data) {
9292
# &omottatt
9393
#
9494
}
95-
96-

R/make_random.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#'
3030
#' # If you initialize again you get overlapping seeds
3131
#' x <- make_random(x, seed = seed, init_seed = TRUE)
32-
#'
3332
#' }
3433
#'
3534
make_random <- function(data, colname = "random", seed = -1, init_seed = FALSE) {

R/write_ok_selection_list.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ write_ok_selection_list <- function(data,
2929
# }
3030
checkmate::assert_character(sheet, min.chars = 1, min.len = 1, max.len = length(data), unique = TRUE, add = checks)
3131
checkmate::assert_character(filename, min.chars = 1, len = 1, add = checks)
32+
# Remove trailing backslash or slash before testing path
33+
filepath <- sub("\\\\{1,2}$|/{1,2}$", "", filepath)
3234
checkmate::assert_directory_exists(filepath, add = checks)
3335
checkmate::assert_logical(calculate_sum, any.missing = FALSE, min.len = 1, add = checks)
3436
checkmate::assert_character(dbsource, min.len = 1, add = checks)
@@ -52,19 +54,19 @@ write_ok_selection_list <- function(data,
5254
property = "colnames")
5355

5456
# order columns and keep only designated columns
55-
okdata <- NVIdb::standardize_columns(data = okdata,
57+
okdata <- NVIdb::standardize_columns(data = okdata,
5658
standards = OKplan::OK_column_standards,
5759
dbsource = dbsource,
5860
property = "colorder", exclude = TRUE)
5961

6062
# INCLUDE EXTRA INFORMATION ----
6163
# Append sum
62-
if(isTRUE(calculate_sum)) {
64+
if (isTRUE(calculate_sum)) {
6365
okdata <- append_sum_line(data = okdata, column = c("ant_prover"), position = "left")
6466
}
6567

6668
# Append date generated
67-
okdata <- append_date_generated_line(okdata)
69+
okdata <- append_date_generated_line(okdata)
6870

6971

7072
# STYLE EXCEL SHEET ----
@@ -78,11 +80,11 @@ write_ok_selection_list <- function(data,
7880
dbsource = dbsource)
7981

8082

81-
if(isTRUE(calculate_sum)) {
83+
if (isTRUE(calculate_sum)) {
8284
style_sum_line(workbook = okwb, sheet = sheet, data = okdata)
8385
}
8486
# }
8587
# SAVE EXCEL WORKBOOK ----
86-
openxlsx::saveWorkbook(wb = okwb, file = paste0(filepath, filename), overwrite = TRUE)
88+
openxlsx::saveWorkbook(wb = okwb, file = file.path(filepath, filename), overwrite = TRUE)
8789

8890
}

0 commit comments

Comments
 (0)