@@ -33,8 +33,8 @@ setGlobalInspectionID <- function(
33
33
}
34
34
35
35
# Just a shortcut
36
- removeEmpty <- function (df ) kwb.utils :: removeEmptyColumns(df , dbg = FALSE )
37
-
36
+ removeEmpty <- function (x ) kwb.utils :: removeEmptyColumns(x , dbg = FALSE )
37
+
38
38
inspections <- removeEmpty(get_elements(inspection.data , " inspections" ))
39
39
observations <- removeEmpty(get_elements(inspection.data , " observations" ))
40
40
@@ -48,87 +48,76 @@ setGlobalInspectionID <- function(
48
48
snake = " inspection_time"
49
49
))
50
50
51
- inspections <- kwb.utils :: hsAddMissingCols(inspections , timeColumn )
52
-
53
- hasNoTime <- kwb.utils :: isNaOrEmpty(inspections [[timeColumn ]])
54
-
55
- if (any(hasNoTime )) {
56
-
57
- n_missing <- sum(hasNoTime )
58
-
59
- message(sprintf(
60
- " Setting %d missing inspection times to '%s' (plus random seconds)." ,
61
- n_missing ,
62
- default.time
63
- ))
64
-
65
- # We have to fix the random number generator otherwise the times are not
66
- # reproducible!
67
- set.seed(123L )
68
-
69
- # Generate a random number for the seconds
70
- inspections [[timeColumn ]][hasNoTime ] <- sprintf(
71
- " %s:%02d" ,
72
- default.time ,
73
- sample(0 : 59 , size = n_missing , replace = TRUE )
74
- )
75
- }
76
-
77
- # Columns from which to generate the hash code
78
- columns <- get_elements(elements = name.convention , list (
79
- norm = c(
80
- " project" ,
81
- " ABF" ,
82
- " ABG" ,
83
- " AAD" ,
84
- " AAF"
85
- ),
86
- camel = c(
87
- " project" ,
88
- " InspDate" ,
89
- " InspTime" ,
90
- " Node1Ref" ,
91
- " Node2Ref"
92
- ),
93
- snake = c(
94
- " project" ,
95
- " inspection_date" ,
96
- " inspection_time" ,
97
- " node_1_ref" ,
98
- " node_2_ref"
99
- )
100
- ))
51
+ inspections [[timeColumn ]] <- fillTimeVector(
52
+ x = if (timeColumn %in% names(inspections )) {
53
+ inspections [[timeColumn ]]
54
+ } else {
55
+ character (nrow(inspections ))
56
+ },
57
+ hhmm = default.time ,
58
+ seed = 123L
59
+ )
101
60
102
61
# Create the inspection IDs and store them in column "inspection_id"
103
62
hashes <- createHashFromColumns(
104
63
data = inspections ,
105
- columns = columns ,
64
+ # Columns from which to generate the hash code
65
+ columns = get_elements(elements = name.convention , list (
66
+ norm = c(" project" , " ABF" , " ABG" , " AAD" , " AAF" ),
67
+ camel = c(" project" , " InspDate" , " InspTime" , " Node1Ref" , " Node2Ref" ),
68
+ snake = c(" project" , " inspection_date" , " inspection_time" , " node_1_ref" , " node_2_ref" )
69
+ )),
106
70
silent = TRUE
107
71
)
108
72
109
73
# Check for duplicates in the hashes
110
74
stop_on_hash_duplicates(hashes , error.file = error.file )
111
-
75
+
112
76
inspections [[" inspection_id" ]] <- hashes
113
-
77
+
114
78
i <- kwb.utils :: selectColumns(observations , " inspno" )
115
79
116
80
observations [[" inspection_id" ]] <- kwb.utils :: selectColumns(
117
81
inspections , " inspection_id"
118
82
)[i ]
119
83
120
84
observations <- kwb.utils :: removeColumns(observations , " inspno" )
121
-
85
+
122
86
# Just a shortcut
123
87
id_first <- function (x ) kwb.utils :: moveColumnsToFront(x , " inspection_id" )
124
-
88
+
125
89
list (
126
90
header.info = get_elements(inspection.data , " header.info" ),
127
91
inspections = id_first(inspections ),
128
92
observations = id_first(observations )
129
93
)
130
94
}
131
95
96
+ # fillTimeVector ---------------------------------------------------------------
97
+ fillTimeVector <- function (x , hhmm = " 22:22" , seed = NULL , silent = FALSE )
98
+ {
99
+ if (any(isEmpty <- kwb.utils :: isNaOrEmpty(x ))) {
100
+
101
+ if (! silent ) {
102
+ message(sprintf(
103
+ " Setting %d missing inspection times to '%s' (plus random seconds)." ,
104
+ sum(isEmpty ), hhmm
105
+ ))
106
+ }
107
+
108
+ # Fix the random number generator to be reproducible
109
+ if (! is.null(seed )) {
110
+ set.seed(seed )
111
+ }
112
+
113
+ # Generate a random number for the seconds.
114
+ seconds <- sample(0 : 59 , size = sum(isEmpty ), replace = TRUE )
115
+ x [isEmpty ] <- sprintf(" %s:%02d" , hhmm , seconds )
116
+ }
117
+
118
+ x
119
+ }
120
+
132
121
# stop_on_hash_duplicates ------------------------------------------------------
133
122
134
123
# '@importFrom utils capture.output
0 commit comments