66# '
77# ' @param user_id user id
88# ' @param spot_id bathing spot id
9+ # ' @param require_hygiene logical. If \code{TRUE} (default) the function will
10+ # ' raise an error if there are no hygienic data
911# ' @export
1012# '
11- provide_input_data <- function (user_id , spot_id )
13+ provide_input_data <- function (user_id , spot_id , require_hygiene = TRUE )
1214{
15+ # kwb.utils::assignPackageObjects("fhpredict")
16+ # user_id=11;spot_id=57
17+
1318 # Define shortcut to kwb.utils::selectColumns()
1419 get <- kwb.utils :: selectColumns
1520
@@ -24,11 +29,9 @@ provide_input_data <- function(user_id, spot_id)
2429
2530 # Add microbiological measurements to the result or return if there are no
2631 # measurements
27- if (nrow(measurements ) == 0 ) {
28- clean_stop(get_text(
29- " no_measurements" , user_id = user_id , spot_id = spot_id
32+ if (nrow(measurements ) == 0 && require_hygiene ) clean_stop(
33+ get_text(" no_measurements" , user_id = user_id , spot_id = spot_id
3034 ))
31- }
3235
3336 # Create "hygiene" data frame
3437 result [[result_element(" hygiene" )]] <- data.frame (
@@ -72,15 +75,27 @@ provide_input_data <- function(user_id, spot_id)
7275 )
7376 }
7477
75- # Look for purification plant measurements
76- plant_measurements <- collect_series_measurements(
77- type = " plant" , prefix = " ka" , user_id , spot_id
78- )
78+ # Helper function to add further list entries (or not if length(x) == 0)
79+ add_series <- function (result , type , prefix , x ) {
7980
80- # Look for generic input measurements
81- generic_measurements <- collect_series_measurements(
82- type = " generic" , prefix = " gen" , user_id , spot_id
83- )
81+ x <- collect_series_measurements(type , prefix , user_id , spot_id )
82+
83+ if (length(x )) {
84+
85+ result [[result_element(prefix )]] <- kwb.utils :: renameColumns(
86+ merge_series_measurements(x ),
87+ list (dateTime = " datum" )
88+ )
89+ }
90+
91+ result
92+ }
93+
94+ # Add purification plant measurements if there are any
95+ result <- add_series(result , type = " plant" , prefix = " ka" )
96+
97+ # Add generic input measurements if there are any
98+ result <- add_series(result , type = " generic" , prefix = " gen" )
8499
85100 # Rename the elements in the result data frame?
86101
@@ -115,3 +130,19 @@ collect_series_measurements <- function(type, prefix, user_id, spot_id)
115130 )
116131 }
117132}
133+
134+ # merge_series_measurements ----------------------------------------------------
135+ merge_series_measurements <- function (x )
136+ {
137+ stopifnot(is.list(x ))
138+
139+ merged <- kwb.utils :: mergeAll(
140+ lapply(x , kwb.utils :: removeColumns , " id" ),
141+ by = " dateTime" ,
142+ all = TRUE
143+ )
144+
145+ names(merged ) <- gsub(" ^value\\ ." , " " , names(merged ))
146+
147+ merged
148+ }
0 commit comments