11
11
# ' @return A list containing information about truffles associated with the oak tree,
12
12
# ' including the total weight, date of the last truffle found, and any comments.
13
13
# '
14
- # ' @importFrom dplyr filter summarise pull
14
+ # ' @importFrom dplyr filter summarise pull arrange desc
15
15
# ' @export
16
16
# '
17
17
# ' @examples
24
24
# ' get_info_chene_truffe(dbtruffe = truffe, theidoak = "119")
25
25
# ' DBI::dbDisconnect(conn)
26
26
get_info_chene_truffe <- function (dbtruffe , theidoak ) {
27
- check_param(dbtruffe , " data.frame" )
28
- check_param(theidoak , " character" )
29
- check_names_dataframe(c(" idoak" , " weight" , " date_found" , " comment" ), dbtruffe )
27
+ check_param(dbtruffe , " data.frame" )
28
+ check_param(theidoak , " character" )
29
+ check_names_dataframe(c(
30
+ " idoak" , " weight" , " date_found" ,
31
+ " comment"
32
+ ), dbtruffe )
33
+ truffe_chene <- filter(dbtruffe , idoak == theidoak )
34
+ if (nrow(truffe_chene ) == 0 ) {
35
+ return (list (weight_tot = 0 , derniere_truffe = " -" , last_comment = " -" , other_comments = " -" ))
36
+ }
30
37
31
- truffe_chene <- dbtruffe | >
32
- filter(idoak == theidoak )
33
38
34
- if (nrow(truffe_chene ) == 0 ) {
35
- return (list (
36
- weight_tot = 0 ,
37
- derniere_truffe = " -" ,
38
- comments = " -"
39
- ))
40
- }
39
+ weight_tot <- pull(summarise(truffe_chene , weight_tot = sum(weight ,
40
+ na.rm = TRUE
41
+ )), weight_tot )
42
+ derniere_truffe <- pull(summarise(truffe_chene , date_found = as.Date(max(date_found ,
43
+ na.rm = TRUE
44
+ ))), date_found )
41
45
42
- weight_tot <- truffe_chene | >
43
- summarise(weight_tot = sum(weight , na.rm = TRUE )) | >
44
- pull(weight_tot )
46
+ sort_comment <- truffe_chene | >
47
+ arrange(desc(date_found ))
45
48
46
- derniere_truffe <- truffe_chene | >
47
- summarise(date_found = as.Date(max(date_found , na.rm = TRUE ))) | >
48
- pull(date_found )
49
-
50
- comments <-
51
- paste(
52
- paste(
53
- as.Date(truffe_chene $ date_found ),
54
- truffe_chene $ comment ,
49
+ last_comment <- paste(as.Date(sort_comment $ date_found [1 ]),
50
+ sort_comment $ comment [1 ],
55
51
sep = " : "
56
- ),
57
- collapse = " <br>"
58
52
)
59
53
60
- return (list (
61
- weight_tot = weight_tot ,
62
- derniere_truffe = derniere_truffe ,
63
- comments = comments
64
- ))
65
- }
54
+ if (nrow(sort_comment ) > 1 ) {
55
+ other_comments <- paste(paste(as.Date(sort_comment $ date_found [- 1 ]),
56
+ sort_comment $ comment [- 1 ],
57
+ sep = " : "
58
+ ), collapse = " <br>" )
59
+ } else {
60
+ other_comments <- " -"
61
+ }
62
+
63
+
64
+
65
+ return (list (
66
+ weight_tot = weight_tot ,
67
+ derniere_truffe = derniere_truffe ,
68
+ last_comment = last_comment ,
69
+ other_comments = other_comments
70
+ ))
71
+ }
0 commit comments