Skip to content

Commit 640c1ee

Browse files
authored
Merge pull request #38 from NorwegianVeterinaryInstitute/dev
NVIpjsr v0.2.1
2 parents 2289581 + 0c988d8 commit 640c1ee

19 files changed

+185
-52
lines changed

CONTRIBUTING.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,74 +20,74 @@ management and data reporting at the Norwegian Veterinary Institute
2020
<col style="width: 78%" />
2121
</colgroup>
2222
<thead>
23-
<tr class="header">
23+
<tr>
2424
<th style="text-align: left;">Package</th>
2525
<th style="text-align: left;">Status</th>
2626
<th style="text-align: left;">Description</th>
2727
</tr>
2828
</thead>
2929
<tbody>
30-
<tr class="odd">
30+
<tr>
3131
<td style="text-align: left;">NVIconfig</td>
3232
<td style="text-align: left;">Private</td>
3333
<td style="text-align: left;">Configuration information necessary for
3434
some NVIverse functions</td>
3535
</tr>
36-
<tr class="even">
36+
<tr>
3737
<td style="text-align: left;">NVIdb</td>
3838
<td style="text-align: left;">Public</td>
3939
<td style="text-align: left;">Tools to facilitate the use of NVI’s
4040
databases</td>
4141
</tr>
42-
<tr class="odd">
42+
<tr>
4343
<td style="text-align: left;">NVIpjsr</td>
4444
<td style="text-align: left;">Public</td>
4545
<td style="text-align: left;">Tools to facilitate working with PJS
4646
data</td>
4747
</tr>
48-
<tr class="even">
48+
<tr>
4949
<td style="text-align: left;">NVIspatial</td>
5050
<td style="text-align: left;">Public</td>
5151
<td style="text-align: left;">Tools to facilitate working with spatial
5252
data at NVI</td>
5353
</tr>
54-
<tr class="odd">
54+
<tr>
5555
<td style="text-align: left;">NVIpretty</td>
5656
<td style="text-align: left;">Public</td>
5757
<td style="text-align: left;">Tools to make R-output pretty in accord
5858
with NVI’s graphical profile</td>
5959
</tr>
60-
<tr class="even">
60+
<tr>
6161
<td style="text-align: left;">NVIbatch</td>
6262
<td style="text-align: left;">Public</td>
6363
<td style="text-align: left;">Tools to facilitate the running of
6464
R-scripts in batch mode at NVI</td>
6565
</tr>
66-
<tr class="odd">
66+
<tr>
6767
<td style="text-align: left;">OKplan</td>
6868
<td style="text-align: left;">Public</td>
6969
<td style="text-align: left;">Tools to facilitate the planning of
7070
surveillance programmes for the NFSA</td>
7171
</tr>
72-
<tr class="even">
72+
<tr>
7373
<td style="text-align: left;">OKcheck</td>
7474
<td style="text-align: left;">Public</td>
7575
<td style="text-align: left;">Tools to facilitate checking of data from
7676
national surveillance programmes</td>
7777
</tr>
78-
<tr class="odd">
78+
<tr>
7979
<td style="text-align: left;">NVIcheckmate</td>
8080
<td style="text-align: left;">Public</td>
8181
<td style="text-align: left;">Extension of checkmate with argument
8282
checking adapted for NVIverse</td>
8383
</tr>
84-
<tr class="even">
84+
<tr>
8585
<td style="text-align: left;">NVIpackager</td>
8686
<td style="text-align: left;">Public</td>
8787
<td style="text-align: left;">Tools to facilitate the development of
8888
NVIverse packages</td>
8989
</tr>
90-
<tr class="odd">
90+
<tr>
9191
<td style="text-align: left;">NVIrpackages</td>
9292
<td style="text-align: left;">Public</td>
9393
<td style="text-align: left;">Keeps a table of the R packages in

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: NVIpjsr
22
Title: Tools to facilitate working with PJS data
3-
Version: 0.2.0
4-
Date: 2025-02-07
3+
Version: 0.2.1
4+
Date: 2025-09-11
55
Authors@R: c(
66
person("Petter", "Hopp", , "Petter.Hopp@vetinst.no", role = c("aut", "cre"),
77
comment = c(ORCID = "0000-0002-8695-0378")),
@@ -20,7 +20,7 @@ Depends:
2020
Encoding: UTF-8
2121
Language: en-GB
2222
Roxygen: list(markdown = FALSE)
23-
RoxygenNote: 7.3.1
23+
RoxygenNote: 7.3.2
2424
Imports:
2525
checkmate (>= 2.1.0),
2626
data.table,

NEWS.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
# NVIpjsr 0.2.1 - (2025-09-11)
2+
3+
## New features:
4+
5+
- `add_PJS_code_description` will now translate "delpr_forbehandlingkode" when the arguments `new_column` = `"auto"` and/or `PJS_variable_type` = `"auto"`.
6+
7+
8+
## Bug fixes:
9+
10+
- `build_query_one_disease` now accepts metodekode with 2 characters only.
11+
12+
- `build_query_outbreak` now accepts metodekode with 2 characters only.
13+
14+
- `build_query_outbreak` now accepts that hensikt = NULL.
15+
16+
- `retrieve_PJSdata` is updated to handle views updated to include "anamnese" when the sql-statement is generated by one of the `build_query`-functions.
17+
18+
119
# NVIpjsr 0.2.0 - (2025-02-07)
220

321
## New features:
@@ -25,11 +43,6 @@
2543
- Improved examples in help for several functions.
2644

2745

28-
## BREAKING CHANGES:
29-
30-
-
31-
32-
3346
# NVIpjsr 0.1.2 - (2024-08-20)
3447

3548
## Bug fixes:

R/add_PJS_code_description.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#' provemateriale \tab provematerialekode \tab provemateriale \tab \cr
4747
#' provemateriale \tab delpr_provematerialekode \tab delprovemateriale \tab \cr
4848
#' forbehandling \tab forbehandlingkode \tab forbehandling \tab \cr
49+
#' forbehandling \tab delpr_forbehandlingkode \tab delpr_forbehandling \tab \cr
4950
#' metode \tab metodekode \tab metode \tab \cr
5051
#' metode \tab subund_metodekode \tab submetode \tab \cr
5152
#' konkl_type \tab konkl_typekode \tab konkl_type \tab \cr

R/build_query_one_disease.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ build_query_one_disease <- function(year, analytt, hensikt = NULL, metode = NULL
7878
checkmate::assert_integerish(year, lower = 1990, upper = as.numeric(format(Sys.Date(), "%Y")), min.len = 1, add = checks)
7979
checkmate::assert_character(analytt, min.chars = 2, any.missing = FALSE, add = checks)
8080
checkmate::assert_character(hensikt, min.chars = 2, null.ok = TRUE, any.missing = FALSE, add = checks)
81-
checkmate::assert_character(metode, min.chars = 6, null.ok = TRUE, any.missing = FALSE, add = checks)
81+
checkmate::assert_character(metode, min.chars = 2, null.ok = TRUE, any.missing = FALSE, add = checks)
8282
checkmate::assert_choice(db, choices = c("PJS"), add = checks)
8383

8484
# Report check-results

R/build_query_outbreak.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ build_query_outbreak <- function(period,
8282
checkmate::assert_character(hensikt, min.chars = 2, null.ok = TRUE, any.missing = FALSE, add = checks)
8383
checkmate::assert_character(analytt, min.chars = 2, null.ok = TRUE, any.missing = FALSE, add = checks)
8484
NVIcheckmate::assert_non_null(x = list(utbrudd, hensikt, analytt), add = checks)
85-
checkmate::assert_character(metode, min.chars = 6, null.ok = TRUE, any.missing = FALSE, add = checks)
85+
checkmate::assert_character(metode, min.chars = 2, null.ok = TRUE, any.missing = FALSE, add = checks)
8686
checkmate::assert_choice(db, choices = c("PJS"), add = checks)
8787

8888
# Report check-results
@@ -92,7 +92,11 @@ build_query_outbreak <- function(period,
9292
select_year <- build_sql_select_year(year = period, varname = "aar")
9393

9494
select_hensikt <- build_sql_select_code(values = hensikt, varname = "hensiktkode")
95-
if (nchar(select_hensikt) > 0) {select_codes <- select_hensikt}
95+
if (nchar(select_hensikt) > 0) {
96+
select_codes <- select_hensikt
97+
} else {
98+
select_codes <- ""
99+
}
96100

97101
# Select utbruddsid
98102
select_utbrudd <- build_sql_select_code(values = utbrudd, varname = "utbrudd_id")

R/data.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#' @title Data: Variables per PJS-level.
22
#'
3-
#' @description A data frame with the variable names (column names) in PJS and their corresponding PJS-level. The column names
4-
#' are the standardized column names, i.e. after running
3+
#' @description A data frame with the variable names (column names) in PJS and
4+
#' their corresponding PJS-level. The column names are the standardized
5+
#' column names, i.e. after running
56
#' \ifelse{html}{\code{\link[NVIdb:standardize_columns]{NVIdb::standardize_columns}}}{\code{NVIdb::standardize_columns}}.
6-
#' The raw data can be edited in the "./data-raw/PJS_levels.xlsx" and the the code for preparing of the data frame is written in
7-
#' "./data-raw/generate_PJS_levels.R". The \code{PJS_levels} is used as input for \code{\link{choose_PJS_levels}}.
7+
#' The raw data can be edited in the "./data-raw/PJS_levels.xlsx" and the the
8+
#' code for preparing of the data frame is written in
9+
#' "./data-raw/generate_PJS_levels.R". The \code{PJS_levels} is used as input
10+
#' for \code{\link{choose_PJS_levels}}.
811
#'
9-
#' @details The variables included into a specific level is given the value 1, if not included they are given the value 0. To
10-
#' ensure that information on a specific level an be traced to the correct sak, all index variables are given value 1.
12+
#' @details The variables included into a specific level is given the value 1,
13+
#' if not included they are given the value 0. To ensure that information on
14+
#' a specific level an be traced to the correct sak, all index variables are
15+
#' given value 1. The first ten variables in the data are index variables.
1116
#'
1217
#' @format A data frame with 9 variables:
1318
#' \describe{

R/report_selection_parameters.R

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ report_selection_parameters <- function(year = NULL,
6363
add = checks)
6464
NVIcheckmate::assert(checkmate::check_file_exists(x = selection_parameters, access = "r"),
6565
checkmate::check_list(x = selection_parameters, min.len = 1,
66-
all.missing = FALSE),
66+
all.missing = FALSE),
6767
combine = "or",
6868
comment = "The argument selection_parameter must either be a file with selection parameters or a list with selection parameters",
6969
add = checks)
@@ -190,23 +190,11 @@ list_parameters <- function(varname = NULL, variables, PJS_variable_type, transl
190190
}
191191
parameters$Variable <- PJS_variable_type
192192
parameters <- NVIpjsr::add_PJS_code_description(parameters,
193-
translation_table = translation_table,
194-
PJS_variable_type = tolower(PJS_variable_type),
195-
code_colname = "kode_stripped",
196-
new_column = "Beskrivelse")
193+
translation_table = translation_table,
194+
PJS_variable_type = tolower(PJS_variable_type),
195+
code_colname = "kode_stripped",
196+
new_column = "Beskrivelse")
197197
parameters <- parameters[, c("Status", "Variable", "Kode", "Beskrivelse")]
198-
199-
# parameters <- as.data.frame(matrix(c(variables),
200-
# nrow = length(c(variables)),
201-
# dimnames = list(NULL, "Kode"))) %>%
202-
# dplyr::mutate(kode_stripped = gsub("%", "", Kode)) %>%
203-
# dplyr::mutate(Status = dplyr::case_when(grepl(pattern = "select", x = varname, ignore.case = TRUE) ~ "Selektert",
204-
# grepl(pattern = "delete", x = varname, ignore.case = TRUE) ~ "Ekskludert")) %>%
205-
# dplyr::mutate(Variable = PJS_variable_type) %>%
206-
# add_PJS_code_description(PJS_variable_type = tolower(PJS_variable_type),
207-
# code_colname = "kode_stripped",
208-
# new_column = "Beskrivelse") %>%
209-
# dplyr::select(Status, Variable, Kode, Beskrivelse)
210198
}
211199

212200
return(parameters)

R/retrieve_PJSdata.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@
4949
#' and a function with the necessary selection_parameters are given,
5050
#' the select_statement constructed by the function will be used.
5151
#'
52+
#' Be aware that there is a known problem for the R-package "odbc" when selecting
53+
#' long text variables like "anamnese". Such variables need to be put last in
54+
#' the select statement. The variable "anamnese" is included in the main views
55+
#' used for retrieving data from PJS. \code{retrieve_PJSdata} will rewrite
56+
#' select statements generated by the \code{build_query}-functions and sql
57+
#' statements starting with "SELECT * FROM 'tablename'", but will not rewrite
58+
#' the sql statement in other situations. If this happens, you must either
59+
#' rewrite the sql statement and put "anamnese" last in the select statement
60+
#' or use the R-package "RODBC" instead. If using "RODBC", you cannot use
61+
#' \code{retrieve_PJSdata}.
62+
#'
5263
#' The output is a named list where each entry is a data frame with PJS data. If
5364
#' the select statement is named, the returned data frame will have that name.
5465
#' If the select statement is unnamed, it will try to identify the first
@@ -237,6 +248,25 @@ retrieve_PJSdata <- function(year = NULL,
237248
# PERFORM SELECTION AND STANDARDISATION FOR EACH SELECT STATEMENT ----
238249
for (i in c(1:length(select_statement))) {
239250

251+
# MOVE anamnese LAST IN SELECTION STATEMENT ----
252+
# Identify table in the first select clause in sql statement
253+
db_table <- sub("SELECT[[:space:]]*\\*[[:space:]]*FROM[[:space:]]*([^[:space:]]*).*",
254+
"\\1",
255+
select_statement[[i]], ignore.case = TRUE)
256+
# List fields in the db_table
257+
if (nchar(db_table) > 0 && regexpr("[[:space:],\\*]", db_table) < 0) {
258+
fields <- DBI::dbListFields(conn = journal_rapp, name = db_table)
259+
# Put anamnese last in select clause if exist in the db_table
260+
if ("anamnese" %in% tolower(fields)) {
261+
fields <- paste0(fields, collapse = ", ")
262+
fields <- paste0(sub("anamnese, ", "", fields, ignore.case = TRUE), ", anamnese")
263+
select_statement[[i]] <- sub(paste0("SELECT[[:space:]]*\\*[[:space:]]*FROM[[:space:]]*", db_table),
264+
paste("SELECT", fields, "FROM", db_table),
265+
select_statement[[i]],
266+
ignore.case = TRUE)
267+
}
268+
}
269+
240270
# READ DATA FROM PJS ----
241271
PJSdata[[i]] <- DBI::dbGetQuery(con = journal_rapp,
242272
statement = select_statement[[i]])

data-raw/PJS_levels.xlsx

429 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)