| 1 |
#' Class "report_silver_eel" |
|
| 2 |
#' |
|
| 3 |
#' the report_silver_eel class is used to calculate various statistics about the silver eel run. It comprises calculation |
|
| 4 |
#' of various maturation index such as Durif's stages and Pankhurst eye index. The objective is to provide standardized |
|
| 5 |
#' output to the stations monitoring the silver eel run. |
|
| 6 |
#' @include create_generic.R |
|
| 7 |
#' @include ref_dc.R |
|
| 8 |
#' @include ref_taxa.R |
|
| 9 |
#' @include ref_stage.R |
|
| 10 |
#' @include ref_horodate.R |
|
| 11 |
#' @include ref_par.R |
|
| 12 |
#' @note This class is displayed by interface_report_silver_eel |
|
| 13 |
#' @slot data A data frame with data generated from the database |
|
| 14 |
#' @slot calcdata A list of dc with processed data. Each dc contains a data frame with |
|
| 15 |
#' \itemize{
|
|
| 16 |
#' \item (1) qualitative data on body contrast (CONT), presence of punctuation on the lateral line (LINP) |
|
| 17 |
#' \item (2) quantitative data "BL" Body length,"W" weight,"Dv" vertical eye diameter,"Dh" horizontal eye diameter,"FL" pectoral fin length |
|
| 18 |
#' \item (3) calculated durif stages, Pankhurst's index, Fulton's body weight coefficient K_ful |
|
| 19 |
#' \item (4) other columns containing data pertaining to the sample and the control operation: lot_identifiant,ope_identifiant, |
|
| 20 |
#' ope_dic_identifiant,ope_date_debut,ope_date_fin,dev_code (destination code of fish), |
|
| 21 |
#' dev_libelle (text for destination of fish) |
|
| 22 |
#' } |
|
| 23 |
#' @slot dc Object of class \link{ref_dc-class}: the control devices
|
|
| 24 |
#' @slot taxa An object of class \link{ref_taxa-class}: the species
|
|
| 25 |
#' @slot stage An object of class \link{ref_stage-class} : the stages of the fish
|
|
| 26 |
#' @slot par An object of class \link{ref_par-class}: the parameters used
|
|
| 27 |
#' @slot horodatedebut An object of class \link{ref_horodate-class}
|
|
| 28 |
#' @slot horodatefin An object of class \link{ref_horodate-class}
|
|
| 29 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 30 |
#' \code{new("report_silver_eel", ...)}
|
|
| 31 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 32 |
#' @family report Objects |
|
| 33 |
#' @keywords classes |
|
| 34 |
#' @example inst/examples/report_silver_eel-example.R |
|
| 35 |
#' @aliases report_silver_eel |
|
| 36 |
#' @export |
|
| 37 |
setClass( |
|
| 38 |
Class = "report_silver_eel", |
|
| 39 |
representation = representation( |
|
| 40 |
data = "data.frame", |
|
| 41 |
calcdata = "list", |
|
| 42 |
dc = "ref_dc", |
|
| 43 |
taxa = "ref_taxa", |
|
| 44 |
stage = "ref_stage", |
|
| 45 |
par = "ref_par", |
|
| 46 |
horodatedebut = "ref_horodate", |
|
| 47 |
horodatefin = "ref_horodate" |
|
| 48 |
), |
|
| 49 |
prototype = prototype( |
|
| 50 |
data = data.frame(), |
|
| 51 |
calcdata = list(), |
|
| 52 |
dc = new("ref_dc"),
|
|
| 53 |
taxa = new("ref_taxa"),
|
|
| 54 |
stage = new("ref_stage"),
|
|
| 55 |
par = new("ref_par"),
|
|
| 56 |
horodatedebut = new("ref_horodate"),
|
|
| 57 |
horodatefin = new("ref_horodate")
|
|
| 58 |
) |
|
| 59 |
) |
|
| 60 |
setValidity("report_silver_eel", function(object)
|
|
| 61 |
{
|
|
| 62 |
rep1 = object@taxa@taxa_selected[1] == '2038' |
|
| 63 |
label1 <- |
|
| 64 |
'report_silver_eel should only be for eel (tax_code=2038)' |
|
| 65 |
rep2 = all(object@stage@stage_selected %in% c('AGG', 'AGJ'))
|
|
| 66 |
label2 <- |
|
| 67 |
'Only stages silver (AGG) and yellow (AGJ) should be used in report_silver_eel' |
|
| 68 |
return(ifelse(rep1 & |
|
| 69 |
rep2 , TRUE , c(label1, label2)[!c(rep1, rep2)])) |
|
| 70 |
}) |
|
| 71 |
#' connect method for report_silver_eel |
|
| 72 |
#' |
|
| 73 |
#' @param object An object of class \link{report_silver_eel-class}
|
|
| 74 |
#' @param silent Boolean if TRUE messages are not displayed |
|
| 75 |
#' @return An object of class \link{report_silver_eel-class} with slot data \code{@data} filled
|
|
| 76 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 77 |
#' @aliases connect.report_silver_eel |
|
| 78 |
setMethod( |
|
| 79 |
"connect", |
|
| 80 |
signature = signature("report_silver_eel"),
|
|
| 81 |
definition = function(object, silent = FALSE) {
|
|
| 82 | 1x |
requete <- new("RequeteDBwheredate")
|
| 83 | 1x |
requete@select = paste("SELECT * FROM ",
|
| 84 | 1x |
get_schema(), |
| 85 | 1x |
"vue_lot_ope_car", |
| 86 | 1x |
sep = "") |
| 87 | 1x |
requete@colonnedebut = "ope_date_debut" |
| 88 | 1x |
requete@colonnefin = "ope_date_fin" |
| 89 | 1x |
requete@datedebut <- object@horodatedebut@horodate |
| 90 | 1x |
requete@datefin <- object@horodatefin@horodate |
| 91 | 1x |
requete@order_by = "ORDER BY ope_date_debut" |
| 92 | 1x |
requete@and = paste( |
| 93 | 1x |
" AND ope_dic_identifiant in ", |
| 94 | 1x |
vector_to_listsql(object@dc@dc_selected), |
| 95 | 1x |
" AND lot_tax_code in ", |
| 96 | 1x |
vector_to_listsql(object@taxa@taxa_selected), |
| 97 | 1x |
" AND lot_std_code in ", |
| 98 | 1x |
vector_to_listsql(object@stage@stage_selected), |
| 99 | 1x |
" AND car_par_code in ", |
| 100 | 1x |
vector_to_listsql(object@par@par_selected), |
| 101 | 1x |
sep = "" |
| 102 |
) |
|
| 103 | 1x |
requete <- stacomirtools::query(requete) |
| 104 | 1x |
object@data <- requete@query |
| 105 | 1x |
if (!silent) |
| 106 | ! |
funout(gettext("Data loaded", domain = "R-stacomiR"))
|
| 107 | 1x |
return(object) |
| 108 |
} |
|
| 109 |
) |
|
| 110 | ||
| 111 | ||
| 112 |
#' charge method for report_silver_eel class |
|
| 113 |
#' |
|
| 114 |
#' this method verifies that boxes have been clicked in the user interface and gets the objects pasted in |
|
| 115 |
#' envir_stacomi. It is not necessary to run this method when loading from the command line using the |
|
| 116 |
#' choice_c method |
|
| 117 |
#' @param object An object of class \link{report_silver_eel-class}
|
|
| 118 |
#' @param h a handler |
|
| 119 |
#' @return An object of class \link{report_silver_eel-class} with slots filled from values assigned in \code{envir_stacomi} environment
|
|
| 120 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 121 |
#' @return An object of the class |
|
| 122 |
#' @aliases charge.report_silver_eel |
|
| 123 |
#' @keywords internal |
|
| 124 |
setMethod( |
|
| 125 |
"charge", |
|
| 126 |
signature = signature("report_silver_eel"),
|
|
| 127 |
definition = function(object, h) {
|
|
| 128 | ! |
if (exists("ref_dc", envir_stacomi)) {
|
| 129 | ! |
object@dc <- get("ref_dc", envir_stacomi)
|
| 130 |
} else {
|
|
| 131 | ! |
funout( |
| 132 | ! |
gettext( |
| 133 | ! |
"You need to choose a counting device, clic on validate\n", |
| 134 | ! |
domain = "R-stacomiR" |
| 135 |
), |
|
| 136 | ! |
arret = TRUE |
| 137 |
) |
|
| 138 |
} |
|
| 139 | ! |
if (exists("ref_taxa", envir_stacomi)) {
|
| 140 | ! |
object@taxa <- get("ref_taxa", envir_stacomi)
|
| 141 |
} else {
|
|
| 142 | ! |
funout( |
| 143 | ! |
gettext("You need to choose a taxa, clic on validate\n", domain = "R-stacomiR"),
|
| 144 | ! |
arret = TRUE |
| 145 |
) |
|
| 146 |
} |
|
| 147 | ! |
if (exists("ref_stage", envir_stacomi)) {
|
| 148 | ! |
object@stage <- get("ref_stage", envir_stacomi)
|
| 149 |
} else {
|
|
| 150 | ! |
funout( |
| 151 | ! |
gettext("You need to choose a stage, clic on validate\n", domain = "R-stacomiR"),
|
| 152 | ! |
arret = TRUE |
| 153 |
) |
|
| 154 |
} |
|
| 155 | ! |
if (exists("ref_par", envir_stacomi)) {
|
| 156 | ! |
object@par <- get("ref_par", envir_stacomi)
|
| 157 |
} else {
|
|
| 158 | ! |
funout( |
| 159 | ! |
gettext("You need to choose a parameter, clic on validate\n", domain = "R-stacomiR"),
|
| 160 | ! |
arret = TRUE |
| 161 |
) |
|
| 162 |
} |
|
| 163 | ! |
if (exists("report_arg_date_debut", envir_stacomi)) {
|
| 164 | ! |
object@horodatedebut@horodate <- |
| 165 | ! |
get("report_arg_date_debut", envir_stacomi)
|
| 166 |
} else {
|
|
| 167 | ! |
funout(gettext("You need to choose the starting date\n", domain = "R-stacomiR"),
|
| 168 | ! |
arret = TRUE) |
| 169 |
} |
|
| 170 | ! |
if (exists("report_arg_date_fin", envir_stacomi)) {
|
| 171 | ! |
object@horodatefin@horodate <- |
| 172 | ! |
get("report_arg_date_fin", envir_stacomi)
|
| 173 |
} else {
|
|
| 174 | ! |
funout(gettext("You need to choose the ending date\n", domain = "R-stacomiR"),
|
| 175 | ! |
arret = TRUE) |
| 176 |
} |
|
| 177 |
|
|
| 178 | ! |
return(object) |
| 179 | ! |
validObject(object) |
| 180 |
} |
|
| 181 |
) |
|
| 182 | ||
| 183 | ||
| 184 |
#' command line interface for report_silver_eel class |
|
| 185 |
#' |
|
| 186 |
#' #' The choice_c method fills in the data slot for classes \link{ref_dc-class}, \link{ref_taxa-class}, \link{ref_stage-class}, \link{ref_par-class} and two slots of \link{ref_horodate-class} and then
|
|
| 187 |
#' uses the choice_c methods of these object to select the data. |
|
| 188 |
#' @param object An object of class \link{report_silver_eel-class}
|
|
| 189 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 190 |
#' @param taxa '2038=Anguilla anguilla', |
|
| 191 |
#' these should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 192 |
#' @param stage 'AGG' |
|
| 193 |
#' @param par Parameters chosen for the report are body size (1786), vertical eye diameter (BBBB), horizontal eye diameter (CCCC), |
|
| 194 |
#' body contrast (CONT), presence of punctuation on the lateral line (LINP), length of the pectoral fin (PECT) |
|
| 195 |
#' @param horodatedebut The starting date as a character, formats like \code{\%Y-\%m-\%d} or \code{\%d-\%m-\%Y} can be used as input
|
|
| 196 |
#' @param horodatefin The finishing date of the report, for this class this will be used to calculate the number of daily steps. |
|
| 197 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 198 |
#' @return An object of class \link{report_mig-class}
|
|
| 199 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 200 |
#' @aliases choice_c.report_silver_eel |
|
| 201 |
setMethod( |
|
| 202 |
"choice_c", |
|
| 203 |
signature = signature("report_silver_eel"),
|
|
| 204 |
definition = function(object, |
|
| 205 |
dc, |
|
| 206 |
taxa = 2038, |
|
| 207 |
stage = 'AGG', |
|
| 208 |
par = c('1786', 'CCCC', 'BBBB', 'CONT', 'LINP', 'A111', 'PECT'),
|
|
| 209 |
horodatedebut, |
|
| 210 |
horodatefin, |
|
| 211 |
silent = FALSE) {
|
|
| 212 |
# code for debug using example |
|
| 213 |
#r_silver<-b_carlothorodatedebut="2010-01-01";horodatefin="2015-12-31" |
|
| 214 | 1x |
r_silver <- object |
| 215 | 1x |
r_silver@dc = charge(r_silver@dc) |
| 216 |
# loads and verifies the dc |
|
| 217 |
# this will set dc_selected slot |
|
| 218 | 1x |
r_silver@dc <- choice_c(object = r_silver@dc, dc) |
| 219 |
# only taxa present in the report_mig are used |
|
| 220 | 1x |
r_silver@taxa <- |
| 221 | 1x |
charge_with_filter(object = r_silver@taxa, r_silver@dc@dc_selected) |
| 222 | 1x |
r_silver@taxa <- choice_c(r_silver@taxa, taxa) |
| 223 | 1x |
r_silver@stage <- |
| 224 | 1x |
charge_with_filter(object = r_silver@stage, |
| 225 | 1x |
r_silver@dc@dc_selected, |
| 226 | 1x |
r_silver@taxa@taxa_selected) |
| 227 | 1x |
r_silver@stage <- choice_c(r_silver@stage, stage) |
| 228 | 1x |
r_silver@par <- |
| 229 | 1x |
charge_with_filter( |
| 230 | 1x |
object = r_silver@par, |
| 231 | 1x |
r_silver@dc@dc_selected, |
| 232 | 1x |
r_silver@taxa@taxa_selected, |
| 233 | 1x |
r_silver@stage@stage_selected |
| 234 |
) |
|
| 235 | 1x |
r_silver@par <- choice_c(r_silver@par, par, silent = silent) |
| 236 | 1x |
r_silver@horodatedebut <- choice_c( |
| 237 | 1x |
object = r_silver@horodatedebut, |
| 238 | 1x |
nomassign = "reportArg_date_debut", |
| 239 | 1x |
funoutlabel = gettext("Beginning date has been chosen\n", domain = "R-stacomiR"),
|
| 240 | 1x |
horodate = horodatedebut, |
| 241 | 1x |
silent = silent |
| 242 |
) |
|
| 243 | 1x |
r_silver@horodatefin <- choice_c( |
| 244 | 1x |
r_silver@horodatefin, |
| 245 | 1x |
nomassign = "reportArg_date_fin", |
| 246 | 1x |
funoutlabel = gettext("Ending date has been chosen\n", domain = "R-stacomiR"),
|
| 247 | 1x |
horodate = horodatefin, |
| 248 | 1x |
silent = silent |
| 249 |
) |
|
| 250 | 1x |
validObject(r_silver) |
| 251 | 1x |
return(r_silver) |
| 252 |
} |
|
| 253 |
) |
|
| 254 | ||
| 255 |
#' Calculate individual silver eel parameters. |
|
| 256 |
#' |
|
| 257 |
#' This calcule method for report_silver_eel, will transform data from long (one line per size characteristic, |
|
| 258 |
#' size, weight, eye diameter, pectoral fin measurement, lateral line and constrast) to wide format (one |
|
| 259 |
#' line per silver eel). It will also calculate Durif silvering index and Pankhurst and Fulton's K. |
|
| 260 |
#' |
|
| 261 |
#' @param object An object of class \link{report_silver_eel-class}
|
|
| 262 |
#' @param silent Boolean, if TRUE, information messages are not displayed, only warnings and errors |
|
| 263 |
#' @return An object of class \link{report_silver_eel-class} with slot calcdata filled, as a list
|
|
| 264 |
#' for each counting device |
|
| 265 |
#' @aliases calcule.report_silver_eel |
|
| 266 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 267 |
setMethod( |
|
| 268 |
"calcule", |
|
| 269 |
signature = signature("report_silver_eel"),
|
|
| 270 |
definition = function(object, silent) {
|
|
| 271 | 3x |
r_silver <- object |
| 272 | 3x |
if (nrow(r_silver@data) == 0) {
|
| 273 | ! |
funout( |
| 274 | ! |
gettext("No data of silver or yellow eel on the selected period", domain =
|
| 275 | ! |
"R-stacomiR"), |
| 276 | ! |
arret = TRUE |
| 277 |
) |
|
| 278 |
} |
|
| 279 | 3x |
arg = r_silver@data |
| 280 | 3x |
lesdc <- r_silver@dc@dc_selected |
| 281 | 3x |
parquant <- c("1786", "A111", "BBBB", "CCCC", "PECT")
|
| 282 | 3x |
parqual <- c("CONT", "LINP")
|
| 283 | 3x |
for (i in 1:length(lesdc)) {
|
| 284 | 6x |
dc <- lesdc[i] |
| 285 | 6x |
other <- |
| 286 | 6x |
dplyr::select( |
| 287 | 6x |
arg, |
| 288 | 6x |
lot_identifiant, |
| 289 | 6x |
ope_dic_identifiant, |
| 290 | 6x |
ope_identifiant, |
| 291 | 6x |
ope_date_debut, |
| 292 | 6x |
ope_date_fin, |
| 293 | 6x |
dev_code, |
| 294 | 6x |
dev_libelle |
| 295 |
) |
|
| 296 | 6x |
other <- dplyr::filter(other, ope_dic_identifiant == dc) |
| 297 | 6x |
other <- |
| 298 | 6x |
dplyr::group_by( |
| 299 | 6x |
other, |
| 300 | 6x |
lot_identifiant, |
| 301 | 6x |
ope_identifiant, |
| 302 | 6x |
ope_dic_identifiant, |
| 303 | 6x |
ope_date_debut, |
| 304 | 6x |
ope_date_fin, |
| 305 | 6x |
dev_code, |
| 306 | 6x |
dev_libelle |
| 307 |
) |
|
| 308 | 6x |
other <- dplyr::summarize(other) |
| 309 | 6x |
other <- as.data.frame(other) |
| 310 | 6x |
other <- |
| 311 | 6x |
fun_date_extraction(other, |
| 312 | 6x |
"ope_date_debut", |
| 313 | 6x |
jour_an = TRUE, |
| 314 | 6x |
jour_mois = FALSE) |
| 315 |
# extracting the dc from the array |
|
| 316 |
# all parms are there but some are null,i.e.val_libelle is null for quantitative parm and |
|
| 317 |
# car_valeur_quantitatif is null for for qualitative parms |
|
| 318 | 6x |
matqual <- reshape2::acast( |
| 319 | 6x |
arg[arg$ope_dic_identifiant == lesdc[i], ], |
| 320 | 6x |
lot_identifiant ~ car_par_code + car_val_identifiant, |
| 321 | 6x |
value.var = "val_libelle", |
| 322 | 6x |
drop = TRUE |
| 323 |
) |
|
| 324 | 6x |
matquant <- reshape2::acast( |
| 325 | 6x |
arg[arg$ope_dic_identifiant == lesdc[i], ], |
| 326 | 6x |
lot_identifiant ~ car_par_code + car_val_identifiant, |
| 327 | 6x |
value.var = "car_valeur_quantitatif", |
| 328 | 6x |
drop = TRUE |
| 329 |
) |
|
| 330 |
|
|
| 331 |
# this function will select the parameters one by one |
|
| 332 |
# test them for pattern against column name |
|
| 333 |
# and return the column. So a data frame of quantitative or qualitative parm are returned |
|
| 334 | 6x |
fn <- function(X, mat) {
|
| 335 | 42x |
veccol <- grepl(X, dimnames(mat)[[2]]) |
| 336 | 42x |
return(mat[, veccol]) |
| 337 |
} |
|
| 338 | 6x |
matquant2 <- sapply(X = parquant, FUN = fn, mat = matquant) |
| 339 | 6x |
colnames(matquant2) <- c("BL", "W", "Dv", "Dh", "FL")
|
| 340 |
|
|
| 341 | 6x |
matqual2 <- sapply( |
| 342 | 6x |
X = parqual, |
| 343 | 6x |
FUN = fn, |
| 344 | 6x |
mat = matqual, |
| 345 | 6x |
simplify = FALSE |
| 346 |
) |
|
| 347 |
# now matquant2 only contain the correct columns |
|
| 348 |
# matqual has two column for a single qualitative variable, which is wrong |
|
| 349 |
# we will merge them |
|
| 350 |
|
|
| 351 |
# however there is a bug if only one value is present |
|
| 352 |
# depending on the data structure there might a bug |
|
| 353 |
# when there is only one dimension (ie on instance of factor where there should be two) |
|
| 354 | 6x |
for (z in 1:length(matqual2)) {
|
| 355 | 12x |
if (is.null(dim(matqual2[[z]])[2])) |
| 356 | ! |
matqual2[[z]] <- cbind(matqual2[[z]], NA) |
| 357 |
} |
|
| 358 | 6x |
matqual3 <- matrix(NA, nrow = nrow(matqual2[[1]]), ncol = length(parqual)) |
| 359 |
# below if the data in the first column is NA we choose the second |
|
| 360 |
# which migh also be NA in which case the result becomes a NA |
|
| 361 |
|
|
| 362 | 6x |
for (j in 1:length(parqual)) {
|
| 363 | 12x |
theparqual = parqual[j] |
| 364 | 12x |
matqual3[, j] <- |
| 365 | 12x |
apply(matqual2[[theparqual]], 1, function(X) |
| 366 | 12x |
ifelse(is.na(X[1]), X[2], X[1])) |
| 367 |
} |
|
| 368 | 6x |
dd <- as.data.frame(matqual3) |
| 369 | 6x |
rownames(dd) <- rownames(matquant2) |
| 370 | 6x |
colnames(dd) <- parqual |
| 371 | 6x |
dd$stage <- as.vector(fun_stage_durif(matquant2)) |
| 372 | 6x |
dd <- cbind(dd, as.data.frame(matquant2)) |
| 373 | 6x |
dd$MD <- rowMeans(dd[, c("Dv", "Dh")], na.rm = TRUE)
|
| 374 | 6x |
dd$Pankhurst = 100 * (dd$MD / 2) ^ 2 * pi / dd$BL |
| 375 |
#K = 100 Wt /TL3 with Wt in g and TL in cm (Cone 1989). (Acou, 2009) |
|
| 376 | 6x |
dd$K_ful = 100 * dd$W / (dd$BL / 10) ^ 3 |
| 377 | 6x |
ddd <- cbind(other, dd) |
| 378 | 6x |
r_silver@calcdata[[as.character(dc)]] <- ddd |
| 379 |
} |
|
| 380 | 3x |
assign("r_silver", r_silver, envir_stacomi)
|
| 381 | 3x |
return(r_silver) |
| 382 |
} |
|
| 383 |
) |
|
| 384 | ||
| 385 | ||
| 386 |
#' Plots of various type for report_silver_eel |
|
| 387 |
#' |
|
| 388 |
#' @param x An object of class \link{report_silver_eel-class}
|
|
| 389 |
#' @param plot.type Default "1" |
|
| 390 |
#' \itemize{
|
|
| 391 |
#' \item{plot.type="1"}{Lattice plot of Durif's stages according to Body Length and Eye Index (average of vertical and horizontal diameters).
|
|
| 392 |
#' If several DC are provided then a comparison of data per dc is provided} |
|
| 393 |
#' \item{plot.type="2"}{Lattice plot giving a comparison of Durif's stage proportion over time, if several DC are provided an annual comparison
|
|
| 394 |
#' is proposed, if only one DC is provided then the migration is split into month.} |
|
| 395 |
#' \item{plot.type="3"}{ Series of graphs showing mean Fulton's coefficient, Pankhurst eye index, along
|
|
| 396 |
#' with a size weight analysis and regression using robust regression (rlm more robust to the presence of outliers)} |
|
| 397 |
#' \item{plot.type="4"}{ Lattice cloud plot of Pankurst~ Body Length ~ weight)}
|
|
| 398 |
#' } |
|
| 399 |
#' @param silent Stops displaying the messages |
|
| 400 |
#' @return A lattice xy.plot if \code{plot.type =1}, a lattice barchart if \code{plot.type=2}, nothing but plots a series of graphs in
|
|
| 401 |
#' a single plot if \code{plot.type=3}, a lattice cloud object if \code{plot.type=4}
|
|
| 402 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 403 |
#' @aliases plot.report_silver_eel |
|
| 404 |
#' @importFrom stats update |
|
| 405 |
#' @export |
|
| 406 |
setMethod( |
|
| 407 |
"plot", |
|
| 408 |
signature(x = "report_silver_eel", y = "missing"), |
|
| 409 |
definition = function(x, |
|
| 410 |
plot.type = c("1","2","3","4"),
|
|
| 411 |
silent = FALSE) {
|
|
| 412 |
#r_silver<-r_sample_char;require(ggplot2);plot.type="1" |
|
| 413 |
#browser() |
|
| 414 | 9x |
oldpar <- par(no.readonly = TRUE) |
| 415 | 9x |
on.exit(par(oldpar)) |
| 416 | 9x |
r_silver <- x |
| 417 | 9x |
plot.type <- as.character(plot.type)# to pass also characters |
| 418 | 9x |
plot.type <- match.arg(plot.type) |
| 419 | 9x |
if (exists("r_silver", envir_stacomi)) {
|
| 420 | 9x |
r_silver <- get("r_silver", envir_stacomi)
|
| 421 |
} else {
|
|
| 422 | ! |
if (!silent) |
| 423 | ! |
funout( |
| 424 | ! |
gettext("You need to launch computation first, clic on calc\n", domain =
|
| 425 | ! |
"R-stacomiR"), |
| 426 | ! |
arret = TRUE |
| 427 |
) |
|
| 428 |
} |
|
| 429 | 9x |
dat <- r_silver@calcdata |
| 430 |
# cols are using viridis::inferno(6,alpha=0.9) |
|
| 431 | 9x |
blue_for_males <- grDevices::adjustcolor("#008490", alpha.f = 0.8)
|
| 432 |
|
|
| 433 | 9x |
datdc <- data.frame() |
| 434 |
|
|
| 435 |
|
|
| 436 | 9x |
for (i in 1:length(dat)) {
|
| 437 | 18x |
datdc <- rbind(datdc, dat[[i]]) |
| 438 |
} |
|
| 439 |
|
|
| 440 |
|
|
| 441 |
|
|
| 442 |
# trellis.par.get() |
|
| 443 | 9x |
datdc$stage <- |
| 444 | 9x |
factor(datdc$stage, levels = c("I", "FII", "FIII", "FIV", "FV", "MII"))
|
| 445 | 9x |
datdc$ope_dic_identifiant <- as.factor(datdc$ope_dic_identifiant) |
| 446 | 9x |
datdc$ouv <- NA |
| 447 | 9x |
for (i in 1:length(r_silver@dc@dc_selected)) {
|
| 448 | 18x |
datdc$ouv[datdc$ope_dic_identifiant == r_silver@dc@dc_selected[i]] <- |
| 449 | 18x |
r_silver@dc@data[r_silver@dc@data$dc == r_silver@dc@dc_selected[i], "ouv_libelle"] |
| 450 |
} |
|
| 451 |
|
|
| 452 |
|
|
| 453 |
|
|
| 454 |
|
|
| 455 |
################################################# |
|
| 456 |
# plot.type =1 Eye, length category durif stages |
|
| 457 |
################################################# |
|
| 458 |
|
|
| 459 | 9x |
if (plot.type == "1") {
|
| 460 | 2x |
my.settings <- list( |
| 461 | 2x |
superpose.symbol = list( |
| 462 | 2x |
col = c( |
| 463 | 2x |
"Lime green", |
| 464 | 2x |
"#420A68E6", |
| 465 | 2x |
"#932667E6", |
| 466 | 2x |
"#DD513AE6", |
| 467 | 2x |
"#FCA50AE6", |
| 468 | 2x |
blue_for_males |
| 469 |
), |
|
| 470 | 2x |
pch = c(3, 4, 8, 15, 16, 17), |
| 471 | 2x |
cex = c(1, 1, 1, 1, 1, 1), |
| 472 | 2x |
alpha = c(0.9, 0.9, 0.9, 0.9, 0.9, 0.9) |
| 473 |
), |
|
| 474 | 2x |
superpose.line = list( |
| 475 | 2x |
col = c( |
| 476 | 2x |
"#FBA338", |
| 477 | 2x |
"#420A68E6", |
| 478 | 2x |
"#932667E6", |
| 479 | 2x |
"#DD513AE6", |
| 480 | 2x |
"#FCA50AE6", |
| 481 | 2x |
blue_for_males |
| 482 |
) |
|
| 483 |
), |
|
| 484 | 2x |
strip.background = list(col = "#932667E6"), |
| 485 | 2x |
strip.border = list(col = "black") |
| 486 |
) |
|
| 487 | 2x |
lattice::trellis.par.set(my.settings) |
| 488 |
# show.settings() |
|
| 489 | 2x |
if (length(dat) > 1) {
|
| 490 | 2x |
form <- as.formula(MD ~ BL | ouv) |
| 491 |
} else {
|
|
| 492 | ! |
form <- as.formula(MD ~ BL) |
| 493 |
} |
|
| 494 |
|
|
| 495 | 2x |
xy.plot <- lattice::xyplot( |
| 496 | 2x |
form, |
| 497 | 2x |
data = datdc, |
| 498 | 2x |
group = stage, |
| 499 | 2x |
type = c("p"),
|
| 500 | 2x |
par.settings = my.settings, |
| 501 | 2x |
xlab = gettext("size (BL mm)", domain = "R-stacomiR"),
|
| 502 | 2x |
ylab = gettext("Mean eye diameter (MD mm)", domain = "R-stacomiR"),
|
| 503 | 2x |
par.strip.text = list(col = "white", font = 2), |
| 504 | 2x |
auto.key = list( |
| 505 | 2x |
title = gettext("Silvering stages (Durif et al. 2009)", domain = "R-stacomiR"),
|
| 506 | 2x |
cex.title = 1.2, |
| 507 | 2x |
space = "top", |
| 508 | 2x |
columns = 6, |
| 509 | 2x |
between.columns = 1 |
| 510 |
) |
|
| 511 |
) |
|
| 512 |
# draw lines in lattice |
|
| 513 | 2x |
xy.plot <- update( |
| 514 | 2x |
xy.plot, |
| 515 | 2x |
panel = function(...) {
|
| 516 | 2x |
lattice::panel.abline( |
| 517 | 2x |
h = c(6.5, 8), |
| 518 | 2x |
v = c(300, 450, 500) , |
| 519 | 2x |
lty = "dotted", |
| 520 | 2x |
col = "light grey" |
| 521 |
) |
|
| 522 | 2x |
lattice::panel.xyplot(...) |
| 523 |
} |
|
| 524 |
) |
|
| 525 |
|
|
| 526 | 2x |
return(xy.plot) |
| 527 |
|
|
| 528 |
} |
|
| 529 |
###################################### |
|
| 530 |
# Migration according to stage, month and year |
|
| 531 |
# !! throws a warning calling par(new=TRUE) with no plot, no dev.new() |
|
| 532 |
###################################### |
|
| 533 | 7x |
if (plot.type == "2") {
|
| 534 | 2x |
datdc1 <- dplyr::select(datdc, ouv, annee, mois, stage) |
| 535 | 2x |
datdc1 <- dplyr::group_by(datdc1, ouv, annee, mois, stage) |
| 536 | 2x |
datdc1 <- dplyr::summarize(datdc1, N = dplyr::n()) |
| 537 | 2x |
datdc1 <- as.data.frame(datdc1) |
| 538 |
# show.settings() |
|
| 539 | 2x |
my.settings <- list( |
| 540 | 2x |
superpose.polygon = list( |
| 541 | 2x |
col = c( |
| 542 | 2x |
"Lime green", |
| 543 | 2x |
"#420A68E6", |
| 544 | 2x |
"#932667E6", |
| 545 | 2x |
"#DD513AE6", |
| 546 | 2x |
"#FCA50AE6", |
| 547 | 2x |
blue_for_males |
| 548 |
), |
|
| 549 | 2x |
alpha = c(0.9, 0.9, 0.9, 0.9, 0.9, 0.9) |
| 550 |
), |
|
| 551 | 2x |
superpose.line = list( |
| 552 | 2x |
col = c( |
| 553 | 2x |
"#FBA338", |
| 554 | 2x |
"#420A68E6", |
| 555 | 2x |
"#932667E6", |
| 556 | 2x |
"#DD513AE6", |
| 557 | 2x |
"#FCA50AE6", |
| 558 | 2x |
blue_for_males |
| 559 |
) |
|
| 560 |
), |
|
| 561 |
#colfn<-colorRampPalette(c("#1C4587", "#BBC7DB"),space = "Lab")
|
|
| 562 |
#colfn(7) |
|
| 563 | 2x |
strip.background = list( |
| 564 | 2x |
col = c( |
| 565 | 2x |
"#1B4586", |
| 566 | 2x |
"#3E5894", |
| 567 | 2x |
"#596DA2", |
| 568 | 2x |
"#7282B0", |
| 569 | 2x |
"#8A98BE", |
| 570 | 2x |
"#A2AFCC", |
| 571 | 2x |
"#BAC6DA" |
| 572 |
) |
|
| 573 |
), |
|
| 574 | 2x |
strip.border = list(col = "black") |
| 575 |
) |
|
| 576 | 2x |
lattice::trellis.par.set(my.settings) |
| 577 |
|
|
| 578 |
# show.settings() |
|
| 579 | 2x |
if (length(dat) > 1) {
|
| 580 | 2x |
form <- as.formula(N ~ annee | ouv) |
| 581 |
} else {
|
|
| 582 | ! |
form <- as.formula(N ~ mois | annee) |
| 583 |
} |
|
| 584 |
|
|
| 585 | 2x |
bb <- lattice::barchart( |
| 586 | 2x |
form, |
| 587 | 2x |
data = datdc1, |
| 588 | 2x |
group = stage, |
| 589 | 2x |
xlab = gettext("Month", domain = "R-stacomiR"),
|
| 590 | 2x |
ylab = gettext("Number", domain = "R-stacomiR"),
|
| 591 | 2x |
par.strip.text = list(col = "white", font = 2), |
| 592 | 2x |
auto.key = list( |
| 593 | 2x |
title = gettext("Number by silvering stage", domain = "R-stacomiR"),
|
| 594 | 2x |
cex.title = 1.2, |
| 595 | 2x |
space = "top", |
| 596 | 2x |
columns = 6, |
| 597 | 2x |
between.columns = 0.5 |
| 598 |
) |
|
| 599 |
) |
|
| 600 | 2x |
return(bb) |
| 601 |
|
|
| 602 |
} |
|
| 603 |
###################################### |
|
| 604 |
# Series of graphs showing proportion of stage, mean Fulton's coefficient, Pankhurst eye index, |
|
| 605 |
# body weight, body size, sex ratio. |
|
| 606 |
###################################### |
|
| 607 | 5x |
if (plot.type == "3") {
|
| 608 | 3x |
layout( |
| 609 | 3x |
matrix(c(1, 2, 3, 4, 4, 5, 6, 6, 7), 3, 3, byrow = TRUE), |
| 610 | 3x |
widths = c(3, 3, 1), |
| 611 | 3x |
heights = c(3, 1, 3) |
| 612 |
) |
|
| 613 |
# width 331 sets the last column relative width |
|
| 614 |
# same for rows |
|
| 615 | 3x |
par(mar = c(3, 4.1, 4.1, 2.1))# ressetting to default |
| 616 | 3x |
datdc <- chnames(datdc, "ope_dic_identifiant", "dc") |
| 617 | 3x |
lesdc <- unique(datdc$dc) |
| 618 | 3x |
datdc$sex <- "F" |
| 619 | 3x |
datdc$sex[datdc$BL < 450] <- "M" |
| 620 |
|
|
| 621 |
############# |
|
| 622 |
# Fulton |
|
| 623 |
############# |
|
| 624 | 3x |
moy <- tapply(datdc$K_ful, list(datdc$dc, datdc$sex), mean, na.rm = TRUE) |
| 625 | 3x |
sd <- |
| 626 | 3x |
tapply(datdc$K_ful, list(datdc$dc, datdc$sex), sd, na.rm = TRUE) # sample standard deviation |
| 627 | 3x |
n <- tapply(datdc$K_ful, list(datdc$dc, datdc$sex), length) |
| 628 | 3x |
SE = sd / sqrt(n) |
| 629 | 3x |
plotTop = max(moy + 3 * SE, na.rm = TRUE) |
| 630 |
|
|
| 631 |
|
|
| 632 | 3x |
bp <- barplot( |
| 633 | 3x |
moy, |
| 634 | 3x |
beside = TRUE, |
| 635 | 3x |
las = 1, |
| 636 | 3x |
ylim = c(0, plotTop), |
| 637 | 3x |
cex.names = 0.75, |
| 638 | 3x |
main = "Fulton coefficient (+-2SE)", |
| 639 | 3x |
ylab = "Fulton K", |
| 640 | 3x |
xlab = "", |
| 641 | 3x |
border = "black", |
| 642 | 3x |
axes = TRUE, |
| 643 |
#legend.text = TRUE, |
|
| 644 |
#args.legend = list(title = "DC", |
|
| 645 |
# x = "topright", |
|
| 646 |
# cex = .7) |
|
| 647 |
) |
|
| 648 | 3x |
graphics::segments(bp, moy - SE * 2, bp, |
| 649 | 3x |
moy + SE * 2, lwd = 2) |
| 650 |
|
|
| 651 | 3x |
graphics::arrows( |
| 652 | 3x |
bp, |
| 653 | 3x |
moy - SE * 2, |
| 654 | 3x |
bp, |
| 655 | 3x |
moy + SE * 2, |
| 656 | 3x |
lwd = 2, |
| 657 | 3x |
angle = 90, |
| 658 | 3x |
code = 3, |
| 659 | 3x |
length = 0.05 |
| 660 |
) |
|
| 661 |
|
|
| 662 |
|
|
| 663 |
############# |
|
| 664 |
# Pankhurst |
|
| 665 |
############# |
|
| 666 | 3x |
moy <- |
| 667 | 3x |
tapply(datdc$Pankhurst, list(datdc$dc, datdc$sex), mean, na.rm = TRUE) |
| 668 | 3x |
sd <- |
| 669 | 3x |
tapply(datdc$Pankhurst, list(datdc$dc, datdc$sex), sd, na.rm = TRUE) # sample standard deviation |
| 670 | 3x |
n <- tapply(datdc$Pankhurst, list(datdc$dc, datdc$sex), length) |
| 671 | 3x |
SE = sd / sqrt(n) |
| 672 | 3x |
plotTop = max(moy + 3 * SE, na.rm = TRUE) |
| 673 |
|
|
| 674 |
|
|
| 675 | 3x |
bp <- barplot( |
| 676 | 3x |
moy, |
| 677 | 3x |
beside = TRUE, |
| 678 | 3x |
las = 1, |
| 679 | 3x |
ylim = c(0, plotTop), |
| 680 | 3x |
cex.names = 0.75, |
| 681 | 3x |
main = "Pankhurst (+-2SE)", |
| 682 | 3x |
ylab = "Pankhurst eye index", |
| 683 | 3x |
xlab = "", |
| 684 | 3x |
border = "black", |
| 685 | 3x |
axes = TRUE, |
| 686 |
#legend.text = TRUE, |
|
| 687 |
#args.legend = list(title = "DC", |
|
| 688 |
# x = "topright", |
|
| 689 |
# cex = .7) |
|
| 690 |
) |
|
| 691 | 3x |
segments(bp, moy - SE * 2, bp, |
| 692 | 3x |
moy + SE * 2, lwd = 2) |
| 693 |
|
|
| 694 | 3x |
arrows( |
| 695 | 3x |
bp, |
| 696 | 3x |
moy - SE * 2, |
| 697 | 3x |
bp, |
| 698 | 3x |
moy + SE * 2, |
| 699 | 3x |
lwd = 2, |
| 700 | 3x |
angle = 90, |
| 701 | 3x |
code = 3, |
| 702 | 3x |
length = 0.05 |
| 703 |
) |
|
| 704 |
|
|
| 705 |
############# |
|
| 706 |
# empty plot |
|
| 707 |
############# |
|
| 708 | 3x |
op <- par(mar = c(1, 1, 1, 1)) |
| 709 | 3x |
plot( |
| 710 | 3x |
1, |
| 711 | 3x |
type = "n", |
| 712 | 3x |
axes = F, |
| 713 | 3x |
xlab = "", |
| 714 | 3x |
ylab = "" |
| 715 |
) |
|
| 716 | 3x |
legend("center",
|
| 717 | 3x |
fill = grDevices::grey.colors(nrow(moy)), |
| 718 | 3x |
legend = unique(datdc$dc)) |
| 719 |
# grey.colors is the default color generation for barplot |
|
| 720 |
############# |
|
| 721 |
# size hist |
|
| 722 |
############# |
|
| 723 | 3x |
par(mar = c(1, 4.1, 1, 1)) |
| 724 | 3x |
for (i in 1:length(lesdc)) {
|
| 725 | 6x |
indexdc <- datdc$dc == lesdc[i] |
| 726 | 6x |
histxn <- |
| 727 | 6x |
graphics::hist(datdc$BL[indexdc], |
| 728 | 6x |
breaks = seq(250, 1100, by = 50), |
| 729 | 6x |
plot = FALSE)$density |
| 730 | 6x |
if (i == 1) |
| 731 | 3x |
histx <- histxn |
| 732 |
else |
|
| 733 | 3x |
histx <- cbind(histx, histxn) |
| 734 |
|
|
| 735 |
} |
|
| 736 | 3x |
if (length(lesdc) > 1) |
| 737 | 3x |
colnames(histx) <- lesdc |
| 738 | 3x |
barplot( |
| 739 | 3x |
height = t(histx), |
| 740 | 3x |
space = 0, |
| 741 | 3x |
beside = FALSE, |
| 742 | 3x |
las = 1, |
| 743 | 3x |
horiz = FALSE, |
| 744 | 3x |
legend.text = FALSE, |
| 745 | 3x |
axes = FALSE |
| 746 |
) |
|
| 747 |
############# |
|
| 748 |
# empty plot |
|
| 749 |
############# |
|
| 750 | 3x |
op <- par(mar = c(1, 1, 1, 1)) |
| 751 | 3x |
plot( |
| 752 | 3x |
1, |
| 753 | 3x |
type = "n", |
| 754 | 3x |
axes = F, |
| 755 | 3x |
xlab = "", |
| 756 | 3x |
ylab = "" |
| 757 |
) |
|
| 758 |
|
|
| 759 |
############# |
|
| 760 |
# size -weight |
|
| 761 |
############# |
|
| 762 | 3x |
par(mar = c(5.1, 4.1, 1, 1)) # blur bottom left up right |
| 763 | 3x |
plot( |
| 764 | 3x |
datdc$BL, |
| 765 | 3x |
datdc$W, |
| 766 | 3x |
type = "n", |
| 767 | 3x |
xlab = gettext("Size (mm)", domain = "R-stacomiR"),
|
| 768 | 3x |
ylab = gettext("Weight(g)", domain = "R-stacomiR"),
|
| 769 | 3x |
xlim = c(250, 1000), |
| 770 | 3x |
ylim = c(0, 2000) |
| 771 |
) |
|
| 772 | 3x |
abline(v = seq(250, 1000, by = 50), |
| 773 | 3x |
col = "lightgray", |
| 774 | 3x |
lty = 2) |
| 775 | 3x |
abline(h = seq(0, 2000, by = 100), |
| 776 | 3x |
col = "lightgray", |
| 777 | 3x |
lty = 2) |
| 778 |
# some alpha blending to better see the points : |
|
| 779 | 3x |
lescol <- ggplot2::alpha(grDevices::grey.colors(nrow(moy)), 0.8) |
| 780 | 3x |
for (i in 1:length(lesdc)) {
|
| 781 | 6x |
indexdc <- datdc$dc == lesdc[i] |
| 782 | 6x |
points( |
| 783 | 6x |
datdc$BL[indexdc], |
| 784 | 6x |
datdc$W[indexdc], |
| 785 | 6x |
pch = 16, |
| 786 | 6x |
col = lescol[i], |
| 787 | 6x |
cex = 0.8 |
| 788 |
) |
|
| 789 |
|
|
| 790 |
} |
|
| 791 |
######################" |
|
| 792 |
# Size - weight model using robust regression |
|
| 793 |
###################### |
|
| 794 | 3x |
subdatdc <- datdc[, c("BL", "W")]
|
| 795 | 3x |
subdatdc$BL3 <- (subdatdc$BL / 1000) ^ 3 |
| 796 |
# plot(subdatdc$W~subdatdc$BL3) |
|
| 797 |
|
|
| 798 | 3x |
rlmmodb <- MASS::rlm(W ~ 0 + BL3, data = subdatdc) |
| 799 |
#summary(rlmmodb) |
|
| 800 | 3x |
newdata <- |
| 801 | 3x |
data.frame("BL" = seq(250, 1000, by = 50),
|
| 802 | 3x |
"BL3" = (seq(250, 1000, by = 50) / 1000) ^ 3) |
| 803 | 3x |
pred <- |
| 804 | 3x |
predict( |
| 805 | 3x |
rlmmodb, |
| 806 | 3x |
newdata = newdata, |
| 807 | 3x |
se.fit = TRUE, |
| 808 | 3x |
type = "response", |
| 809 | 3x |
interval = "prediction" |
| 810 |
) |
|
| 811 | 3x |
newdata$predlm <- pred$fit[, 1] |
| 812 | 3x |
newdata$predlowIC <- pred$fit[, 2] |
| 813 | 3x |
newdata$predhighIC <- pred$fit[, 3] |
| 814 |
|
|
| 815 | 3x |
points(newdata$BL, newdata$predlm, type = "l") |
| 816 | 3x |
points( |
| 817 | 3x |
newdata$BL, |
| 818 | 3x |
newdata$predlowIC, |
| 819 | 3x |
type = "l", |
| 820 | 3x |
lty = 2, |
| 821 | 3x |
col = "grey50" |
| 822 |
) |
|
| 823 | 3x |
points( |
| 824 | 3x |
newdata$BL, |
| 825 | 3x |
newdata$predhighIC, |
| 826 | 3x |
type = "l", |
| 827 | 3x |
lty = 2, |
| 828 | 3x |
col = "grey50" |
| 829 |
) |
|
| 830 |
|
|
| 831 | 3x |
text(400, 1500, stringr::str_c("W=", round(coefficients(rlmmodb), 1), " BL^3"))
|
| 832 |
|
|
| 833 |
############# |
|
| 834 |
# weight hist rotate |
|
| 835 |
############# |
|
| 836 | 3x |
par(mar = c(5.1, 1, 1, 1)) |
| 837 | 3x |
for (i in 1:length(lesdc)) {
|
| 838 | 6x |
indexdc <- datdc$dc == lesdc[i] |
| 839 | 6x |
histyn <- |
| 840 | 6x |
hist(datdc$W[indexdc], |
| 841 | 6x |
plot = FALSE, |
| 842 | 6x |
breaks = seq(0, 2500, by = 100))$density |
| 843 | 6x |
if (i == 1) |
| 844 | 3x |
histy <- histyn |
| 845 |
else |
|
| 846 | 3x |
histy <- cbind(histy, histyn) |
| 847 |
|
|
| 848 |
} |
|
| 849 | 3x |
if (length(lesdc) > 1) |
| 850 | 3x |
colnames(histy) <- lesdc |
| 851 | 3x |
barplot( |
| 852 | 3x |
height = t(histy), |
| 853 | 3x |
space = 0, |
| 854 | 3x |
beside = FALSE, |
| 855 | 3x |
las = 1, |
| 856 | 3x |
horiz = TRUE, |
| 857 | 3x |
legend.text = FALSE, |
| 858 | 3x |
axes = FALSE |
| 859 |
) |
|
| 860 |
|
|
| 861 |
} |
|
| 862 | 5x |
if (plot.type == "4") {
|
| 863 |
#creating a shingle with some overlaps |
|
| 864 | 2x |
my.settings <- list( |
| 865 | 2x |
superpose.polygon = list( |
| 866 | 2x |
col = c( |
| 867 | 2x |
"Lime green", |
| 868 | 2x |
"#420A68E6", |
| 869 | 2x |
"#932667E6", |
| 870 | 2x |
"#DD513AE6", |
| 871 | 2x |
"#FCA50AE6", |
| 872 | 2x |
blue_for_males |
| 873 |
), |
|
| 874 | 2x |
alpha = c(0.9, 0.9, 0.9, 0.9, 0.9, 0.9) |
| 875 |
), |
|
| 876 | 2x |
superpose.line = list( |
| 877 | 2x |
col = c( |
| 878 | 2x |
"#FBA338", |
| 879 | 2x |
"#420A68E6", |
| 880 | 2x |
"#932667E6", |
| 881 | 2x |
"#DD513AE6", |
| 882 | 2x |
"#FCA50AE6", |
| 883 | 2x |
blue_for_males |
| 884 |
) |
|
| 885 |
), |
|
| 886 |
#colfn<-colorRampPalette(c("#1C4587", "#BBC7DB"),space = "Lab")
|
|
| 887 |
#colfn(7) |
|
| 888 | 2x |
strip.background = list( |
| 889 | 2x |
col = c( |
| 890 | 2x |
"#1B4586", |
| 891 | 2x |
"#3E5894", |
| 892 | 2x |
"#596DA2", |
| 893 | 2x |
"#7282B0", |
| 894 | 2x |
"#8A98BE", |
| 895 | 2x |
"#A2AFCC", |
| 896 | 2x |
"#BAC6DA" |
| 897 |
) |
|
| 898 |
), |
|
| 899 | 2x |
strip.border = list(col = "black") |
| 900 |
) |
|
| 901 | 2x |
lattice::trellis.par.set(my.settings) |
| 902 | 2x |
datdc <- |
| 903 | 2x |
datdc[complete.cases(datdc[, c("Pankhurst", "W", "BL", "ouv", "stage")]), ]
|
| 904 | 2x |
ccc <- |
| 905 | 2x |
lattice::cloud( |
| 906 | 2x |
Pankhurst ~ W * BL | ouv, |
| 907 | 2x |
data = datdc, |
| 908 | 2x |
group = stage, |
| 909 | 2x |
screen = list(x = -90, y = 70), |
| 910 | 2x |
distance = .4, |
| 911 | 2x |
zoom = .6, |
| 912 | 2x |
strip = lattice::strip.custom(par.strip.text = list(col = "white")) |
| 913 |
) |
|
| 914 | 2x |
return(ccc) |
| 915 |
} |
|
| 916 |
|
|
| 917 |
|
|
| 918 |
} |
|
| 919 |
) |
|
| 920 | ||
| 921 |
#' summary for report_silver_eel |
|
| 922 |
#' @param object An object of class \code{\link{report_silver_eel-class}}
|
|
| 923 |
#' @param silent Should the program stay silent or display messages, default FALSE |
|
| 924 |
#' @param ... Additional parameters |
|
| 925 |
#' @return A list per DC with statistic for Durif stages, Pankhurst, MD Eye diameter, BL body length and weight W |
|
| 926 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 927 |
#' @aliases summary.report_silver_eel |
|
| 928 |
#' @export |
|
| 929 |
setMethod( |
|
| 930 |
"summary", |
|
| 931 |
signature = signature(object = "report_silver_eel"), |
|
| 932 |
definition = function(object, silent = FALSE, ...) {
|
|
| 933 | 2x |
r_silver <- object |
| 934 | 2x |
if (exists("r_silver", envir_stacomi)) {
|
| 935 | 2x |
r_silver <- get("r_silver", envir_stacomi)
|
| 936 |
} else {
|
|
| 937 | ! |
if (!silent) |
| 938 | ! |
funout( |
| 939 | ! |
gettext("You need to launch computation first, clic on calc\n", domain =
|
| 940 | ! |
"R-stacomiR"), |
| 941 | ! |
arret = TRUE |
| 942 |
) |
|
| 943 |
} |
|
| 944 | 2x |
dat <- r_silver@calcdata |
| 945 |
# cols are using viridis::inferno(6,alpha=0.9) |
|
| 946 |
|
|
| 947 | 2x |
printstat <- function(vec, silent) {
|
| 948 | 16x |
moy <- mean(vec, na.rm = TRUE) |
| 949 | 16x |
sd <- sd(vec, na.rm = TRUE) # sample standard deviation |
| 950 | 16x |
n <- length(vec[!is.na(vec)]) |
| 951 | 16x |
SE = sd / sqrt(n) |
| 952 | 16x |
if (!silent) |
| 953 | 8x |
print(noquote( |
| 954 | 8x |
stringr::str_c( |
| 955 | 8x |
"mean=", |
| 956 | 8x |
round(moy, 2), |
| 957 | 8x |
",SD=", |
| 958 | 8x |
round(sd, 2), |
| 959 | 8x |
",N=", |
| 960 | 8x |
n, |
| 961 | 8x |
",SE=", |
| 962 | 8x |
round(SE, 2) |
| 963 |
) |
|
| 964 |
)) |
|
| 965 | 16x |
return(list( |
| 966 | 16x |
"mean" = moy, |
| 967 | 16x |
"SD" = sd, |
| 968 | 16x |
"N" = n, |
| 969 | 16x |
"SE" = SE |
| 970 |
)) |
|
| 971 |
} |
|
| 972 | 2x |
result <- list() |
| 973 | 2x |
for (i in 1:length(dat)) {
|
| 974 | 4x |
datdc <- dat[[i]] |
| 975 | 4x |
ouvrage <- |
| 976 | 4x |
r_silver@dc@data[r_silver@dc@data$dc == r_silver@dc@dc_selected[i], "ouv_libelle"] |
| 977 | 4x |
dc <- as.character(unique(datdc$ope_dic_identifiant)) |
| 978 | 4x |
result[[dc]] <- list() |
| 979 | 4x |
result[[dc]][["ouvrage"]] <- ouvrage |
| 980 | 4x |
if (!silent) {
|
| 981 | 2x |
print(noquote(stringr::str_c("Statistics for dam : ", ouvrage)))
|
| 982 | 2x |
print(noquote("========================"))
|
| 983 | 2x |
print(noquote("Stages Durif"))
|
| 984 | 2x |
print(table(datdc$stage)) |
| 985 |
} |
|
| 986 | 4x |
result[[dc]][["Stages"]] <- table(datdc$stage) |
| 987 | 4x |
if (!silent) {
|
| 988 | 2x |
print(noquote("-----------------------"))
|
| 989 | 2x |
print(noquote("Pankhurst"))
|
| 990 | 2x |
print(noquote("-----------------------"))
|
| 991 |
} |
|
| 992 | 4x |
result[[dc]][["Pankhurst"]] <- |
| 993 | 4x |
printstat(datdc$Pankhurst, silent = silent) |
| 994 | 4x |
if (!silent) {
|
| 995 | 2x |
print(noquote("-----------------------"))
|
| 996 | 2x |
print(noquote('Eye diameter (mm)'))
|
| 997 | 2x |
print(noquote("-----------------------"))
|
| 998 |
} |
|
| 999 | 4x |
result[[dc]][["MD"]] <- printstat(datdc$MD, silent = silent) |
| 1000 | 4x |
if (!silent) {
|
| 1001 | 2x |
print(noquote("-----------------------"))
|
| 1002 | 2x |
print(noquote('Length (mm)'))
|
| 1003 | 2x |
print(noquote("-----------------------"))
|
| 1004 |
} |
|
| 1005 | 4x |
result[[dc]][["BL"]] <- printstat(datdc$BL, silent = silent) |
| 1006 | 4x |
if (!silent) {
|
| 1007 | 2x |
print(noquote("-----------------------"))
|
| 1008 | 2x |
print(noquote('Weight (g)'))
|
| 1009 | 2x |
print(noquote("-----------------------"))
|
| 1010 |
} |
|
| 1011 | 4x |
result[[dc]][["W"]] <- printstat(datdc$W, silent = silent) |
| 1012 |
} |
|
| 1013 | 2x |
return(result) |
| 1014 |
} |
|
| 1015 |
) |
|
| 1016 | ||
| 1017 |
#' Method to print the command line of the object |
|
| 1018 |
#' @param x An object of class report_silver_eel |
|
| 1019 |
#' @param ... Additional parameters passed to print |
|
| 1020 |
#' @return NULL, prints data in the console |
|
| 1021 |
#' @author cedric.briand |
|
| 1022 |
#' @aliases print.report_silver_eel |
|
| 1023 |
#' @export |
|
| 1024 |
setMethod( |
|
| 1025 |
"print", |
|
| 1026 |
signature = signature("report_silver_eel"),
|
|
| 1027 |
definition = function(x, ...) {
|
|
| 1028 | ! |
sortie1 <- "r_silver=new('report_silver_eel')"
|
| 1029 | ! |
sortie2 <- stringr::str_c( |
| 1030 | ! |
"r_silver=choice_c(r_silver,", |
| 1031 | ! |
"dc=c(",
|
| 1032 | ! |
stringr::str_c(x@dc@dc_selected, collapse = ","), |
| 1033 |
"),", |
|
| 1034 | ! |
"taxa=c(",
|
| 1035 | ! |
stringr::str_c(shQuote(x@taxa@data$tax_nom_latin), collapse = ","), |
| 1036 |
"),", |
|
| 1037 | ! |
"stage=c(",
|
| 1038 | ! |
stringr::str_c(shQuote(x@stage@stage_selected), collapse = ","), |
| 1039 |
"),", |
|
| 1040 | ! |
"par=c(",
|
| 1041 | ! |
stringr::str_c(shQuote(x@par@par_selected), collapse = ","), |
| 1042 |
"),", |
|
| 1043 | ! |
"horodatedebut=", |
| 1044 | ! |
shQuote( |
| 1045 | ! |
strftime(x@horodatedebut@horodate, format = "%d/%m/%Y %H-%M-%S") |
| 1046 |
), |
|
| 1047 | ! |
",horodatefin=", |
| 1048 | ! |
shQuote( |
| 1049 | ! |
strftime(x@horodatefin@horodate, format = "%d/%m/%Y %H-%M-%S") |
| 1050 |
), |
|
| 1051 |
")" |
|
| 1052 |
) |
|
| 1053 |
# removing backslashes |
|
| 1054 | ! |
funout(sortie1) |
| 1055 | ! |
funout(stringr::str_c(sortie2, ...)) |
| 1056 | ! |
return(invisible(NULL)) |
| 1057 |
} |
|
| 1058 |
) |
|
| 1059 | ||
| 1060 | ||
| 1061 |
#' funplotreport_silver_eel |
|
| 1062 |
#' |
|
| 1063 |
#' assigns an object g in envir_stacomi for eventual modification of the plot |
|
| 1064 |
#' @param action, action 1,2,3 or 4 corresponding to plot |
|
| 1065 |
#' @param ... Additional parameters |
|
| 1066 |
#' @return Nothing |
|
| 1067 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 1068 |
#' @keywords internal |
|
| 1069 |
funplotreport_silver_eel = function(action, ...) {
|
|
| 1070 | ! |
r_silver <- get(x = "report_arg", envir = envir_stacomi) |
| 1071 | ! |
r_silver <- charge(r_silver) |
| 1072 | ! |
r_silver <- connect(r_silver) |
| 1073 | ! |
r_silver <- calcule(r_silver) |
| 1074 |
#plot.type is determined by button in h$action |
|
| 1075 | ! |
the_plot <- plot(r_silver, plot.type = action) |
| 1076 | ! |
print(the_plot) |
| 1077 |
} |
|
| 1078 | ||
| 1079 | ||
| 1080 | ||
| 1081 |
#' Function to calculate the stages from Durif |
|
| 1082 |
#' |
|
| 1083 |
#' @param data A dataset with columns BL, W, Dv, Dh, FL corresponding to body length (mm), |
|
| 1084 |
#' Weight (g), vertical eye diameter (mm), vertical eye diameter (mm), and pectoral fin length (mm) |
|
| 1085 |
#' @returns A data.frame with durif stages per individual |
|
| 1086 |
#' @author Laurent Beaulaton \email{laurent.beaulaton@onema.fr}
|
|
| 1087 |
#' @export |
|
| 1088 |
fun_stage_durif = function(data) {
|
|
| 1089 |
# see section Good Practise in ? data |
|
| 1090 | 7x |
data(coef_durif, envir = environment()) |
| 1091 | 7x |
stopifnot(colnames(data) == c("BL", "W", "Dv", "Dh", "FL"))
|
| 1092 | 7x |
data <- |
| 1093 | 7x |
cbind(1, data[, c(1, 2, 5)], rowMeans(data[, c("Dv", "Dh")], na.rm = TRUE))
|
| 1094 | 7x |
colnames(data) <- c("Constant", "BL", "W", "FL", "MD")
|
| 1095 | 7x |
data <- data[, c(1, 2, 3, 5, 4)] |
| 1096 | 7x |
indices <- data %*% coef_durif |
| 1097 | 7x |
return(unlist(apply(indices, 1, function(X) |
| 1098 | 7x |
ifelse(is.na(X[1]), NA, names(which.max(X)))))) |
| 1099 |
} |
| 1 |
#' Creates a list of available schemas in the db |
|
| 2 |
#' |
|
| 3 |
#' @return A table with of data providers with org_code, the user of each schema, and org_description the description of the schema |
|
| 4 |
#' @export |
|
| 5 |
fun_schema <- function(){
|
|
| 6 | ! |
req = new("RequeteDB")
|
| 7 |
# this query will get characteristics from lot_pere when null |
|
| 8 | ! |
req@sql = "SELECT * FROM ref.ts_organisme_org too WHERE NOT org_code IN ('nat','invite') ORDER BY org_code"
|
| 9 | ! |
schema_table <- query(req)@query |
| 10 | ! |
return(schema_table) |
| 11 |
} |
|
| 12 |
|
|
| 13 | ||
| 14 |
| 1 |
#' Class "report_annual" |
|
| 2 |
#' |
|
| 3 |
#' This class displays annual migration counts, for several counting device, taxa or stages. |
|
| 4 |
#' @include ref_dc.R |
|
| 5 |
#' @include ref_taxa.R |
|
| 6 |
#' @include ref_stage.R |
|
| 7 |
#' @include ref_year.R |
|
| 8 |
#' @slot dc Object of class \code{\link{ref_dc-class}}, the counting device, multiple values allowed
|
|
| 9 |
#' @slot data Object of class \code{"data.frame"} data for report lot
|
|
| 10 |
#' @slot taxa An object of class \code{\link{ref_taxa-class}}, multiple values allowed
|
|
| 11 |
#' @slot stage An object of class \code{\link{ref_stage-class}}, multiple values allowed
|
|
| 12 |
#' @slot start_year Object of class \code{\link{ref_year-class}}. ref_year allows to choose year of beginning
|
|
| 13 |
#' @slot end_year Object of class \code{\link{ref_year-class}}
|
|
| 14 |
#' ref_year allows to choose last year of the report |
|
| 15 |
#' @aliases report_annual |
|
| 16 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 17 |
#' @family report Objects |
|
| 18 |
#' @keywords classes |
|
| 19 |
#' @example inst/examples/report_annual-example.R |
|
| 20 |
#' @export |
|
| 21 |
setClass( |
|
| 22 |
Class = "report_annual", |
|
| 23 |
representation = |
|
| 24 |
representation( |
|
| 25 |
dc = "ref_dc", |
|
| 26 |
taxa = "ref_taxa", |
|
| 27 |
stage = "ref_stage", |
|
| 28 |
data = "data.frame", |
|
| 29 |
start_year = "ref_year", |
|
| 30 |
end_year = "ref_year" |
|
| 31 |
), |
|
| 32 |
prototype = prototype( |
|
| 33 |
dc = new("ref_dc"),
|
|
| 34 |
taxa = new("ref_taxa"),
|
|
| 35 |
stage = new("ref_stage"),
|
|
| 36 |
data = data.frame(), |
|
| 37 |
start_year = new("ref_year"),
|
|
| 38 |
end_year = new("ref_year")
|
|
| 39 |
) |
|
| 40 |
) |
|
| 41 | ||
| 42 | ||
| 43 |
#' charge method for report_annual class |
|
| 44 |
#' |
|
| 45 |
#' Method used by the graphical interface to load data and check that all choices have |
|
| 46 |
#' been made by the user |
|
| 47 |
#' @param object An object of class \link{report_annual-class}
|
|
| 48 |
#' @param silent Stops messages from being displayed if silent=TRUE, default FALSE |
|
| 49 |
#' @aliases charge.report_annual |
|
| 50 |
#' @return object An object of class \link{report_annual-class} with data set from values assigned in \code{envir_stacomi} environment
|
|
| 51 |
#' @keywords internal |
|
| 52 |
setMethod( |
|
| 53 |
"charge", |
|
| 54 |
signature = signature("report_annual"),
|
|
| 55 |
definition = function(object, silent = FALSE) {
|
|
| 56 | ! |
r_ann <- object |
| 57 | ! |
if (exists("ref_dc", envir_stacomi)) {
|
| 58 | ! |
r_ann@dc <- get("ref_dc", envir_stacomi)
|
| 59 |
} else {
|
|
| 60 | ! |
funout( |
| 61 | ! |
gettext( |
| 62 | ! |
"You need to choose a counting device, clic on validate\n", |
| 63 | ! |
domain = "R-stacomiR" |
| 64 |
), |
|
| 65 | ! |
arret = TRUE |
| 66 |
) |
|
| 67 |
} |
|
| 68 | ! |
if (exists("ref_taxa", envir_stacomi)) {
|
| 69 | ! |
r_ann@taxa <- get("ref_taxa", envir_stacomi)
|
| 70 |
} else {
|
|
| 71 | ! |
funout( |
| 72 | ! |
gettext("You need to choose a taxa, clic on validate\n", domain = "R-stacomiR"),
|
| 73 | ! |
arret = TRUE |
| 74 |
) |
|
| 75 |
} |
|
| 76 | ! |
if (exists("ref_stage", envir_stacomi)) {
|
| 77 | ! |
r_ann@stage <- get("ref_stage", envir_stacomi)
|
| 78 |
} else |
|
| 79 |
{
|
|
| 80 | ! |
funout( |
| 81 | ! |
gettext("You need to choose a stage, clic on validate\n", domain = "R-stacomiR"),
|
| 82 | ! |
arret = TRUE |
| 83 |
) |
|
| 84 |
} |
|
| 85 | ! |
if (exists("start_year", envir_stacomi)) {
|
| 86 | ! |
r_ann@start_year <- get("start_year", envir_stacomi)
|
| 87 |
} else {
|
|
| 88 | ! |
funout(gettext("You need to choose the starting year\n", domain = "R-stacomiR"),
|
| 89 | ! |
arret = TRUE) |
| 90 |
} |
|
| 91 | ! |
if (exists("end_year", envir_stacomi)) {
|
| 92 | ! |
r_ann@end_year <- get("end_year", envir_stacomi)
|
| 93 |
} else {
|
|
| 94 | ! |
funout(gettext("You need to choose the ending year\n", domain = "R-stacomiR"),
|
| 95 | ! |
arret = TRUE) |
| 96 |
} |
|
| 97 | ! |
assign("report_annual", r_ann, envir_stacomi)
|
| 98 | ! |
funout( |
| 99 | ! |
gettext( |
| 100 | ! |
"The object report_annual is stored in the stacomi environment, type r_ann <-get('report_annual',envir_stacomi)",
|
| 101 | ! |
domain = "R-stacomiR" |
| 102 |
) |
|
| 103 |
) |
|
| 104 | ! |
return(r_ann) |
| 105 |
|
|
| 106 |
|
|
| 107 |
} |
|
| 108 |
) |
|
| 109 | ||
| 110 | ||
| 111 |
#' connect method for report_annual class |
|
| 112 |
#' this method performs the sum over the year attention this function does |
|
| 113 |
#' not count subsamples. |
|
| 114 |
#' @param object An object of class \link{report_annual-class}
|
|
| 115 |
#' @param silent Stops messages from being displayed if silent=TRUE, default FALSE |
|
| 116 |
#' @return An instantiated object with values filled with user choice |
|
| 117 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 118 |
#' @return An object of class \link{report_annual-class} including a dataframe with column effectif, comprising the sum of report_mig counts
|
|
| 119 |
#' @importFrom dplyr anti_join arrange bind_rows |
|
| 120 |
#' @aliases connect.report_annual |
|
| 121 |
setMethod( |
|
| 122 |
"connect", |
|
| 123 |
signature = signature("report_annual"),
|
|
| 124 |
definition = function(object, silent = FALSE) |
|
| 125 |
{
|
|
| 126 | 14x |
r_ann <- object |
| 127 | 14x |
req = new("RequeteDB")
|
| 128 |
############################## |
|
| 129 |
##############################" |
|
| 130 | 14x |
start_year = r_ann@start_year@year_selected |
| 131 | 14x |
end_year = r_ann@end_year@year_selected |
| 132 | 14x |
dc = vector_to_listsql(r_ann@dc@dc_selected) |
| 133 | 14x |
tax = vector_to_listsql(r_ann@taxa@taxa_selected) |
| 134 | 14x |
std = vector_to_listsql(r_ann@stage@stage_selected) |
| 135 |
|
|
| 136 | 14x |
reqdiff = new("RequeteDB")
|
| 137 |
|
|
| 138 | 14x |
reqdiff@sql = paste( |
| 139 | 14x |
"select *, extract(year from ope_date_debut) as annee_debut, extract(year from ope_date_fin) as annee_fin FROM ", |
| 140 | 14x |
get_schema(), |
| 141 | 14x |
"t_operation_ope join ", |
| 142 | 14x |
get_schema(), |
| 143 | 14x |
"t_lot_lot on lot_ope_identifiant=ope_identifiant |
| 144 | 14x |
where ope_dic_identifiant in ", |
| 145 | 14x |
dc, |
| 146 | 14x |
" and extract(year from ope_date_debut)>=", |
| 147 | 14x |
start_year, |
| 148 | 14x |
" and extract(year from ope_date_debut)<=", |
| 149 | 14x |
end_year, |
| 150 | 14x |
" and ope_dic_identifiant in ", |
| 151 | 14x |
dc, |
| 152 | 14x |
" and lot_tax_code in ", |
| 153 | 14x |
tax, |
| 154 | 14x |
" and lot_std_code in ", |
| 155 | 14x |
std, |
| 156 | 14x |
" and lot_lot_identifiant is null |
| 157 | 14x |
order by ope_dic_identifiant, annee_debut,annee_fin; ", |
| 158 | 14x |
sep = "" |
| 159 |
) |
|
| 160 | 14x |
reqdiff@sql <- |
| 161 | 14x |
stringr::str_replace_all(reqdiff@sql, "[\r\n\t]" , " ") |
| 162 | 14x |
reqdiff <- stacomirtools::query(reqdiff) |
| 163 | 14x |
detailed_data <- stacomirtools::getquery(reqdiff) |
| 164 |
# If there are some operations with year of date_debut different to the year of date_fin we need to find these operations |
|
| 165 |
# and apply on it the overlaps function to separate fish that arrive during the first year from the rest |
|
| 166 |
#If we don't have operation on two years we apply the simple sum per year |
|
| 167 | 14x |
annee_differentes <- |
| 168 | 14x |
detailed_data$annee_debut != detailed_data$annee_fin |
| 169 | 14x |
if (any(annee_differentes)) {
|
| 170 | 8x |
data_to_cut <- detailed_data[annee_differentes, ] |
| 171 | 8x |
data_not_to_cut <- detailed_data[!annee_differentes, ] |
| 172 |
# vector of years of cut |
|
| 173 | 8x |
round_years <- |
| 174 | 8x |
lubridate::floor_date(data_to_cut$ope_date_debut, "years") + lubridate::years(1) |
| 175 | 8x |
end_of_the_year = difftime(round_years, data_to_cut$ope_date_debut, units = |
| 176 | 8x |
"days") |
| 177 | 8x |
beginning_of_the_year = difftime(data_to_cut$ope_date_fin, round_years, units = |
| 178 | 8x |
"day") |
| 179 | 8x |
operation_duration = difftime(data_to_cut$ope_date_fin, |
| 180 | 8x |
data_to_cut$ope_date_debut, |
| 181 | 8x |
units = "day") |
| 182 | 8x |
data_beginning_of_the_year <- data_to_cut |
| 183 | 8x |
data_beginning_of_the_year$lot_effectif <- |
| 184 | 8x |
data_beginning_of_the_year$lot_effectif * |
| 185 | 8x |
as.numeric(beginning_of_the_year) / as.numeric(operation_duration) |
| 186 | 8x |
data_beginning_of_the_year$ope_date_debut <- round_years |
| 187 | 8x |
data_beginning_of_the_year$annee_debut <- |
| 188 | 8x |
lubridate::year(round_years) |
| 189 | 8x |
data_end_of_the_year <- data_to_cut |
| 190 | 8x |
data_end_of_the_year$lot_effectif <- |
| 191 | 8x |
data_end_of_the_year$lot_effectif * |
| 192 | 8x |
as.numeric(end_of_the_year) / as.numeric(operation_duration) |
| 193 | 8x |
data_end_of_the_year$ope_date_fin <- round_years |
| 194 | 8x |
final_data <- |
| 195 | 8x |
rbind(data_not_to_cut, |
| 196 | 8x |
data_beginning_of_the_year, |
| 197 | 8x |
data_end_of_the_year) |
| 198 |
|
|
| 199 | 8x |
con <- new("ConnectionDB")
|
| 200 | 8x |
con <- connect(con) |
| 201 | 8x |
on.exit(pool::poolClose(con@connection)) |
| 202 | 8x |
pool::dbWriteTable(con@connection, |
| 203 | 8x |
name = "final_data", |
| 204 | 8x |
value=final_data, |
| 205 | 8x |
temporary=TRUE) |
| 206 | 8x |
r_ann@data <- pool::dbGetQuery(con@connection, |
| 207 | 8x |
" select sum(lot_effectif) as effectif, annee_debut as annee, |
| 208 | 8x |
ope_dic_identifiant, |
| 209 | 8x |
lot_tax_code, |
| 210 | 8x |
lot_std_code |
| 211 | 8x |
from |
| 212 | 8x |
final_data |
| 213 | 8x |
group by annee, ope_dic_identifiant, lot_tax_code, lot_std_code |
| 214 | 8x |
order by ope_dic_identifiant, annee, lot_tax_code, lot_std_code; ") |
| 215 |
|
|
| 216 |
} |
|
| 217 |
#If we have dc and years with no difference in the years of start and end for the same operation we calculate the "classical" sum by year |
|
| 218 |
else {
|
|
| 219 | 6x |
req@sql = paste( |
| 220 | 6x |
" select sum(lot_effectif) as effectif, annee, ope_dic_identifiant,lot_tax_code, lot_std_code from |
| 221 | 6x |
(select *, extract(year from ope_date_debut) as annee FROM ", |
| 222 | 6x |
get_schema(), |
| 223 | 6x |
"t_operation_ope ", |
| 224 | 6x |
" join ", |
| 225 | 6x |
get_schema(), |
| 226 | 6x |
"t_lot_lot on lot_ope_identifiant=ope_identifiant where ope_dic_identifiant in", |
| 227 | 6x |
dc, |
| 228 | 6x |
" and extract(year from ope_date_debut)>=", |
| 229 | 6x |
start_year, |
| 230 | 6x |
" and extract(year from ope_date_fin)<=", |
| 231 | 6x |
end_year, |
| 232 | 6x |
" and ope_dic_identifiant in ", |
| 233 | 6x |
dc, |
| 234 | 6x |
" and lot_tax_code in ", |
| 235 | 6x |
tax, |
| 236 | 6x |
" and lot_std_code in ", |
| 237 | 6x |
std, |
| 238 | 6x |
" and lot_lot_identifiant is null) as tmp", |
| 239 | 6x |
" group by annee, ope_dic_identifiant, lot_tax_code, lot_std_code ", |
| 240 | 6x |
" order by ope_dic_identifiant, annee, lot_tax_code, lot_std_code; ", |
| 241 | 6x |
sep = "" |
| 242 |
) |
|
| 243 | 6x |
req@sql <- stringr::str_replace_all(req@sql, "[\r\n\t]" , "") |
| 244 | 6x |
req <- stacomirtools::query(req) |
| 245 | 6x |
resdata <- getquery(req) |
| 246 | ||
| 247 | 6x |
all_comb <- expand.grid( |
| 248 | 6x |
annee = start_year:end_year, |
| 249 | 6x |
ope_dic_identifiant = r_ann@dc@dc_selected, |
| 250 | 6x |
lot_tax_code = r_ann@taxa@taxa_selected, |
| 251 | 6x |
lot_std_code = r_ann@stage@stage_selected |
| 252 |
) |
|
| 253 | 6x |
missing <- dplyr::anti_join(all_comb,resdata[,c("annee", "ope_dic_identifiant", "lot_tax_code",
|
| 254 | 6x |
"lot_std_code")]) |
| 255 | 6x |
if (nrow(missing) > 0){
|
| 256 | 1x |
missing$effectif = 0 |
| 257 | 1x |
r_ann@data <- dplyr::bind_rows(resdata,missing) |
| 258 |
} else {
|
|
| 259 | 5x |
r_ann@data <- resdata |
| 260 |
} |
|
| 261 | 6x |
r_ann@data <- dplyr::arrange(r_ann@data,ope_dic_identifiant, lot_tax_code, lot_std_code, annee) |
| 262 |
} |
|
| 263 | 14x |
return(r_ann) |
| 264 |
} |
|
| 265 |
) |
|
| 266 | ||
| 267 |
#' command line interface for \link{report_annual-class}
|
|
| 268 |
#' |
|
| 269 |
#' The choice_c method fills in the data slot for classes \link{ref_dc-class}, \link{ref_taxa-class},
|
|
| 270 |
#' \link{ref_stage-class} and two slots of \link{ref_year-class}
|
|
| 271 |
#' @param object An object of class \link{report_annual-class}
|
|
| 272 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 273 |
#' @param taxa Either a species name in latin or the SANDRE code for species (ie 2038=Anguilla anguilla), |
|
| 274 |
#' it should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 275 |
#' @param stage A stage code matching the ref.tr_stadedeveloppement_std table in the stacomi database, see \link{choice_c,ref_stage-method}
|
|
| 276 |
#' @param start_year The starting the first year, passed as character or integer |
|
| 277 |
#' @param end_year the finishing year |
|
| 278 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 279 |
#' @return An object of class \link{report_annual-class} with data selected
|
|
| 280 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 281 |
#' @aliases choice_c.report_annual |
|
| 282 |
setMethod( |
|
| 283 |
"choice_c", |
|
| 284 |
signature = signature("report_annual"),
|
|
| 285 |
definition = function(object, |
|
| 286 |
dc, |
|
| 287 |
taxa, |
|
| 288 |
stage, |
|
| 289 |
start_year, |
|
| 290 |
end_year, |
|
| 291 |
silent = FALSE) {
|
|
| 292 |
# code for debug using example |
|
| 293 |
#dc=c(5,6);taxa="Anguilla anguilla";stage=c("AGJ","AGG","CIV");start_year="1996";end_year="2016"
|
|
| 294 | 4x |
r_ann <- object |
| 295 | 4x |
r_ann@dc = charge(r_ann@dc) |
| 296 |
# loads and verifies the dc |
|
| 297 |
# this will set dc_selected slot |
|
| 298 | 4x |
r_ann@dc <- choice_c(object = r_ann@dc, dc) |
| 299 |
# only taxa present in the report_mig are used |
|
| 300 | 4x |
r_ann@taxa <- |
| 301 | 4x |
charge_with_filter(object = r_ann@taxa, r_ann@dc@dc_selected) |
| 302 | 4x |
r_ann@taxa <- choice_c(r_ann@taxa, taxa) |
| 303 | 4x |
r_ann@stage <- |
| 304 | 4x |
charge_with_filter(object = r_ann@stage, |
| 305 | 4x |
r_ann@dc@dc_selected, |
| 306 | 4x |
r_ann@taxa@taxa_selected) |
| 307 | 4x |
r_ann@stage <- choice_c(r_ann@stage, stage) |
| 308 |
|
|
| 309 | 4x |
r_ann@start_year <- charge(object = r_ann@start_year, |
| 310 | 4x |
objectreport = "report_annual") |
| 311 | 4x |
r_ann@start_year <- choice_c( |
| 312 | 4x |
object = r_ann@start_year, |
| 313 | 4x |
nomassign = "start_year", |
| 314 | 4x |
annee = start_year, |
| 315 | 4x |
silent = silent |
| 316 |
) |
|
| 317 | 4x |
r_ann@end_year@data <- r_ann@start_year@data |
| 318 | 4x |
r_ann@end_year <- choice_c( |
| 319 | 4x |
object = r_ann@end_year, |
| 320 | 4x |
nomassign = "end_year", |
| 321 | 4x |
annee = end_year, |
| 322 | 4x |
silent = silent |
| 323 |
) |
|
| 324 | 4x |
assign("report_annual", r_ann, envir = envir_stacomi)
|
| 325 | 4x |
return(r_ann) |
| 326 |
} |
|
| 327 |
) |
|
| 328 | ||
| 329 |
#' xtable function for \link{report_annual-class}
|
|
| 330 |
#' create an xtable objet but also assigns an add.to.column argument in envir_stacomi, |
|
| 331 |
#' for later use by the print.xtable method. |
|
| 332 |
#' @param x, an object of class "report_annual" |
|
| 333 |
#' @param caption, see xtable |
|
| 334 |
#' @param label, see xtable |
|
| 335 |
#' @param align, see xtable, overidden if NULL |
|
| 336 |
#' @param digits default 0 |
|
| 337 |
#' @param display see xtable |
|
| 338 |
#' @param auto see xtable |
|
| 339 |
#' @param dc_name A string indicating the names of the DC, in the order of \code{x@dc@dc_selected}
|
|
| 340 |
#' if not provided DC codes are used. |
|
| 341 |
#' @param tax_name A string indicating the names of the taxa, if not provided latin names are used |
|
| 342 |
#' @param std_name A string indicating the stages names, if not provided then std_libelle are used |
|
| 343 |
#' @return A xtable for annual report |
|
| 344 |
#' @aliases xtable.report_annual |
|
| 345 |
#' @export |
|
| 346 |
setMethod( |
|
| 347 |
"xtable", |
|
| 348 |
signature = signature("report_annual"),
|
|
| 349 |
definition = function(x, |
|
| 350 |
caption = NULL, |
|
| 351 |
label = NULL, |
|
| 352 |
align = NULL, |
|
| 353 |
digits = 0, |
|
| 354 |
display = NULL, |
|
| 355 |
auto = FALSE, |
|
| 356 |
dc_name = NULL, |
|
| 357 |
tax_name = NULL, |
|
| 358 |
std_name = NULL) {
|
|
| 359 | 5x |
r_ann <- x |
| 360 | 5x |
dat = r_ann@data |
| 361 | 5x |
tax = r_ann@taxa@taxa_selected |
| 362 | 5x |
std = r_ann@stage@stage_selected |
| 363 | 5x |
dc = r_ann@dc@dc_selected |
| 364 |
# giving names by default if NULL else checking that arguments dc_name, tax_name, std_name |
|
| 365 |
#have the right length |
|
| 366 | 5x |
if (is.null(dc_name)){
|
| 367 | 1x |
dc_name = r_ann@dc@data$dc_code[r_ann@dc@data$dc %in% r_ann@dc@dc_selected] |
| 368 |
} |
|
| 369 | 5x |
if (length(dc) != length(dc_name)) {
|
| 370 | ! |
stop (stringr::str_c("dc_name argument should have length ", length(dc)))
|
| 371 |
} |
|
| 372 | 5x |
if (is.null(tax_name)){
|
| 373 | 1x |
tax_name = r_ann@taxa@data$tax_nom_latin[r_ann@taxa@data$tax_code %in% r_ann@taxa@taxa_selected] |
| 374 |
} |
|
| 375 | 5x |
if (length(tax) != length(tax_name)){
|
| 376 | ! |
stop (stringr::str_c("tax_name argument should have length ", length(tax)))
|
| 377 |
} |
|
| 378 | 5x |
if (is.null(std_name)){
|
| 379 | 1x |
std_name = r_ann@stage@data$std_libelle[r_ann@stage@data$std_code %in% r_ann@stage@stage_selected] |
| 380 |
} |
|
| 381 | ||
| 382 | 5x |
if (length(std) != length(std_name)){
|
| 383 | ! |
stop (stringr::str_c("std_name argument should have length ", length(std)))
|
| 384 |
} |
|
| 385 |
|
|
| 386 | 5x |
dat <- |
| 387 | 5x |
dat[, c("annee",
|
| 388 | 5x |
"effectif", |
| 389 | 5x |
"ope_dic_identifiant", |
| 390 | 5x |
"lot_tax_code", |
| 391 | 5x |
"lot_std_code")] |
| 392 | 5x |
dat <- |
| 393 | 5x |
reshape2::dcast(dat, |
| 394 | 5x |
annee ~ ope_dic_identifiant + lot_tax_code + lot_std_code, |
| 395 | 5x |
value.var = "effectif") |
| 396 | 5x |
coln <- colnames(dat)[2:length(colnames(dat))] |
| 397 |
# names header for DC |
|
| 398 |
# this function creates title as "multicolumn" arguments, repeated over columns if necessary |
|
| 399 |
# it will be passed later as add.to.row print.xtable command |
|
| 400 | 5x |
fn_title <- function(les_valeurs, valeur_uk, name_uk, total = TRUE) {
|
| 401 | 15x |
which_arg <- match(les_valeurs, valeur_uk) |
| 402 | 15x |
if (length(les_valeurs) == 1) {
|
| 403 | ! |
repetes <- FALSE |
| 404 |
} else {
|
|
| 405 | 15x |
repetes <- |
| 406 | 15x |
c(les_valeurs[2:length(les_valeurs)] == les_valeurs[1:(length(les_valeurs) - |
| 407 | 15x |
1)], FALSE) # FALSE, at the end we want the values aggregated anyway |
| 408 |
} |
|
| 409 | 15x |
rr = 1 |
| 410 | 15x |
les_valeurs_final <- vector() |
| 411 | 15x |
for (i in 1:length(les_valeurs)) {
|
| 412 |
# if the same argument is repeated over current value and the next |
|
| 413 | 75x |
if (repetes[i]) {
|
| 414 | 30x |
rr <- rr + 1 |
| 415 |
} else {
|
|
| 416 |
# sortie de la boucle |
|
| 417 | 45x |
les_valeurs_final <- |
| 418 | 45x |
c( |
| 419 | 45x |
les_valeurs_final, |
| 420 | 45x |
stringr::str_c( |
| 421 | 45x |
"\\multicolumn{",
|
| 422 | 45x |
rr, |
| 423 | 45x |
"}{c}{",
|
| 424 | 45x |
xtable::sanitize(name_uk[which_arg[i]]), |
| 425 |
"}" |
|
| 426 |
) |
|
| 427 |
) |
|
| 428 | 45x |
rr = 1 |
| 429 |
} |
|
| 430 |
} |
|
| 431 | 15x |
if (total) {
|
| 432 | 5x |
les_valeurs_final <- |
| 433 | 5x |
stringr::str_c(" & ",
|
| 434 | 5x |
stringr::str_c(les_valeurs_final, collapse = " & "), |
| 435 | 5x |
" & Total\\\\\n") |
| 436 |
} else {
|
|
| 437 | 10x |
les_valeurs_final <- |
| 438 | 10x |
stringr::str_c(" & ",
|
| 439 | 10x |
stringr::str_c(les_valeurs_final, collapse = " & "), |
| 440 | 10x |
" & \\\\\n") |
| 441 |
} |
|
| 442 | 15x |
return(les_valeurs_final) |
| 443 |
} |
|
| 444 | 5x |
les_dc <- |
| 445 | 5x |
unlist(lapply(stringr::str_split(coln, "_"), function(X) |
| 446 | 5x |
X[1])) |
| 447 | 5x |
les_dc <- |
| 448 | 5x |
fn_title( |
| 449 | 5x |
les_valeurs = les_dc, |
| 450 | 5x |
valeur_uk = dc, |
| 451 | 5x |
name_uk = dc_name, |
| 452 | 5x |
total = FALSE |
| 453 |
) |
|
| 454 |
|
|
| 455 |
#header for tax |
|
| 456 | 5x |
les_tax <- |
| 457 | 5x |
unlist(lapply(stringr::str_split(coln, "_"), function(X) |
| 458 | 5x |
X[2])) |
| 459 | 5x |
les_tax <- |
| 460 | 5x |
fn_title( |
| 461 | 5x |
les_valeurs = les_tax, |
| 462 | 5x |
valeur_uk = tax, |
| 463 | 5x |
name_uk = tax_name, |
| 464 | 5x |
total = FALSE |
| 465 |
) |
|
| 466 |
# name header for std |
|
| 467 | 5x |
les_std <- |
| 468 | 5x |
unlist(lapply(stringr::str_split(coln, "_"), function(X) |
| 469 | 5x |
X[3])) |
| 470 | 5x |
les_std <- |
| 471 | 5x |
fn_title( |
| 472 | 5x |
les_valeurs = les_std, |
| 473 | 5x |
valeur_uk = std, |
| 474 | 5x |
name_uk = std_name, |
| 475 | 5x |
total = TRUE |
| 476 |
) |
|
| 477 |
# remove annee (it is now only rownames) |
|
| 478 | 5x |
rownames(dat) <- dat$annee |
| 479 | 5x |
dat <- dat[, -1, FALSE] |
| 480 |
# calculating sum |
|
| 481 | 5x |
if (ncol(dat) > 1) |
| 482 | 5x |
dat$sum <- rowSums(dat[, 1:ncol(dat)], na.rm = TRUE) |
| 483 |
|
|
| 484 |
|
|
| 485 | 5x |
if (is.null(align)) |
| 486 | 5x |
align <- c("l", rep("r", ncol(dat)))
|
| 487 | 5x |
if (is.null(display)) |
| 488 | 5x |
display = c("s", rep("f", ncol(dat)))
|
| 489 | 5x |
xt <- xtable::xtable( |
| 490 | 5x |
dat, |
| 491 | 5x |
caption = caption, |
| 492 | 5x |
label = label, |
| 493 | 5x |
align = align, |
| 494 | 5x |
digits = 0, |
| 495 | 5x |
display = display, |
| 496 |
# integer,small scientific if it saves place, string.. |
|
| 497 | 5x |
auto = auto |
| 498 |
) |
|
| 499 | 5x |
addtorow <- list() |
| 500 | 5x |
addtorow$pos <- list() |
| 501 | 5x |
addtorow$pos[[1]] <- 0 |
| 502 | 5x |
addtorow$pos[[2]] <- 0 |
| 503 | 5x |
addtorow$pos[[3]] <- 0 |
| 504 | 5x |
addtorow$pos[[4]] <- 0 |
| 505 | 5x |
addtorow$pos[[5]] <- 0 |
| 506 | 5x |
addtorow$command <- |
| 507 | 5x |
c(les_dc, "\\hline\n", les_tax , "\\hline\n", les_std) |
| 508 | 5x |
assign("addtorow", addtorow, envir_stacomi)
|
| 509 | 5x |
return(xt) |
| 510 |
} |
|
| 511 |
) |
|
| 512 | ||
| 513 | ||
| 514 |
#' barplot method for object \link{report_annual-class}
|
|
| 515 |
#' @param height An object of class report_annual |
|
| 516 |
#' @param legend.text See barplot help |
|
| 517 |
#' @param ... additional arguments passed to barplot |
|
| 518 |
#' @return No return value, called for side effects |
|
| 519 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 520 |
#' @aliases barplot.report_annual |
|
| 521 |
#' @seealso \link{report_annual-class} for examples
|
|
| 522 |
#' @export |
|
| 523 |
setMethod( |
|
| 524 |
"barplot", |
|
| 525 |
signature(height = "report_annual"), |
|
| 526 |
definition = function(height, legend.text = NULL, ...) {
|
|
| 527 | 5x |
r_ann <- height |
| 528 |
# require(ggplot2) |
|
| 529 | 5x |
if (nrow(r_ann@data) > 0) {
|
| 530 | 5x |
dat = r_ann@data |
| 531 | 5x |
lesdic <- unique(dat$ope_dic_identifiant) |
| 532 | 5x |
lestax <- unique(dat$lot_tax_code) |
| 533 | 5x |
lesstd <- unique(dat$lot_std_code) |
| 534 |
|
|
| 535 |
# create a matrix of each dc, taxa, stage |
|
| 536 | 5x |
if (length(lestax) == 1 & length(lesstd) & length(lesdic) == 1) {
|
| 537 | ! |
dat0 <- |
| 538 | ! |
reshape2::dcast(dat, lot_tax_code ~ annee, value.var = "effectif") |
| 539 | ! |
mat <- as.matrix(dat0[, 2:ncol(dat0)]) |
| 540 | ! |
mat[is.na(mat)] <- 0 |
| 541 | ! |
barplot(mat, ...) |
| 542 |
|
|
| 543 | 5x |
} else if (length(lestax) == 1 & length(lesstd) == 1) {
|
| 544 | 2x |
dat0 <- |
| 545 | 2x |
reshape2::dcast(dat, ope_dic_identifiant ~ annee, value.var = "effectif") |
| 546 | 2x |
mat <- as.matrix(dat0[, 2:ncol(dat0)]) |
| 547 | 2x |
mat[is.na(mat)] <- 0 |
| 548 | 2x |
if (is.null(legend.text)) {
|
| 549 | ! |
legend.text = dat0$ope_dic_identifiant |
| 550 | ! |
barplot(mat, legend.text = legend.text, ...) |
| 551 |
} else {
|
|
| 552 | 2x |
barplot(mat, ...) |
| 553 |
} |
|
| 554 |
|
|
| 555 | 5x |
} else if (length(lestax) == 1 & length(lesdic) == 1) {
|
| 556 | ! |
dat0 <- |
| 557 | ! |
reshape2::dcast(dat, lot_std_code ~ annee, value.var = "effectif") |
| 558 | ! |
mat <- as.matrix(dat0[, 2:ncol(dat0)]) |
| 559 | ! |
mat[is.na(mat)] <- 0 |
| 560 | ! |
if (is.null(legend.text)) {
|
| 561 | ! |
legend.text = dat0$lot_std_code |
| 562 | ! |
barplot(mat, legend.text = legend.text, ...) |
| 563 |
} else {
|
|
| 564 | ! |
barplot(mat, ...) |
| 565 |
} |
|
| 566 |
|
|
| 567 | 5x |
} else if (length(lesdic) == 1 & length(lesstd) == 1) {
|
| 568 | ! |
dat0 <- |
| 569 | ! |
reshape2::dcast(dat, lot_tax_code ~ annee, value.var = "effectif") |
| 570 | ! |
mat <- as.matrix(dat0[, 2:ncol(dat0)]) |
| 571 | ! |
mat[is.na(mat)] <- 0 |
| 572 | ! |
if (is.null(legend.text)) {
|
| 573 | ! |
legend.text <- dat0$lot_tax_code |
| 574 | ! |
barplot(mat, legend.text = legend.text, ...) |
| 575 |
} else {
|
|
| 576 | ! |
barplot(mat, ...) |
| 577 |
} |
|
| 578 |
|
|
| 579 | 5x |
} else if (length(lestax) == 1) {
|
| 580 | 3x |
dat0 <- |
| 581 | 3x |
reshape2::dcast(dat, |
| 582 | 3x |
ope_dic_identifiant + lot_std_code ~ annee, |
| 583 | 3x |
value.var = "effectif") |
| 584 | 3x |
mat <- as.matrix(dat0[, 3:ncol(dat0)]) |
| 585 | 3x |
mat[is.na(mat)] <- 0 |
| 586 | 3x |
if (is.null(legend.text)) {
|
| 587 | 3x |
legend.text <- |
| 588 | 3x |
stringr::str_c(dat0$ope_dic_identifiant, "_", dat0$lot_std_code) |
| 589 | 3x |
barplot(mat, legend.text = legend.text, ...) |
| 590 |
} else {
|
|
| 591 | ! |
barplot(mat, ...) |
| 592 |
} |
|
| 593 |
|
|
| 594 | 5x |
} else if (length(lesstd) == 1) {
|
| 595 | ! |
dat0 <- |
| 596 | ! |
reshape2::dcast(dat, |
| 597 | ! |
ope_dic_identifiant + lot_tax_code ~ annee, |
| 598 | ! |
value.var = "effectif") |
| 599 | ! |
mat <- as.matrix(dat0[, 3:ncol(dat0)]) |
| 600 | ! |
mat[is.na(mat)] <- 0 |
| 601 | ! |
if (is.null(legend.text)) {
|
| 602 | ! |
legend.text <- |
| 603 | ! |
stringr::str_c(dat0$ope_dic_identifiant, "_", dat0$lot_tax_code) |
| 604 | ! |
barplot(mat, legend.text = legend.text, ...) |
| 605 |
} else {
|
|
| 606 | ! |
barplot(mat, ...) |
| 607 |
} |
|
| 608 | 5x |
} else if (length(lesdic) == 1) {
|
| 609 | ! |
dat0 <- |
| 610 | ! |
reshape2::dcast(dat, lot_std_code + lot_tax_code ~ annee, value.var = "effectif") |
| 611 | ! |
mat <- as.matrix(dat0[, 3:ncol(dat0)]) |
| 612 | ! |
mat[is.na(mat)] <- 0 |
| 613 | ! |
if (is.null(legend.text)) {
|
| 614 | ! |
legend.text <- stringr::str_c(dat0$lot_tax_code, "_", dat0$lot_std_code) |
| 615 | ! |
barplot(mat, legend.text = legend.text, ...) |
| 616 |
} else {
|
|
| 617 | ! |
barplot(mat, ...) |
| 618 |
} |
|
| 619 |
|
|
| 620 |
} else {
|
|
| 621 | ! |
dat0 <- |
| 622 | ! |
reshape2::dcast(dat, |
| 623 | ! |
ope_dic_identifiant + lot_tax_code + lot_std_code ~ annee, |
| 624 | ! |
value.var = "effectif") |
| 625 | ! |
mat <- as.matrix(dat0[, 4:ncol(dat0)]) |
| 626 | ! |
mat[is.na(mat)] <- 0 |
| 627 | ! |
if (is.null(legend.text)) {
|
| 628 | ! |
legend.text <- stringr::str_c(dat0$ope_dic_identifiant, |
| 629 |
"_", |
|
| 630 | ! |
dat0$lot_tax_code, |
| 631 |
"_", |
|
| 632 | ! |
dat0$lot_std_code) |
| 633 | ! |
barplot(mat, legend.text = legend.text, ...) |
| 634 |
} else {
|
|
| 635 | ! |
barplot(mat, ...) |
| 636 |
} |
|
| 637 |
} |
|
| 638 |
} else {
|
|
| 639 | ! |
funout(gettext("No data", domain = "R-stacomiR"))
|
| 640 |
} |
|
| 641 | 5x |
return(invisible(NULL)) |
| 642 |
} |
|
| 643 |
) |
|
| 644 | ||
| 645 | ||
| 646 | ||
| 647 |
#' Plot method for report_annual |
|
| 648 |
#' |
|
| 649 |
#' @param x An object of class \link{report_annual-class}
|
|
| 650 |
#' @param plot.type Default point |
|
| 651 |
#' @param silent Stops displaying the messages. |
|
| 652 |
#' \itemize{
|
|
| 653 |
#' \item{plot.type="point": ggplot+geom_point}'
|
|
| 654 |
#' } |
|
| 655 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 656 |
#' @aliases plot.report_annual |
|
| 657 |
#' @seealso \link{report_mig_interannual-class} for examples
|
|
| 658 |
#' @return No return value, called for side effects |
|
| 659 |
#' @importFrom scales breaks_pretty |
|
| 660 |
#' @export |
|
| 661 |
setMethod( |
|
| 662 |
"plot", |
|
| 663 |
signature(x = "report_annual", y = "missing"), |
|
| 664 |
definition = function(x, |
|
| 665 |
plot.type = "point", |
|
| 666 |
silent = FALSE) {
|
|
| 667 | 4x |
r_ann <- x |
| 668 | 4x |
dat <- r_ann@data |
| 669 | 4x |
lesdic <- unique(dat$ope_dic_identifiant) |
| 670 | 4x |
lestax <- unique(dat$lot_tax_code) |
| 671 | 4x |
lesstd <- unique(dat$lot_std_code) |
| 672 | 4x |
if (nrow(r_ann@data) > 0) {
|
| 673 | 4x |
if (plot.type == "point") {
|
| 674 | 4x |
colnames(dat) <- c("effectif", "annee", "dc", "taxa", "stage")
|
| 675 | 4x |
dat$dc <- as.factor(dat$dc) |
| 676 | 4x |
dat$taxa <- as.factor(dat$taxa) |
| 677 | 4x |
if (length(lestax) == 1 & length(lesstd) & length(lesdic) == 1) {
|
| 678 |
# note below the scale is made to avoid 2000.5 2001 ... and too much breaks as well |
|
| 679 |
# see #27 |
|
| 680 | 1x |
g <- ggplot(dat, aes(x = annee, y = effectif)) + geom_point() + |
| 681 | 1x |
geom_line() + |
| 682 | 1x |
scale_x_continuous(breaks = scales::breaks_pretty(n=pmin(length(unique(dat$annee)),10))) + |
| 683 | 1x |
theme_bw() |
| 684 | 1x |
print(g) |
| 685 | 1x |
assign("g", g, envir_stacomi)
|
| 686 | 1x |
if (!silent) |
| 687 | 1x |
funout( |
| 688 | 1x |
gettext( |
| 689 | 1x |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 690 | 1x |
domain = "R-stacomiR" |
| 691 |
) |
|
| 692 |
) |
|
| 693 |
|
|
| 694 | 4x |
} else if (length(lestax) == 1 & length(lesstd) == 1) {
|
| 695 | 2x |
g <- ggplot(dat, aes(x = annee, y = effectif)) + |
| 696 | 2x |
geom_point(aes(col = dc)) + |
| 697 | 2x |
geom_line(aes(col = dc)) + |
| 698 | 2x |
scale_x_continuous(breaks = scales::breaks_pretty(n=pmin(length(unique(dat$annee)),10))) + |
| 699 | 2x |
theme_bw() |
| 700 | 2x |
print(g) |
| 701 | 2x |
assign("g", g, envir_stacomi)
|
| 702 | 2x |
if (!silent) |
| 703 | 2x |
funout( |
| 704 | 2x |
gettext( |
| 705 | 2x |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 706 | 2x |
domain = "R-stacomiR" |
| 707 |
) |
|
| 708 |
) |
|
| 709 |
|
|
| 710 | 4x |
} else if (length(lestax) == 1 & length(lesdic) == 1) {
|
| 711 | ! |
g <- ggplot(dat, aes(x = annee, y = effectif)) + geom_point(aes(col = stage)) + |
| 712 | ! |
geom_line(aes(col = stage)) + |
| 713 | ! |
scale_x_continuous(breaks = scales::breaks_pretty(n=pmin(length(unique(dat$annee)),10))) + |
| 714 | ! |
theme_bw() |
| 715 | ! |
print(g) |
| 716 | ! |
assign("g", g, envir_stacomi)
|
| 717 | ! |
if (!silent) |
| 718 | ! |
funout( |
| 719 | ! |
gettext( |
| 720 | ! |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 721 | ! |
domain = "R-stacomiR" |
| 722 |
) |
|
| 723 |
) |
|
| 724 |
|
|
| 725 | 4x |
} else if (length(lesdic) == 1 & length(lesstd) == 1) {
|
| 726 | ! |
g <- ggplot(dat, aes(x = annee, y = effectif)) + geom_point(aes(col = taxa)) + |
| 727 | ! |
geom_line(aes(col = taxa)) + |
| 728 | ! |
scale_x_continuous(breaks = scales::breaks_pretty(n=pmin(length(unique(dat$annee)),10))) + |
| 729 | ! |
theme_bw() |
| 730 | ! |
print(g) |
| 731 | ! |
assign("g", g, envir_stacomi)
|
| 732 | ! |
if (!silent) |
| 733 | ! |
funout( |
| 734 | ! |
gettext( |
| 735 | ! |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 736 | ! |
domain = "R-stacomiR" |
| 737 |
) |
|
| 738 |
) |
|
| 739 |
|
|
| 740 |
|
|
| 741 | 4x |
} else if (length(lestax) == 1) {
|
| 742 | 1x |
g <- |
| 743 | 1x |
ggplot(dat, aes(x = annee, y = effectif)) + geom_point(aes(col = dc, shape = |
| 744 | 1x |
stage)) + |
| 745 | 1x |
geom_line(aes(col = dc, linetype = stage)) + |
| 746 | 1x |
scale_x_continuous(breaks = scales::breaks_pretty(n=pmin(length(unique(dat$annee)),10))) + |
| 747 | 1x |
theme_bw() |
| 748 | 1x |
print(g) |
| 749 | 1x |
assign("g", g, envir_stacomi)
|
| 750 | 1x |
if (!silent) |
| 751 | 1x |
funout( |
| 752 | 1x |
gettext( |
| 753 | 1x |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 754 | 1x |
domain = "R-stacomiR" |
| 755 |
) |
|
| 756 |
) |
|
| 757 |
|
|
| 758 | 4x |
} else if (length(lesstd) == 1) {
|
| 759 | ! |
g <- |
| 760 | ! |
ggplot(dat, aes(x = annee, y = effectif)) + geom_point(aes(col = dc, shape = |
| 761 | ! |
taxa)) + |
| 762 | ! |
geom_line(aes(col = dc, shape = taxa)) + |
| 763 | ! |
scale_x_continuous(breaks = scales::breaks_pretty(n=pmin(length(unique(dat$annee)),10))) + |
| 764 | ! |
theme_bw() |
| 765 | ! |
print(g) |
| 766 | ! |
assign("g", g, envir_stacomi)
|
| 767 | ! |
if (!silent) |
| 768 | ! |
funout( |
| 769 | ! |
gettext( |
| 770 | ! |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 771 | ! |
domain = "R-stacomiR" |
| 772 |
) |
|
| 773 |
) |
|
| 774 |
|
|
| 775 | 4x |
} else if (length(lesdic) == 1) {
|
| 776 | ! |
g <- |
| 777 | ! |
ggplot(dat, aes(x = annee, y = effectif)) + geom_point(aes(col = taxa, shape = |
| 778 | ! |
stage)) + |
| 779 | ! |
geom_line(aes(col = taxa, shape = stage)) + |
| 780 | ! |
scale_x_continuous(breaks = scales::breaks_pretty(n=pmin(length(unique(dat$annee)),10))) + |
| 781 | ! |
theme_bw() |
| 782 | ! |
print(g) |
| 783 | ! |
assign("g", g, envir_stacomi)
|
| 784 | ! |
if (!silent) |
| 785 | ! |
funout( |
| 786 | ! |
gettext( |
| 787 | ! |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 788 | ! |
domain = "R-stacomiR" |
| 789 |
) |
|
| 790 |
) |
|
| 791 |
|
|
| 792 |
} else {
|
|
| 793 | ! |
if (length(lesdic) < 3) {
|
| 794 | ! |
g <- |
| 795 | ! |
ggplot(dat, aes(x = annee, y = effectif)) + geom_point(aes(col = taxa, shape = |
| 796 | ! |
stage)) + |
| 797 | ! |
geom_line(aes(col = taxa, shape = stage)) + |
| 798 | ! |
scale_x_continuous(breaks = scales::breaks_pretty(n=pmin(length(unique(dat$annee)),10))) + |
| 799 | ! |
facet_wrap( ~ dc) + |
| 800 | ! |
theme_bw() |
| 801 | ! |
print(g) |
| 802 | ! |
assign("g", g, envir_stacomi)
|
| 803 |
} else {
|
|
| 804 | ! |
g <- ggplot(dat, aes(x = annee, y = effectif)) + geom_point(aes(col = stage)) + |
| 805 | ! |
geom_line(aes(col = stage)) + |
| 806 | ! |
facet_grid(dc ~ stage) + |
| 807 | ! |
scale_x_continuous(breaks = scales::breaks_pretty(n=pmin(length(unique(dat$annee)),10))) + |
| 808 | ! |
theme_bw() |
| 809 | ! |
print(g) |
| 810 |
|
|
| 811 | ! |
assign("g", g, envir_stacomi)
|
| 812 | ! |
if (!silent) |
| 813 | ! |
funout( |
| 814 | ! |
gettext( |
| 815 | ! |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 816 | ! |
domain = "R-stacomiR" |
| 817 |
) |
|
| 818 |
) |
|
| 819 |
} |
|
| 820 |
} |
|
| 821 |
} |
|
| 822 |
|
|
| 823 |
} else {
|
|
| 824 | ! |
funout(gettext("No data", domain = "R-stacomiR"))
|
| 825 |
} |
|
| 826 | 4x |
return(invisible(NULL)) |
| 827 |
} |
|
| 828 | ||
| 829 |
) |
|
| 830 |
| 1 | ||
| 2 | ||
| 3 |
#' Class "report_dc" report du fonctionnement du dispositif de |
|
| 4 |
#' comptage |
|
| 5 |
#' |
|
| 6 |
#' The counting device is not always working. It may me stopped either |
|
| 7 |
#' following a monitoring protocol, or due to malfunction of the device, this |
|
| 8 |
#' class allows to draw graphics allowing an overview of the device operation |
|
| 9 |
#' @slot data A data frame |
|
| 10 |
#' @slot dc An object of class \code{ref_dc-class}
|
|
| 11 |
#' @slot horodatedebut An object of class \code{ref_horodate-class}
|
|
| 12 |
#' @slot horodatefin An object of class \code{ref_horodate-class}
|
|
| 13 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 14 |
#' \code{new("report_dc", ...)}.
|
|
| 15 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 16 |
#' @example inst/examples/report_dc-example.R |
|
| 17 |
#' @family report Objects |
|
| 18 |
#' @keywords classes |
|
| 19 |
#' @aliases report_dc |
|
| 20 |
#' @export |
|
| 21 |
setClass( |
|
| 22 |
Class = "report_dc", |
|
| 23 |
representation = representation( |
|
| 24 |
data = "data.frame", |
|
| 25 |
dc = "ref_dc", |
|
| 26 |
horodatedebut = "ref_horodate", |
|
| 27 |
horodatefin = "ref_horodate" |
|
| 28 |
), |
|
| 29 |
prototype = prototype( |
|
| 30 |
data = data.frame(), |
|
| 31 |
dc = new("ref_dc"),
|
|
| 32 |
horodatedebut = new("ref_horodate"),
|
|
| 33 |
horodatefin = new("ref_horodate")
|
|
| 34 |
) |
|
| 35 |
) |
|
| 36 | ||
| 37 | ||
| 38 | ||
| 39 | ||
| 40 |
#' connect method for report_dc |
|
| 41 |
#' |
|
| 42 |
#' loads the working periods and type of arrest or disfunction of the DC |
|
| 43 |
#' @param object An object of class \link{report_dc-class}
|
|
| 44 |
#' @param silent boolean, default FALSE, if TRUE messages are not displayed |
|
| 45 |
#' @return An object of class \link{report_dc-class} with slot data filled from the database
|
|
| 46 |
#' @aliases connect.report_dc |
|
| 47 |
#' @author cedric.briand |
|
| 48 |
setMethod( |
|
| 49 |
"connect", |
|
| 50 |
signature = signature("report_dc"),
|
|
| 51 |
definition = function(object, silent = FALSE) {
|
|
| 52 |
#object<-report_dc |
|
| 53 | 21x |
if (length(object@dc@dc_selected)==0) stop("No selected dc in repor_dc@dc@dc_selected, did you forget to use the method charge ?")
|
| 54 | 21x |
req <- new("RequeteDBwheredate")
|
| 55 | 21x |
req@select = sql <- paste( |
| 56 | 21x |
"SELECT", |
| 57 | 21x |
" per_dis_identifiant,", |
| 58 | 21x |
" per_date_debut,", |
| 59 | 21x |
" per_date_fin,", |
| 60 | 21x |
" per_commentaires,", |
| 61 | 21x |
" per_etat_fonctionnement,", |
| 62 | 21x |
" per_tar_code,", |
| 63 | 21x |
" tar_libelle AS libelle", |
| 64 | 21x |
" FROM ", |
| 65 | 21x |
get_schema(), |
| 66 | 21x |
"t_periodefonctdispositif_per per", |
| 67 | 21x |
" INNER JOIN ref.tr_typearretdisp_tar tar ON tar.tar_code=per.per_tar_code", |
| 68 | 21x |
sep = "" |
| 69 |
) |
|
| 70 | 21x |
req@colonnedebut <- "per_date_debut" |
| 71 | 21x |
req@colonnefin <- "per_date_fin" |
| 72 | 21x |
req@datedebut <- object@horodatedebut@horodate |
| 73 | 21x |
req@datefin <- object@horodatefin@horodate |
| 74 | 21x |
req@order_by <- "ORDER BY per_date_debut" |
| 75 | 21x |
req@and <- |
| 76 | 21x |
paste("AND per_dis_identifiant in ",
|
| 77 | 21x |
vector_to_listsql(object@dc@dc_selected)) |
| 78 |
#req@where=#defini dans la methode DBwheredate |
|
| 79 | 21x |
req <- |
| 80 | 21x |
stacomirtools::query(req) # appel de la methode connect de l'object DBWHEREDATE |
| 81 | 21x |
object@data <- req@query |
| 82 | 21x |
if (!silent) |
| 83 | 21x |
funout(gettext("Time steps loaded for this counting device\n", domain =
|
| 84 | 21x |
"R-stacomiR")) |
| 85 | 21x |
return(object) |
| 86 |
} |
|
| 87 |
) |
|
| 88 | ||
| 89 |
#' charge method for report_dc |
|
| 90 |
#' |
|
| 91 |
#' used by the graphical interface to retrieve the objects of referential classes |
|
| 92 |
#' assigned to envir_stacomi |
|
| 93 |
#' @param object An object of class \link{report_dc-class}
|
|
| 94 |
#' @param silent boolean, default FALSE, if TRUE messages are not displayed. |
|
| 95 |
#' @aliases charge.report_dc |
|
| 96 |
#' @return object An object of class \link{report_dc-class} with data set from values assigned in \code{envir_stacomi} environment
|
|
| 97 | ||
| 98 |
#' @keywords internal |
|
| 99 |
setMethod( |
|
| 100 |
"charge", |
|
| 101 |
signature = signature("report_dc"),
|
|
| 102 |
definition = function(object, silent = FALSE) {
|
|
| 103 | 22x |
if (exists("ref_dc", envir_stacomi)) {
|
| 104 | 22x |
object@dc <- get("ref_dc", envir_stacomi)
|
| 105 |
} else {
|
|
| 106 | ! |
funout( |
| 107 | ! |
gettext( |
| 108 | ! |
"You need to choose a counting device, clic on validate\n", |
| 109 | ! |
domain = "R-stacomiR" |
| 110 |
), |
|
| 111 | ! |
arret = TRUE |
| 112 |
) |
|
| 113 |
} |
|
| 114 |
|
|
| 115 | 22x |
if (exists("report_dc_date_debut", envir_stacomi)) {
|
| 116 | 22x |
object@horodatedebut@horodate <- |
| 117 | 22x |
get("report_dc_date_debut", envir_stacomi)
|
| 118 |
} else {
|
|
| 119 | ! |
funout(gettext("You need to choose the starting date\n", domain = "R-stacomiR"),
|
| 120 | ! |
arret = TRUE) |
| 121 |
} |
|
| 122 |
|
|
| 123 | 22x |
if (exists("report_dc_date_fin", envir_stacomi)) {
|
| 124 | 22x |
object@horodatefin@horodate <- get("report_dc_date_fin", envir_stacomi)
|
| 125 |
} else {
|
|
| 126 | ! |
funout(gettext("You need to choose the ending date\n", domain = "R-stacomiR"),
|
| 127 | ! |
arret = TRUE) |
| 128 |
} |
|
| 129 | 22x |
return(object) |
| 130 |
} |
|
| 131 |
) |
|
| 132 | ||
| 133 | ||
| 134 |
#' command line interface for report_dc class |
|
| 135 |
#' |
|
| 136 |
#' The choice_c method fills in the data slot for ref_dc, and then |
|
| 137 |
#' uses the choice_c methods of these object to "select" the data. |
|
| 138 |
#' @param object An object of class \link{ref_dc-class}
|
|
| 139 |
#' @param dc The dc to set |
|
| 140 |
#' @param horodatedebut A POSIXt or Date or character to fix the date of beginning of the report |
|
| 141 |
#' @param horodatefin A POSIXt or Date or character to fix the last date of the report |
|
| 142 |
#' @param silent Should program be silent or display messages |
|
| 143 |
#' @aliases choice_c.report_dc |
|
| 144 |
#' @return An object of class \link{ref_dc-class} with data selected
|
|
| 145 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 146 |
#' @export |
|
| 147 |
setMethod( |
|
| 148 |
"choice_c", |
|
| 149 |
signature = signature("report_dc"),
|
|
| 150 |
definition = function(object, |
|
| 151 |
dc, |
|
| 152 |
horodatedebut, |
|
| 153 |
horodatefin, |
|
| 154 |
silent = FALSE) {
|
|
| 155 |
# report_dc<-r_dc;dc=5;horodatedebut="2000-01-01";horodatefin="2015-12-31";silent=TRUE |
|
| 156 | 3x |
report_dc <- object |
| 157 | 3x |
assign("report_dc", report_dc, envir = envir_stacomi)
|
| 158 | 3x |
if (!silent) |
| 159 | 3x |
funout( |
| 160 | 3x |
gettext( |
| 161 | 3x |
"Loading of the list for fishways and choice of the time step\n", |
| 162 | 3x |
domain = "R-stacomiR" |
| 163 |
) |
|
| 164 |
) |
|
| 165 | 3x |
report_dc@dc <- charge(report_dc@dc) |
| 166 | 3x |
report_dc@dc <- choice_c(report_dc@dc, dc) |
| 167 |
# assigns the parameter (horodatedebut) of the method to the object using choice_c method for ref_dc |
|
| 168 | 3x |
report_dc@horodatedebut <- choice_c( |
| 169 | 3x |
object = report_dc@horodatedebut, |
| 170 | 3x |
nomassign = "report_dc_date_debut", |
| 171 | 3x |
funoutlabel = gettext("Beginning date has been chosen\n", domain = "R-stacomiR"),
|
| 172 | 3x |
horodate = horodatedebut, |
| 173 | 3x |
silent = silent |
| 174 |
) |
|
| 175 | 2x |
report_dc@horodatefin <- choice_c( |
| 176 | 2x |
report_dc@horodatefin, |
| 177 | 2x |
nomassign = "report_dc_date_fin", |
| 178 | 2x |
funoutlabel = gettext("Ending date has been chosen\n", domain = "R-stacomiR"),
|
| 179 | 2x |
horodate = horodatefin, |
| 180 | 2x |
silent = silent |
| 181 |
) |
|
| 182 | 2x |
assign("report_dc", report_dc, envir = envir_stacomi)
|
| 183 | 2x |
return(report_dc) |
| 184 |
} |
|
| 185 |
) |
|
| 186 | ||
| 187 |
#' Different plots for report_dc |
|
| 188 |
#' |
|
| 189 |
#' \itemize{
|
|
| 190 |
#' \item{plot.type=1}{A barplot of the operation time per month}
|
|
| 191 |
#' \item{plot.type=2}{Barchat giving the time per type of operation }
|
|
| 192 |
#' \item{plot.type=2}{Rectangle plots drawn along a line}
|
|
| 193 |
#' \item{plot.type=4}{Plots per day drawn over the period to show the operation of a df, days in x, hours in y}
|
|
| 194 |
#' } |
|
| 195 |
#' |
|
| 196 |
#' @note The program cuts periods which overlap between two month. |
|
| 197 |
#' The splitting of different periods into month is |
|
| 198 |
#' assigned to the \code{envir_stacomi} environment.
|
|
| 199 |
#' @param x An object of class \link{report_dc-class}.
|
|
| 200 |
#' @param plot.type 1 to 4, barplot, barchart, rectangle plot and box showing details of daily operation, |
|
| 201 |
#' a plot with adjacent rectangles. |
|
| 202 |
#' @param silent Stops displaying the messages default to FALSE |
|
| 203 |
#' @param main The title of the graph, if NULL a default title will be plotted |
|
| 204 |
#' with the number of the DF. |
|
| 205 |
#' @param color_type_oper Named vector of color for the graph, must match type operation default to |
|
| 206 |
#' c("Fonc normal" = "#76BEBE",
|
|
| 207 |
#' "Arr ponctuel" = "#FF6700", |
|
| 208 |
#' "Arr maint" = "#9E0142", |
|
| 209 |
#' "Dysfonc" = "#EE1874", |
|
| 210 |
#' "Non connu" = "#999999"). |
|
| 211 |
#' @param color_etat Named vector state value (must match the names "TRUE", "FALSE"). |
|
| 212 |
#' @return Nothing but prints the different plots. |
|
| 213 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 214 |
#' @aliases plot.report_dc |
|
| 215 |
#' @importFrom utils setTxtProgressBar |
|
| 216 |
#' @export |
|
| 217 |
setMethod( |
|
| 218 |
"plot", |
|
| 219 |
signature(x = "report_dc", y = "missing"), |
|
| 220 |
definition = |
|
| 221 |
function(x, |
|
| 222 |
plot.type = 1, |
|
| 223 |
silent = FALSE, |
|
| 224 |
main = NULL, |
|
| 225 |
color_type_oper = c("Fonc normal" = "#76BEBE",
|
|
| 226 |
"Arr ponctuel" = "#FF6700", |
|
| 227 |
"Arr maint" = "#9E0142", |
|
| 228 |
"Dysfonc" = "#EE1874", |
|
| 229 |
"Non connu" = "#999999"), |
|
| 230 |
color_etat=c("TRUE"="#0F313A","FALSE"="#CEB99A")) {
|
|
| 231 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 232 |
# PLOT OF TYPE BARCHART (plot.type=1 (true/false) or plot.type=2) |
|
| 233 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 234 |
#report_dc<-r_dc; require(RGtk2); require(lubridate);require(ggplot2);main=NULL;silent=FALSE;plot.type="1" |
|
| 235 |
|
|
| 236 | 8x |
report_dc <- x |
| 237 | 8x |
plot.type <- as.character(plot.type)# to pass also characters |
| 238 | 8x |
if (!plot.type %in% c("1", "2", "3", "4"))
|
| 239 | 8x |
stop('plot.type must be 1,2,3 or 4')
|
| 240 | 8x |
if (nrow(report_dc@data) == 0) |
| 241 | 8x |
funout(gettext("No data for this counting device\n", domain = "R-stacomiR"),
|
| 242 | 8x |
arret = TRUE) |
| 243 | 8x |
if (plot.type == "1" | plot.type == "2") {
|
| 244 | 4x |
t_periodefonctdispositif_per = report_dc@data # on recupere le data.frame |
| 245 | 4x |
tempsdebut <- t_periodefonctdispositif_per$per_date_debut |
| 246 | 4x |
tempsfin <- t_periodefonctdispositif_per$per_date_fin |
| 247 | 4x |
tempsdebut[tempsdebut < report_dc@horodatedebut@horodate] <- |
| 248 | 4x |
report_dc@horodatedebut@horodate |
| 249 | 4x |
tempsfin[tempsfin > report_dc@horodatefin@horodate] <- |
| 250 | 4x |
report_dc@horodatefin@horodate |
| 251 | 4x |
t_periodefonctdispositif_per = cbind(t_periodefonctdispositif_per, tempsdebut, tempsfin) |
| 252 | 4x |
seqmois = seq( |
| 253 | 4x |
from = tempsdebut[1], |
| 254 | 4x |
to = tempsfin[nrow(t_periodefonctdispositif_per)], |
| 255 | 4x |
by = "month", |
| 256 | 4x |
tz = "GMT" |
| 257 |
) |
|
| 258 | 4x |
seqmois = as.POSIXlt(round_date(seqmois, unit = "month")) |
| 259 |
# adding one month at the end to get a complete coverage of the final month |
|
| 260 | 4x |
seqmois <- c(seqmois, |
| 261 | 4x |
seqmois[length(seqmois)] %m+% months(1)) |
| 262 | 4x |
t_periodefonctdispositif_per_mois = t_periodefonctdispositif_per[1, ] |
| 263 |
############################ |
|
| 264 |
# progress bar |
|
| 265 |
########################### |
|
| 266 |
|
|
| 267 | 4x |
progress_bar <- utils::txtProgressBar() |
| 268 |
# this function assigns |
|
| 269 | 4x |
z = 0 # compteur tableau t_periodefonctdispositif_per_mois |
| 270 | 4x |
for (j in 1:nrow(t_periodefonctdispositif_per)) {
|
| 271 |
#cat( j |
|
| 272 | 2176x |
setTxtProgressBar(progress_bar,j / nrow(t_periodefonctdispositif_per)) |
| 273 | 2176x |
if (j > 1) |
| 274 | 2176x |
t_periodefonctdispositif_per_mois = rbind(t_periodefonctdispositif_per_mois, |
| 275 | 2176x |
t_periodefonctdispositif_per[j, ]) |
| 276 | 2176x |
lemoisnext = seqmois[seqmois > tempsdebut[j]][1] # le premier mois superieur a tempsdebut |
| 277 | 2176x |
while (tempsfin[j] > lemoisnext) {
|
| 278 |
# on est a cheval sur deux periodes |
|
| 279 |
|
|
| 280 |
#if (z>0) stop("erreur")
|
|
| 281 | 740x |
z = z + 1 |
| 282 | 740x |
t_periodefonctdispositif_per_mois = rbind(t_periodefonctdispositif_per_mois, |
| 283 | 740x |
t_periodefonctdispositif_per[j, ]) |
| 284 | 740x |
t_periodefonctdispositif_per_mois[j + z, "tempsdebut"] = as.POSIXct(lemoisnext) |
| 285 | 740x |
t_periodefonctdispositif_per_mois[j + z - 1, "tempsfin"] = as.POSIXct(lemoisnext) |
| 286 | 740x |
lemoisnext = seqmois[match(as.character(lemoisnext), as.character(seqmois)) + |
| 287 | 740x |
1] # on decale de 1 mois avant de rerentrer dans la boucle |
| 288 |
#if (is.na(lemoisnext) ) break |
|
| 289 |
} |
|
| 290 |
#if (is.na(lemoisnext)) break |
|
| 291 |
} |
|
| 292 | 4x |
t_periodefonctdispositif_per_mois$sumduree <- |
| 293 | 4x |
as.numeric( |
| 294 | 4x |
difftime( |
| 295 | 4x |
t_periodefonctdispositif_per_mois$tempsfin, |
| 296 | 4x |
t_periodefonctdispositif_per_mois$tempsdebut, |
| 297 | 4x |
units = "hours" |
| 298 |
) |
|
| 299 |
) |
|
| 300 | 4x |
t_periodefonctdispositif_per_mois$mois1 = strftime(as.POSIXlt(t_periodefonctdispositif_per_mois$tempsdebut), |
| 301 | 4x |
"%b") |
| 302 | 4x |
t_periodefonctdispositif_per_mois$mois = strftime(as.POSIXlt(t_periodefonctdispositif_per_mois$tempsdebut), |
| 303 | 4x |
"%m") |
| 304 | 4x |
t_periodefonctdispositif_per_mois$annee = strftime(as.POSIXlt(t_periodefonctdispositif_per_mois$tempsdebut), |
| 305 | 4x |
"%Y") |
| 306 | 4x |
cat("All done.\n")
|
| 307 | 4x |
close(progress_bar) |
| 308 | 4x |
if (is.null(main)) |
| 309 | 4x |
main <- |
| 310 | 4x |
gettextf("Operation of the counting device %s",
|
| 311 | 4x |
report_dc@dc@dc_selected) |
| 312 |
|
|
| 313 |
# graphic |
|
| 314 |
#modification of the order |
|
| 315 |
|
|
| 316 | 4x |
t_periodefonctdispositif_per_mois = t_periodefonctdispositif_per_mois[order(t_periodefonctdispositif_per_mois$per_etat_fonctionnement, |
| 317 | 4x |
decreasing = TRUE), ] |
| 318 | 4x |
g <- ggplot(t_periodefonctdispositif_per_mois, |
| 319 | 4x |
aes(x = mois, y = sumduree, fill = libelle)) + |
| 320 | 4x |
facet_grid(annee ~ .) + |
| 321 | 4x |
ggtitle(main) + |
| 322 | 4x |
ylab(gettext("duration", domain = "R-stacomiR")) +
|
| 323 | 4x |
xlab(gettext("month", domain = "R-stacomiR")) +
|
| 324 | 4x |
geom_bar(stat = 'identity') + |
| 325 | 4x |
scale_fill_manual( |
| 326 | 4x |
gettext("type_oper.", domain = "R-stacomiR"),
|
| 327 | 4x |
values = color_type_oper |
| 328 |
) + |
|
| 329 | 4x |
theme( |
| 330 | 4x |
plot.background = element_rect(fill = "white"), |
| 331 | 4x |
panel.background = element_rect(fill = "white"), |
| 332 | 4x |
legend.background = element_rect(fill = "white"), |
| 333 | 4x |
strip.background = element_rect(colour = "pink", fill = "brown"), |
| 334 | 4x |
strip.text = element_text(colour = "white"), |
| 335 | 4x |
panel.grid.major = element_blank(), |
| 336 | 4x |
panel.grid.minor = element_blank(), |
| 337 | 4x |
text = element_text(colour = "navyblue"), |
| 338 | 4x |
line = element_line(colour = "black"), |
| 339 | 4x |
legend.key = element_rect(fill = "white", colour = "black"), |
| 340 | 4x |
axis.text = element_text(colour = "black") |
| 341 |
) |
|
| 342 |
|
|
| 343 | 4x |
t_periodefonctdispositif_per_mois = t_periodefonctdispositif_per_mois[order(t_periodefonctdispositif_per_mois$per_etat_fonctionnement), ] |
| 344 | 4x |
t_periodefonctdispositif_per_mois$per_etat_fonctionnement = as.factor(t_periodefonctdispositif_per_mois$per_etat_fonctionnement) |
| 345 | 4x |
g1 <- |
| 346 | 4x |
ggplot(t_periodefonctdispositif_per_mois, |
| 347 | 4x |
aes(x = mois, y = sumduree)) + |
| 348 | 4x |
facet_grid(annee ~ .) + |
| 349 | 4x |
ggtitle(main) + |
| 350 | 4x |
ylab(gettext("duration", domain = "R-stacomiR")) +
|
| 351 | 4x |
xlab(gettext("month", domain = "R-stacomiR")) +
|
| 352 | 4x |
geom_bar(stat = 'identity', aes(fill = per_etat_fonctionnement)) + |
| 353 | 4x |
scale_fill_manual(gettext("operation", domain = "R-stacomiR"),
|
| 354 | 4x |
values = color_etat) + |
| 355 | 4x |
theme( |
| 356 | 4x |
plot.background = element_rect(fill = "white"), |
| 357 | 4x |
panel.background = element_rect(fill = "white"), |
| 358 | 4x |
legend.background = element_rect(fill = "white"), |
| 359 | 4x |
strip.background = element_rect(colour = "#C07C44", fill = "#A07C68"), |
| 360 | 4x |
strip.text = element_text(colour = "#41DADE"), |
| 361 | 4x |
panel.grid.major = element_blank(), |
| 362 | 4x |
panel.grid.minor = element_blank(), |
| 363 | 4x |
text = element_text(colour = "#482E21"), |
| 364 | 4x |
line = element_line(colour = "black"), |
| 365 | 4x |
legend.key = element_rect(fill = "white", colour = "black"), |
| 366 | 4x |
axis.text = element_text(colour = "black") |
| 367 |
) |
|
| 368 |
|
|
| 369 | 4x |
if (plot.type == "1") {
|
| 370 | 2x |
print(g) |
| 371 | 2x |
assign(x = "g_report_dc_1", |
| 372 | 2x |
value = g, |
| 373 | 2x |
envir = envir_stacomi) |
| 374 | 2x |
if (!silent){
|
| 375 | 1x |
funout(text = |
| 376 | 1x |
gettext( |
| 377 | 1x |
"Writing the ggplot into envir_stacomi environment : g_report_dc_1=get('g_report_dc_1',envir_stacomi)\n",
|
| 378 | 1x |
domain = "R-stacomiR" |
| 379 |
) |
|
| 380 |
) |
|
| 381 |
} |
|
| 382 | 4x |
} # end if plot 1 |
| 383 | 4x |
if (plot.type == "2"){
|
| 384 | 2x |
print(g1) |
| 385 | 2x |
assign("g_report_dc_2",
|
| 386 | 2x |
g1, |
| 387 | 2x |
envir = envir_stacomi) |
| 388 | 2x |
if (!silent){
|
| 389 | 1x |
funout( |
| 390 | 1x |
gettext( |
| 391 | 1x |
"Writing the ggplot into envir_stacomi environment : g_report_dc_2=get('g_report_dc_2',envir_stacomi)\n",
|
| 392 | 1x |
domain = "R-stacomiR" |
| 393 |
) |
|
| 394 |
) |
|
| 395 |
} |
|
| 396 | 4x |
} # end if plot 2 |
| 397 | 4x |
assign("periodeDC",
|
| 398 | 4x |
t_periodefonctdispositif_per_mois, |
| 399 | 4x |
envir = envir_stacomi) |
| 400 | 4x |
if (!silent) |
| 401 | 4x |
funout( |
| 402 | 4x |
gettext( |
| 403 | 4x |
"Writing the table into envir_stacomi environment : write periodeDC=get('periodeDC',envir_stacomi)\n",
|
| 404 | 4x |
domain = "R-stacomiR" |
| 405 |
) |
|
| 406 |
) |
|
| 407 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 408 |
# PLOT OF TYPE BOX (plot.type=3) |
|
| 409 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 410 | 8x |
} else if (plot.type == "3") {
|
| 411 |
#report_dc<-r_dc; require(RGtk2); require(lubridate);require(ggplot2);main=NULL;silent=FALSE;plot.type="3" |
|
| 412 | 2x |
t_periodefonctdispositif_per = report_dc@data |
| 413 | 2x |
graphdate <- function(vectordate) {
|
| 414 | 32x |
vectordate <- as.POSIXct(vectordate) |
| 415 | 32x |
attributes(vectordate) <- NULL |
| 416 | 32x |
unclass(vectordate) |
| 417 | 32x |
return(vectordate) |
| 418 |
} |
|
| 419 | 2x |
time.sequence = seq.POSIXt( |
| 420 | 2x |
from = report_dc@horodatedebut@horodate, |
| 421 | 2x |
to = report_dc@horodatefin@horodate, |
| 422 | 2x |
by = "day" |
| 423 |
) |
|
| 424 | 2x |
debut = graphdate(time.sequence[1]) |
| 425 | 2x |
fin = graphdate(time.sequence[length(time.sequence)]) |
| 426 |
|
|
| 427 |
|
|
| 428 |
|
|
| 429 |
# creation d'un graphique vide |
|
| 430 | 2x |
if (is.null(main)) |
| 431 | 2x |
main <- "" |
| 432 | 2x |
plot( |
| 433 | 2x |
graphdate(time.sequence), |
| 434 | 2x |
seq(0, 1, length.out = length(time.sequence)), |
| 435 | 2x |
xlim = c(debut, fin), |
| 436 | 2x |
type = "n", |
| 437 | 2x |
xlab = "", |
| 438 | 2x |
xaxt = "n", |
| 439 | 2x |
yaxt = "n", |
| 440 | 2x |
ylab = gettext("Counting device", domain = "R-stacomiR"),
|
| 441 | 2x |
main = main, |
| 442 |
#bty="n", |
|
| 443 | 2x |
cex = 0.8 |
| 444 |
) |
|
| 445 | 2x |
r <- round(range(time.sequence), "day") |
| 446 | 2x |
graphics::axis(1, |
| 447 | 2x |
at = graphdate(seq(r[1], r[2], by = "month")), |
| 448 | 2x |
labels = strftime(as.POSIXlt(seq(r[1], r[2], by = "month")), format = "%d-%b")) |
| 449 | 2x |
if (dim(t_periodefonctdispositif_per)[1] == 0) {
|
| 450 | ! |
rect( |
| 451 | ! |
xleft = debut, |
| 452 | ! |
ybottom = 0.6, |
| 453 | ! |
xright = fin, |
| 454 | ! |
ytop = 0.9, |
| 455 | ! |
col = "grey", |
| 456 | ! |
border = NA, |
| 457 | ! |
lwd = 1 |
| 458 |
) |
|
| 459 | ! |
rect( |
| 460 | ! |
xleft = debut, |
| 461 | ! |
ybottom = 0.1, |
| 462 | ! |
xright = fin, |
| 463 | ! |
ytop = 0.4, |
| 464 | ! |
col = color_type_oper["Non connu"], |
| 465 | ! |
border = NA, |
| 466 | ! |
lwd = 1 |
| 467 |
) |
|
| 468 | ! |
legend( |
| 469 | ! |
x = "bottom", |
| 470 | ! |
legend = gettext("Func.", "Stop", "Normal func", domain = "R-stacomiR"),
|
| 471 | ! |
pch = c(16, 16), |
| 472 | ! |
col = c("grey", color_type_oper["Non connu"]),
|
| 473 |
#horiz=TRUE, |
|
| 474 | ! |
ncol = 3, |
| 475 | ! |
bty = "n" |
| 476 |
) |
|
| 477 |
} else {
|
|
| 478 | 2x |
if (sum(t_periodefonctdispositif_per$per_etat_fonctionnement == 1) > 0) {
|
| 479 | 2x |
rect( |
| 480 | 2x |
xleft = graphdate(t_periodefonctdispositif_per$per_date_debut[t_periodefonctdispositif_per$per_etat_fonctionnement == |
| 481 | 2x |
1]), |
| 482 | 2x |
ybottom = 0.6, |
| 483 | 2x |
xright = graphdate(t_periodefonctdispositif_per$per_date_fin[t_periodefonctdispositif_per$per_etat_fonctionnement == |
| 484 | 2x |
1]), |
| 485 | 2x |
ytop = 0.9, |
| 486 | 2x |
col = color_etat["TRUE"], |
| 487 | 2x |
border = NA, |
| 488 | 2x |
lwd = 1 |
| 489 |
) |
|
| 490 |
} |
|
| 491 | 2x |
if (sum(t_periodefonctdispositif_per$per_etat_fonctionnement == 0) > |
| 492 | 2x |
0) {
|
| 493 | 2x |
rect( |
| 494 | 2x |
xleft = graphdate(t_periodefonctdispositif_per$per_date_debut[t_periodefonctdispositif_per$per_etat_fonctionnement == |
| 495 | 2x |
0]), |
| 496 | 2x |
ybottom = 0.6, |
| 497 | 2x |
xright = graphdate(t_periodefonctdispositif_per$per_date_fin[t_periodefonctdispositif_per$per_etat_fonctionnement == |
| 498 | 2x |
0]), |
| 499 | 2x |
ytop = 0.9, |
| 500 | 2x |
col = color_etat["FALSE"], |
| 501 | 2x |
border = NA, |
| 502 | 2x |
lwd = 1 |
| 503 |
) |
|
| 504 |
} |
|
| 505 |
} |
|
| 506 | 2x |
listeperiode <- |
| 507 | 2x |
fun_table_per_dis( |
| 508 | 2x |
typeperiode = t_periodefonctdispositif_per$per_tar_code, |
| 509 | 2x |
tempsdebut = t_periodefonctdispositif_per$per_date_debut, |
| 510 | 2x |
tempsfin = t_periodefonctdispositif_per$per_date_fin, |
| 511 | 2x |
libelle = t_periodefonctdispositif_per$libelle, |
| 512 | 2x |
color = color_type_oper[t_periodefonctdispositif_per$libelle], |
| 513 | 2x |
date = FALSE |
| 514 |
) |
|
| 515 |
|
|
| 516 | 2x |
for (j in 1:length(listeperiode)) {
|
| 517 |
|
|
| 518 |
|
|
| 519 | 8x |
rect( |
| 520 | 8x |
xleft = graphdate(listeperiode[[j]]$debut), |
| 521 | 8x |
ybottom = 0.1, |
| 522 | 8x |
xright = graphdate(listeperiode[[j]]$fin), |
| 523 | 8x |
ytop = 0.4, |
| 524 | 8x |
col = listeperiode[[j]]$color, |
| 525 | 8x |
border = NA, |
| 526 | 8x |
lwd = 1 |
| 527 |
) |
|
| 528 |
} |
|
| 529 | 2x |
legend ( |
| 530 | 2x |
x = debut, |
| 531 | 2x |
y = 0.6, |
| 532 | 2x |
legend = gettext( |
| 533 | 2x |
"Normal", |
| 534 | 2x |
"Stop", |
| 535 | 2x |
domain = "R-stacomiR" |
| 536 |
), |
|
| 537 | 2x |
pch = c(15, 15), |
| 538 | 2x |
col = color_etat, |
| 539 | 2x |
bty = "n", |
| 540 | 2x |
horiz = TRUE, |
| 541 | 2x |
text.width = (fin - debut) / 6 , |
| 542 | 2x |
cex = 0.8 |
| 543 |
) |
|
| 544 | 2x |
legend ( |
| 545 | 2x |
x = debut, |
| 546 | 2x |
y = 0.1, |
| 547 | 2x |
legend = names(color_type_oper), |
| 548 | 2x |
pch = c(15, 15), |
| 549 | 2x |
col = color_type_oper, |
| 550 | 2x |
bty = "n", |
| 551 | 2x |
horiz = TRUE, |
| 552 | 2x |
text.width = (fin - debut) / 6, |
| 553 | 2x |
cex = 0.8 |
| 554 |
) |
|
| 555 | 2x |
graphics::text( |
| 556 | 2x |
x = debut, |
| 557 | 2x |
y = 0.95, |
| 558 | 2x |
label = gettext("Operation of the counting device", domain = "R-stacomiR"),
|
| 559 | 2x |
font = 4, |
| 560 | 2x |
pos = 4 |
| 561 |
) |
|
| 562 | 2x |
graphics::text( |
| 563 | 2x |
x = debut, |
| 564 | 2x |
y = 0.45, |
| 565 | 2x |
label = gettext("Shutdowns types for this counting device", domain = "R-stacomiR"),
|
| 566 | 2x |
font = 4, |
| 567 | 2x |
pos = 4 |
| 568 |
) |
|
| 569 |
|
|
| 570 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 571 |
# PLOT OF TYPE BOX (plot.type=4) |
|
| 572 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 573 | 8x |
} else if (plot.type == "4") {
|
| 574 | 2x |
if (is.null(main)) |
| 575 | 2x |
main <- |
| 576 | 2x |
gettext("Working of the counting device",
|
| 577 | 2x |
report_dc@dc@dc_selected) |
| 578 |
|
|
| 579 |
#report_dc<-r_dc; require(RGtk2); require(lubridate);require(ggplot2);main=NULL;silent=FALSE;plot.type="4" |
|
| 580 | 2x |
t_periodefonctdispositif_per = report_dc@data |
| 581 | 2x |
tpp <- |
| 582 | 2x |
split_per_day( |
| 583 | 2x |
t_periodefonctdispositif_per, |
| 584 | 2x |
horodatedebut = "per_date_debut", |
| 585 | 2x |
horodatefin = "per_date_fin" |
| 586 |
) |
|
| 587 |
|
|
| 588 | 2x |
g <- ggplot(tpp) + |
| 589 | 2x |
geom_rect(aes( |
| 590 | 2x |
xmin = xmin, |
| 591 | 2x |
xmax = xmax, |
| 592 | 2x |
ymin = Hdeb, |
| 593 | 2x |
ymax = Hfin, |
| 594 | 2x |
fill = libelle |
| 595 |
), |
|
| 596 | 2x |
alpha = 0.8) + |
| 597 | 2x |
scale_fill_manual( |
| 598 | 2x |
"type", |
| 599 | 2x |
values = c( |
| 600 | 2x |
color_type_oper |
| 601 |
), |
|
| 602 | 2x |
labels = gettext( |
| 603 | 2x |
"Normal oper", |
| 604 | 2x |
"Operational stop", |
| 605 | 2x |
"Stop", |
| 606 | 2x |
"Dysfunct", |
| 607 | 2x |
"Unknown", |
| 608 | 2x |
domain = "R-stacomiR" |
| 609 |
) |
|
| 610 |
) + |
|
| 611 |
#scale_colour_manual("type",values=c("1"="#40CA2C","2"="#C8B22D","3"="#AB3B26","4"="#B46BED","5"="#B8B8B8"),
|
|
| 612 |
# labels = gettext("Normal oper","Operational stop","Stop","Dysfunct","Unknown")+ )
|
|
| 613 | 2x |
ggtitle(main) + |
| 614 | 2x |
ylab("Heure") + theme(
|
| 615 | 2x |
plot.background = element_rect(fill = "black"), |
| 616 | 2x |
panel.background = element_rect(fill = "black"), |
| 617 | 2x |
legend.background = element_rect(fill = "black"), |
| 618 | 2x |
panel.grid.major = element_blank(), |
| 619 | 2x |
panel.grid.minor = element_blank(), |
| 620 | 2x |
text = element_text(colour = "white"), |
| 621 | 2x |
line = element_line(colour = "grey50"), |
| 622 | 2x |
legend.key = element_rect(fill = "black", colour = "black"), |
| 623 | 2x |
axis.text = element_text(colour = "white") |
| 624 |
) |
|
| 625 |
|
|
| 626 | 2x |
print(g) |
| 627 | 2x |
assign("g_report_dc_4",
|
| 628 | 2x |
g, |
| 629 | 2x |
envir = envir_stacomi) |
| 630 | 2x |
if (!silent) |
| 631 | 2x |
funout( |
| 632 | 2x |
gettext( |
| 633 | 2x |
"Writing the ggplot into envir_stacomi environment : g_report_dc_4 <- get('g_report_dc_4',envir_stacomi)\n",
|
| 634 | 2x |
domain = "R-stacomiR" |
| 635 |
) |
|
| 636 |
) |
|
| 637 |
|
|
| 638 |
} |
|
| 639 | 8x |
return(invisible(NULL)) |
| 640 |
} |
|
| 641 |
) |
|
| 642 | ||
| 643 | ||
| 644 | ||
| 645 | ||
| 646 | ||
| 647 |
#' Method to print the command line of the object. |
|
| 648 |
#' @param x An object of class report_dc |
|
| 649 |
#' @param ... Additional parameters passed to print |
|
| 650 |
#' @return Nothing, called for its side effect |
|
| 651 |
#' @author cedric.briand |
|
| 652 |
#' @aliases print.report_dc |
|
| 653 |
#' @export |
|
| 654 |
setMethod( |
|
| 655 |
"print", |
|
| 656 |
signature = signature("report_dc"),
|
|
| 657 |
definition = function(x, ...) {
|
|
| 658 | 1x |
sortie1 <- "report_dc=new('report_dc')\n"
|
| 659 | 1x |
sortie2 <- stringr::str_c( |
| 660 | 1x |
"report_dc=choice_c(report_dc,", |
| 661 | 1x |
"dc=", |
| 662 | 1x |
x@dc@dc_selected, |
| 663 |
",", |
|
| 664 | 1x |
"horodatedebut=", |
| 665 | 1x |
shQuote(as.character(x@horodatedebut@horodate)), |
| 666 |
",", |
|
| 667 | 1x |
"horodatefin=", |
| 668 | 1x |
shQuote(as.character(x@horodatefin@horodate)), |
| 669 |
")" |
|
| 670 |
) |
|
| 671 |
# removing backslashes |
|
| 672 | 1x |
funout(stringr::str_c(sortie1, sortie2), ...) |
| 673 | 1x |
return(invisible(NULL)) |
| 674 |
} |
|
| 675 |
) |
|
| 676 | ||
| 677 | ||
| 678 | ||
| 679 |
#' summary for report_dc, write csv and html output, and prints summary statistics |
|
| 680 |
#' @param object An object of class \code{\link{report_dc-class}}
|
|
| 681 |
#' @param silent Should the program stay silent or display messages, default FALSE |
|
| 682 |
#' @param ... Additional parameters (not used there) |
|
| 683 |
#' @return Nothing, called for its side effect of writing html, csv files and printing summary |
|
| 684 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 685 |
#' @aliases summary.report_dc |
|
| 686 |
#' @export |
|
| 687 |
setMethod( |
|
| 688 |
"summary", |
|
| 689 |
signature = signature(object = "report_dc"), |
|
| 690 |
definition = function(object, silent = FALSE, ...) {
|
|
| 691 |
#report_dc<-r_dc |
|
| 692 | 2x |
report_dc <- object |
| 693 | 2x |
t_periodefonctdispositif_per <- |
| 694 | 2x |
report_dc@data # on recupere le data.frame |
| 695 | 2x |
t_periodefonctdispositif_per$per_date_debut <- |
| 696 | 2x |
as.character(t_periodefonctdispositif_per$per_date_debut) |
| 697 | 2x |
t_periodefonctdispositif_per$per_date_fin <- |
| 698 | 2x |
as.character(t_periodefonctdispositif_per$per_date_fin) |
| 699 | 2x |
annee = paste(unique(strftime( |
| 700 | 2x |
as.POSIXlt(t_periodefonctdispositif_per$per_date_debut), |
| 701 | 2x |
"%Y" |
| 702 | 2x |
)), collapse = "+") |
| 703 | 2x |
path1 = file.path( |
| 704 | 2x |
path.expand(get("datawd", envir = envir_stacomi)),
|
| 705 | 2x |
paste( |
| 706 | 2x |
"t_periodefonctdispositif_per_DC_", |
| 707 | 2x |
report_dc@dc@dc_selected, |
| 708 |
"_", |
|
| 709 | 2x |
annee, |
| 710 | 2x |
".csv", |
| 711 | 2x |
sep = "" |
| 712 |
), |
|
| 713 | 2x |
fsep = "\\" |
| 714 |
) |
|
| 715 | 2x |
res <- tryCatch( |
| 716 | 2x |
write.table( |
| 717 | 2x |
t_periodefonctdispositif_per, |
| 718 | 2x |
file = path1, |
| 719 | 2x |
row.names = FALSE, |
| 720 | 2x |
col.names = TRUE, |
| 721 | 2x |
sep = ";" |
| 722 | 2x |
), error = function(e) e, |
| 723 | 2x |
finally = |
| 724 | 2x |
if (!silent) funout(gettextf("Writing of %s \n", path1, domain = "R-stacomiR"))
|
| 725 |
) |
|
| 726 | 2x |
if (inherits(res, "simpleError")) {
|
| 727 | ! |
warnings("The table could not be reported, please modify datawd with options(stacomiR.path='path/to/directory'")
|
| 728 |
} else {
|
|
| 729 |
# reports works anyways, write html |
|
| 730 | 2x |
path1html <- |
| 731 | 2x |
file.path( |
| 732 | 2x |
path.expand(get("datawd", envir = envir_stacomi)),
|
| 733 | 2x |
paste( |
| 734 | 2x |
"t_periodefonctdispositif_per_DC_", |
| 735 | 2x |
report_dc@dc@dc_selected, |
| 736 |
"_", |
|
| 737 | 2x |
annee, |
| 738 | 2x |
".html", |
| 739 | 2x |
sep = "" |
| 740 |
), |
|
| 741 | 2x |
fsep = "\\" |
| 742 |
) |
|
| 743 | 2x |
funout(gettextf( |
| 744 | 2x |
"Writing of %s this might take a while, please be patient ...\n", |
| 745 | 2x |
path1html |
| 746 |
)) |
|
| 747 | 2x |
funhtml( |
| 748 | 2x |
t_periodefonctdispositif_per, |
| 749 | 2x |
caption = gettextf( |
| 750 | 2x |
"t_periodefonctdispositif_per_DC_%s_%s", |
| 751 | 2x |
report_dc@dc@dc_selected, |
| 752 | 2x |
annee |
| 753 |
), |
|
| 754 | 2x |
top = TRUE, |
| 755 | 2x |
outfile = path1html, |
| 756 | 2x |
clipboard = FALSE, |
| 757 | 2x |
append = FALSE, |
| 758 | 2x |
digits = 2 |
| 759 |
) |
|
| 760 |
} |
|
| 761 | 2x |
print(gettextf("summary statistics for CD=%s", report_dc@dc@dc_selected),
|
| 762 | 2x |
domain = "R-stacomiR") |
| 763 | 2x |
print(gettextf("dc_code=%s", report_dc@dc@data[report_dc@dc@data$dc ==
|
| 764 | 2x |
report_dc@dc@dc_selected, "dc_code"], domain = "R-stacomiR")) |
| 765 | 2x |
duree <- |
| 766 | 2x |
difftime( |
| 767 | 2x |
t_periodefonctdispositif_per$per_date_fin, |
| 768 | 2x |
t_periodefonctdispositif_per$per_date_debut, |
| 769 | 2x |
units = "day" |
| 770 |
) |
|
| 771 | 2x |
sommes <- |
| 772 | 2x |
tapply(duree, t_periodefonctdispositif_per$per_tar_code, sum) |
| 773 | 2x |
perc <- round(100 * sommes / as.numeric(sum(duree))) |
| 774 | 2x |
sommes <- round(sommes, 2) |
| 775 | 2x |
funout(gettext("Duration in days (operation type):", domain = "R-stacomiR"))
|
| 776 | 2x |
funout(paste( |
| 777 | 2x |
gettext( |
| 778 | 2x |
"Normal oper", |
| 779 | 2x |
"Operational stop", |
| 780 | 2x |
"Stop", |
| 781 | 2x |
"Dysfunct", |
| 782 | 2x |
"Unknown", |
| 783 | 2x |
gettext("Func.", "Stop", "Normal func", domain = "R-stacomiR")
|
| 784 |
), |
|
| 785 |
" :", |
|
| 786 | 2x |
sommes, |
| 787 |
"(",
|
|
| 788 | 2x |
perc, |
| 789 |
"%)", |
|
| 790 | 2x |
sep = "" |
| 791 |
)) |
|
| 792 | 2x |
sommes <- |
| 793 | 2x |
tapply(duree, |
| 794 | 2x |
t_periodefonctdispositif_per$per_etat_fonctionnement, |
| 795 | 2x |
sum) |
| 796 | 2x |
perc <- round(100 * sommes / as.numeric(sum(duree))) |
| 797 | 2x |
sommes <- round(sommes, 2) |
| 798 | 2x |
funout(gettext("Duration in days (operation):", domain = "R-stacomiR"))
|
| 799 | 2x |
funout(paste(rev( |
| 800 | 2x |
gettext("Func.", "Stop", domain = "R-stacomiR")
|
| 801 |
), |
|
| 802 |
" :", |
|
| 803 | 2x |
sommes, "(", perc, "%)", sep = ""))
|
| 804 | 2x |
return(invisible(NULL)) |
| 805 |
} |
|
| 806 |
) |
| 1 |
#' Report on fishway operation |
|
| 2 |
#' |
|
| 3 |
#' Fishways (DF) are of various nature, from very simple eel ladders fed by water discharged from the river, |
|
| 4 |
#' to more complex fishways with levels adjusted by the opening of various gates and regulators. |
|
| 5 |
#' The objective of this class is to provide an assessment of the working status of a fishway throughout the year. |
|
| 6 |
#' A number of fishes ascending a fishway has meaning only if we know that the fishway is operational, and that the counting |
|
| 7 |
#' operated on the fishway has remained operational. |
|
| 8 |
#' In the database the operation of the fishway (DF) and counting device (DC) is agregated in one table (t_periodefonctdispositif_per). |
|
| 9 |
#' The column per_etat_fonctionnement indicates whether the fishway is operational (with a boolean) and the column per_tar_code indicates |
|
| 10 |
#' the status of either the fishway or DC. In the database four types of operation are set, "1"=normal operation, |
|
| 11 |
#' "2"=Device stopped in normal operation (ie lift ascending, high tide...), |
|
| 12 |
#' "3"="Stopped for maintenance or other problem", |
|
| 13 |
#' "4"="Works but not fully operational,i.e.flow problem, flood, clogged with debris...", |
|
| 14 |
#' "5"="Not known") |
|
| 15 |
#' |
|
| 16 |
#' @include ref_df.R |
|
| 17 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 18 |
#' \code{new("report_df")}.
|
|
| 19 |
#' @slot data A data frame |
|
| 20 |
#' @slot df An object of class \code{ref_df-class}
|
|
| 21 |
#' @slot horodatedebut An object of class \code{ref_horodate-class}
|
|
| 22 |
#' @slot horodatefin An object of class \code{ref_horodate-class}
|
|
| 23 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 24 |
#' @family report Objects |
|
| 25 |
#' @keywords classes |
|
| 26 |
#' @example inst/examples/report_df-example.R |
|
| 27 |
#' @aliases report_df |
|
| 28 |
#' @export |
|
| 29 |
setClass( |
|
| 30 |
Class = "report_df", |
|
| 31 |
representation = representation( |
|
| 32 |
data = "data.frame", |
|
| 33 |
df = "ref_df", |
|
| 34 |
horodatedebut = "ref_horodate", |
|
| 35 |
horodatefin = "ref_horodate" |
|
| 36 |
), |
|
| 37 |
prototype = prototype( |
|
| 38 |
data = data.frame(), |
|
| 39 |
df = new("ref_df"),
|
|
| 40 |
horodatedebut = new("ref_horodate"),
|
|
| 41 |
horodatefin = new("ref_horodate")
|
|
| 42 |
) |
|
| 43 |
) |
|
| 44 | ||
| 45 | ||
| 46 |
#' connect method for report_df |
|
| 47 |
#' |
|
| 48 |
#' @param object An object of class \link{report_df-class}
|
|
| 49 |
#' loads the working periods and type of arrest or disfunction of the DF |
|
| 50 |
#' @param silent Boolean, TRUE removes messages. |
|
| 51 |
#' @return An object of class \code{report_df} with slot data filled from the database
|
|
| 52 |
#' @aliases connect.report_df |
|
| 53 |
#' @author cedric.briand |
|
| 54 |
setMethod( |
|
| 55 |
"connect", |
|
| 56 |
signature = signature("report_df"),
|
|
| 57 |
definition = function(object, silent = FALSE) {
|
|
| 58 |
# construit une requete DBwheredate |
|
| 59 | 21x |
req <- new("RequeteDBwheredate")
|
| 60 | 21x |
req@select = paste( |
| 61 | 21x |
"SELECT", |
| 62 | 21x |
" per_dis_identifiant,", |
| 63 | 21x |
" per_date_debut,", |
| 64 | 21x |
" per_date_fin,", |
| 65 | 21x |
" per_commentaires,", |
| 66 | 21x |
" per_etat_fonctionnement,", |
| 67 | 21x |
" per_tar_code,", |
| 68 | 21x |
" tar_libelle AS libelle", |
| 69 | 21x |
" FROM ", |
| 70 | 21x |
get_schema(), |
| 71 | 21x |
"t_periodefonctdispositif_per per", |
| 72 | 21x |
" INNER JOIN ref.tr_typearretdisp_tar tar ON tar.tar_code=per.per_tar_code", |
| 73 | 21x |
sep = "" |
| 74 |
) |
|
| 75 | 21x |
req@colonnedebut = "per_date_debut" |
| 76 | 21x |
req@colonnefin = "per_date_fin" |
| 77 | 21x |
req@order_by = "ORDER BY per_date_debut" |
| 78 | 21x |
req@datedebut <- object@horodatedebut@horodate |
| 79 | 21x |
req@datefin <- object@horodatefin@horodate |
| 80 | 21x |
req@and = paste("AND per_dis_identifiant in",
|
| 81 | 21x |
vector_to_listsql(object@df@df_selected)) |
| 82 |
#req@where=#defini dans la methode DBwheredate |
|
| 83 | 21x |
req <- |
| 84 | 21x |
stacomirtools::query(req) # appel de la methode connect de l'object DBWHEREDATE |
| 85 | 21x |
object@data <- req@query |
| 86 | 21x |
if (!silent) |
| 87 | 21x |
funout(gettext("Time steps of the fishway loaded\n", domain = "R-stacomiR"))
|
| 88 | 21x |
return(object) |
| 89 |
} |
|
| 90 |
) |
|
| 91 | ||
| 92 | ||
| 93 |
#' charge method for report_df |
|
| 94 |
#' |
|
| 95 |
#' |
|
| 96 |
#' used by the graphical interface to retrieve the objects of referential classes |
|
| 97 |
#' assigned to envir_stacomi |
|
| 98 |
#' @param object An object of class \link{report_df-class}
|
|
| 99 |
#' @param silent Keeps program silent |
|
| 100 |
#' @return An object of class \link{report_df-class} with data set from values assigned in \code{envir_stacomi} environment
|
|
| 101 |
#' @aliases charge.report_df |
|
| 102 |
#' @keywords internal |
|
| 103 |
setMethod( |
|
| 104 |
"charge", |
|
| 105 |
signature = signature("report_df"),
|
|
| 106 |
definition = function(object, silent = FALSE) {
|
|
| 107 |
# object<-BfDF |
|
| 108 | 22x |
if (exists("ref_df", envir = envir_stacomi)) {
|
| 109 | 22x |
object@df <- get("ref_df", envir = envir_stacomi)
|
| 110 |
} else {
|
|
| 111 | ! |
funout( |
| 112 | ! |
gettext( |
| 113 | ! |
"You need to choose a crossing device, clic on validate\n", |
| 114 | ! |
domain = "R-stacomiR" |
| 115 |
), |
|
| 116 | ! |
arret = TRUE |
| 117 |
) |
|
| 118 |
} |
|
| 119 |
|
|
| 120 | 22x |
if (exists("report_df_date_debut", envir = envir_stacomi)) {
|
| 121 | 22x |
object@horodatedebut@horodate <- |
| 122 | 22x |
get("report_df_date_debut", envir = envir_stacomi)
|
| 123 |
} else {
|
|
| 124 | ! |
funout(gettext("You need to choose the starting date\n", domain = "R-stacomiR"),
|
| 125 | ! |
arret = TRUE) |
| 126 |
} |
|
| 127 |
|
|
| 128 | 22x |
if (exists("report_df_date_fin", envir = envir_stacomi)) {
|
| 129 | 22x |
object@horodatefin@horodate <- |
| 130 | 22x |
get("report_df_date_fin", envir = envir_stacomi)
|
| 131 |
} else {
|
|
| 132 | ! |
funout(gettext("You need to choose the ending date\n", domain = "R-stacomiR"),
|
| 133 | ! |
arret = TRUE) |
| 134 |
} |
|
| 135 | 22x |
assign("report_df", object, envir = envir_stacomi)
|
| 136 | 22x |
return(object) |
| 137 |
} |
|
| 138 |
) |
|
| 139 | ||
| 140 |
#' command line interface for report_df class |
|
| 141 |
#' |
|
| 142 |
#' The choice_c method fills in the data slot for ref_df, and then |
|
| 143 |
#' uses the choice_c methods of these object to "select" the data. |
|
| 144 |
#' @param object An object of class \link{ref_df-class}
|
|
| 145 |
#' @param df The df to set |
|
| 146 |
#' @param horodatedebut A POSIXt or Date or character to fix the date of beginning of the report |
|
| 147 |
#' @param horodatefin A POSIXt or Date or character to fix the last date of the report |
|
| 148 |
#' @param silent Should program be silent or display messages |
|
| 149 |
#' @return An object of class \link{ref_df-class} with data selected
|
|
| 150 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 151 |
#' @aliases choice_c.report_df |
|
| 152 |
setMethod( |
|
| 153 |
"choice_c", |
|
| 154 |
signature = signature("report_df"),
|
|
| 155 |
definition = function(object, |
|
| 156 |
df, |
|
| 157 |
horodatedebut, |
|
| 158 |
horodatefin, |
|
| 159 |
silent = FALSE) {
|
|
| 160 |
# report_df<-r_df;df=2;horodatedebut="2013-01-01";horodatefin="2013-12-31";silent=TRUE |
|
| 161 | 3x |
report_df <- object |
| 162 | 3x |
assign("report_df", report_df, envir = envir_stacomi)
|
| 163 | 3x |
if (!silent) |
| 164 | 3x |
funout( |
| 165 | 3x |
gettext( |
| 166 | 3x |
"Loading of the list for fishways and choice of the time step\n", |
| 167 | 3x |
domain = "R-stacomiR" |
| 168 |
) |
|
| 169 |
) |
|
| 170 | 3x |
report_df@df <- charge(report_df@df) |
| 171 | 3x |
report_df@df <- choice_c(report_df@df, df) |
| 172 |
# assigns the parameter (horodatedebut) of the method to the object using choice_c method for ref_dc |
|
| 173 | 3x |
report_df@horodatedebut <- choice_c( |
| 174 | 3x |
object = report_df@horodatedebut, |
| 175 | 3x |
nomassign = "report_df_date_debut", |
| 176 | 3x |
funoutlabel = gettext("Beginning date has been chosen\n", domain = "R-stacomiR"),
|
| 177 | 3x |
horodate = horodatedebut, |
| 178 | 3x |
silent = silent |
| 179 |
) |
|
| 180 | 2x |
report_df@horodatefin <- choice_c( |
| 181 | 2x |
report_df@horodatefin, |
| 182 | 2x |
nomassign = "report_df_date_fin", |
| 183 | 2x |
funoutlabel = gettext("Ending date has been chosen\n", domain = "R-stacomiR"),
|
| 184 | 2x |
horodate = horodatefin, |
| 185 | 2x |
silent = silent |
| 186 |
) |
|
| 187 | 2x |
assign("report_df", report_df, envir = envir_stacomi)
|
| 188 | 2x |
return(report_df) |
| 189 |
} |
|
| 190 |
) |
|
| 191 | ||
| 192 |
#' Different plots for report_df |
|
| 193 |
#' |
|
| 194 |
#' \itemize{
|
|
| 195 |
#' \item{plot.type=1}{A barplot of the operation time per month}
|
|
| 196 |
#' \item{plot.type=2}{Barchat giving the time per type of operation }
|
|
| 197 |
#' \item{plot.type=2}{Rectangle plots drawn along a line}
|
|
| 198 |
#' \item{plot.type=4}{Plots per day drawn over the period to show the operation of a df, days in x, hours in y}
|
|
| 199 |
#' } |
|
| 200 |
#' |
|
| 201 |
#' @note The program cuts periods which overlap between two month. The splitting of different periods into month is |
|
| 202 |
#' assigned to the \code{envir_stacomi} environment.
|
|
| 203 |
#' @param x An object of class \link{report_df-class}.
|
|
| 204 |
#' @param plot.type 1 to 4. |
|
| 205 |
#' @param silent Stops displaying the messages. |
|
| 206 |
#' @param main The title of the graph, if NULL a default title will be plotted with the number of the DF. |
|
| 207 |
#' @param color_type_oper Named vector of color for the graph, must match type operation default to c( |
|
| 208 |
#' "Fonc normal" = "#1B9E77","Arr ponctuel" = "#E6AB02", "Arr maint" = "#9E0142", |
|
| 209 |
#' "Dysfonc" = "#E41A1C","Non connu" = "#999999"). |
|
| 210 |
#' @param color_etat Named vector state value (must match the names "TRUE", "FALSE"). |
|
| 211 |
#' @return Nothing but prints the different plots. |
|
| 212 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 213 |
#' @aliases plot.report_df |
|
| 214 |
#' @export |
|
| 215 |
setMethod( |
|
| 216 |
"plot", |
|
| 217 |
signature(x = "report_df", y = "missing"), |
|
| 218 |
# attention laisser sur une ligne sinon plante au check |
|
| 219 |
definition = function(x, |
|
| 220 |
plot.type = 1, |
|
| 221 |
silent = FALSE, |
|
| 222 |
main = NULL, |
|
| 223 |
color_type_oper = c("Fonc normal" = "#1B9E77",
|
|
| 224 |
"Arr ponctuel" = "#E6AB02", |
|
| 225 |
"Arr maint" = "#9E0142", |
|
| 226 |
"Dysfonc" = "#E41A1C", |
|
| 227 |
"Non connu" = "#999999"), |
|
| 228 |
color_etat = c("TRUE"="chartreuse3","FALSE"="orangered3")) {
|
|
| 229 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 230 |
# PLOT OF TYPE BARCHART (plot.type=1 (true/false) or plot.type=2) |
|
| 231 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 232 |
#report_df<-r_df; require(RGtk2); require(lubridate);require(ggplot2);main=NULL;silent=FALSE;plot.type="1" |
|
| 233 | 5x |
report_df <- x |
| 234 | 5x |
plot.type <- as.character(plot.type)# to pass also characters |
| 235 | 5x |
if (!plot.type %in% c("1", "2", "3", "4"))
|
| 236 | 5x |
stop('plot.type must be 1,2,3 or 4')
|
| 237 | 5x |
if (nrow(report_df@data) == 0) |
| 238 | 5x |
funout(gettext("No data for this fishway\n", domain = "R-stacomiR"),
|
| 239 | 5x |
arret = TRUE) |
| 240 | 5x |
if (plot.type == "1" | plot.type == "2") {
|
| 241 | 2x |
t_periodefonctdispositif_per = report_df@data # on recupere le data.frame |
| 242 | 2x |
tempsdebut <- t_periodefonctdispositif_per$per_date_debut |
| 243 | 2x |
tempsfin <- t_periodefonctdispositif_per$per_date_fin |
| 244 | 2x |
tempsdebut[tempsdebut < report_df@horodatedebut@horodate] <- |
| 245 | 2x |
report_df@horodatedebut@horodate |
| 246 | 2x |
tempsfin[tempsfin > report_df@horodatefin@horodate] <- |
| 247 | 2x |
report_df@horodatefin@horodate |
| 248 | 2x |
t_periodefonctdispositif_per = cbind(t_periodefonctdispositif_per, tempsdebut, tempsfin) |
| 249 | 2x |
seqmois = seq( |
| 250 | 2x |
from = tempsdebut[1], |
| 251 | 2x |
to = tempsfin[nrow(t_periodefonctdispositif_per)], |
| 252 | 2x |
by = "month", |
| 253 | 2x |
tz = "GMT" |
| 254 |
) |
|
| 255 | 2x |
seqmois = as.POSIXlt(round_date(seqmois, unit = "month")) |
| 256 |
# adding one month at the end to get a complete coverage of the final month |
|
| 257 | 2x |
seqmois <- c(seqmois, |
| 258 | 2x |
seqmois[length(seqmois)] %m+% months(1)) |
| 259 |
|
|
| 260 |
#seqmois<-c(seqmois,seqmois[length(seqmois)]+months(1)) |
|
| 261 | 2x |
t_periodefonctdispositif_per_mois = t_periodefonctdispositif_per[1, ] |
| 262 |
############################ |
|
| 263 |
#progress bar |
|
| 264 |
########################### |
|
| 265 | 2x |
progress_bar <- utils::txtProgressBar() |
| 266 | 2x |
z = 0 # compteur tableau t_periodefonctdispositif_per_mois |
| 267 | 2x |
for (j in 1:nrow(t_periodefonctdispositif_per)) {
|
| 268 |
#cat( j |
|
| 269 | 8522x |
setTxtProgressBar(progress_bar, j / nrow(t_periodefonctdispositif_per)) |
| 270 | 8522x |
if (j > 1) |
| 271 | 8522x |
t_periodefonctdispositif_per_mois = rbind(t_periodefonctdispositif_per_mois, |
| 272 | 8522x |
t_periodefonctdispositif_per[j, ]) |
| 273 | 8522x |
lemoisnext = seqmois[seqmois > tempsdebut[j]][1] # le premier mois superieur a tempsdebut |
| 274 | 8522x |
while (tempsfin[j] > lemoisnext) {
|
| 275 |
# on est a cheval sur deux periodes |
|
| 276 |
|
|
| 277 |
#if (z>0) stop("erreur")
|
|
| 278 | 22x |
z = z + 1 |
| 279 | 22x |
t_periodefonctdispositif_per_mois = rbind(t_periodefonctdispositif_per_mois, |
| 280 | 22x |
t_periodefonctdispositif_per[j, ]) |
| 281 | 22x |
t_periodefonctdispositif_per_mois[j + z, "tempsdebut"] = as.POSIXct(lemoisnext) |
| 282 | 22x |
t_periodefonctdispositif_per_mois[j + z - 1, "tempsfin"] = as.POSIXct(lemoisnext) |
| 283 | 22x |
lemoisnext = seqmois[match(as.character(lemoisnext), as.character(seqmois)) + |
| 284 | 22x |
1] # on decale de 1 mois avant de rerentrer dans la boucle |
| 285 |
#if (is.na(lemoisnext) ) break |
|
| 286 |
} |
|
| 287 |
#if (is.na(lemoisnext)) break |
|
| 288 |
} |
|
| 289 | 2x |
t_periodefonctdispositif_per_mois$sumduree <- |
| 290 | 2x |
as.numeric( |
| 291 | 2x |
difftime( |
| 292 | 2x |
t_periodefonctdispositif_per_mois$tempsfin, |
| 293 | 2x |
t_periodefonctdispositif_per_mois$tempsdebut, |
| 294 | 2x |
units = "hours" |
| 295 |
) |
|
| 296 |
) |
|
| 297 | 2x |
t_periodefonctdispositif_per_mois$mois1 = strftime(as.POSIXlt(t_periodefonctdispositif_per_mois$tempsdebut), |
| 298 | 2x |
"%b") |
| 299 | 2x |
t_periodefonctdispositif_per_mois$mois = strftime(as.POSIXlt(t_periodefonctdispositif_per_mois$tempsdebut), |
| 300 | 2x |
"%m") |
| 301 | 2x |
t_periodefonctdispositif_per_mois$annee = strftime(as.POSIXlt(t_periodefonctdispositif_per_mois$tempsdebut), |
| 302 | 2x |
"%Y") |
| 303 | 2x |
if (is.null(main)) |
| 304 | 2x |
main <- gettextf("Fishway operation %s", report_df@df@df_selected)
|
| 305 |
# graphic |
|
| 306 | 2x |
t_periodefonctdispositif_per_mois = t_periodefonctdispositif_per_mois[order(t_periodefonctdispositif_per_mois$per_tar_code, |
| 307 | 2x |
decreasing = TRUE), ] |
| 308 |
|
|
| 309 | 2x |
g <- ggplot(t_periodefonctdispositif_per_mois, |
| 310 | 2x |
aes(x = mois, y = sumduree, fill = libelle)) + |
| 311 | 2x |
facet_grid(annee ~ .) + |
| 312 | 2x |
ylab(gettext("duration", domain = "R-stacomiR")) +
|
| 313 | 2x |
xlab(gettext("month", domain = "R-stacomiR")) +
|
| 314 | 2x |
ggtitle(main) + |
| 315 | 2x |
geom_bar(stat = 'identity') + |
| 316 | 2x |
scale_fill_manual( |
| 317 | 2x |
gettext("operation"),
|
| 318 | 2x |
values = color_type_oper |
| 319 |
) |
|
| 320 |
|
|
| 321 | 2x |
t_periodefonctdispositif_per_mois = t_periodefonctdispositif_per_mois[order(t_periodefonctdispositif_per_mois$per_etat_fonctionnement), ] |
| 322 | 2x |
t_periodefonctdispositif_per_mois$per_etat_fonctionnement = as.factor(t_periodefonctdispositif_per_mois$per_etat_fonctionnement) |
| 323 |
|
|
| 324 | 2x |
g1 <- |
| 325 | 2x |
ggplot(t_periodefonctdispositif_per_mois, |
| 326 | 2x |
aes(x = mois, y = sumduree)) + facet_grid(annee ~ .) + |
| 327 | 2x |
ylab(gettext("duration", domain = "R-stacomiR")) +
|
| 328 | 2x |
xlab(gettext("month", domain = "R-stacomiR")) +
|
| 329 | 2x |
ggtitle(main) + |
| 330 | 2x |
geom_bar(stat = 'identity', aes(fill = per_etat_fonctionnement)) + |
| 331 | 2x |
scale_fill_manual(gettext("operation", domain = "R-stacomiR"),
|
| 332 | 2x |
values = color_etat) |
| 333 |
|
|
| 334 | 2x |
if (plot.type == "1") |
| 335 | 2x |
print(g1) |
| 336 | 2x |
if (plot.type == "2") |
| 337 | 2x |
print(g) |
| 338 | 2x |
assign("periodeDF",
|
| 339 | 2x |
t_periodefonctdispositif_per_mois, |
| 340 | 2x |
envir_stacomi) |
| 341 | 2x |
if (!silent) |
| 342 | 2x |
funout( |
| 343 | 2x |
gettext( |
| 344 | 2x |
"Writing the table into envir_stacomi environment : write periodeDF=get('periodeDF',envir_stacomi)\n",
|
| 345 | 2x |
domain = "R-stacomiR" |
| 346 |
) |
|
| 347 |
) |
|
| 348 |
# the progress bar has been assigned in envir_stacomi, we destroy it |
|
| 349 | 2x |
close(progress_bar) |
| 350 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 351 |
# PLOT OF TYPE BOX (plot.type=3) |
|
| 352 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 353 | 5x |
} else if (plot.type == "3") {
|
| 354 |
#report_df<-r_df; require(RGtk2); require(lubridate);require(ggplot2);main=NULL;silent=FALSE;plot.type="3" |
|
| 355 | 1x |
if (!silent) |
| 356 | 1x |
funout(gettext("No data for this fishway\n"))
|
| 357 | 1x |
t_periodefonctdispositif_per = report_df@data |
| 358 | 1x |
graphdate <- function(vectordate) {
|
| 359 | 18x |
vectordate <- as.POSIXct(vectordate) |
| 360 | 18x |
attributes(vectordate) <- NULL |
| 361 | 18x |
unclass(vectordate) |
| 362 | 18x |
return(vectordate) |
| 363 |
} |
|
| 364 | 1x |
time.sequence = seq.POSIXt( |
| 365 | 1x |
from = report_df@horodatedebut@horodate, |
| 366 | 1x |
to = report_df@horodatefin@horodate, |
| 367 | 1x |
by = "day" |
| 368 |
) |
|
| 369 | 1x |
debut = graphdate(time.sequence[1]) |
| 370 | 1x |
fin = graphdate(time.sequence[length(time.sequence)]) |
| 371 | ||
| 372 |
# creation d'un graphique vide |
|
| 373 | 1x |
if (is.null(main)) |
| 374 | 1x |
main <- "" |
| 375 | 1x |
plot( |
| 376 | 1x |
graphdate(time.sequence), |
| 377 | 1x |
seq(0, 1, length.out = length(time.sequence)), |
| 378 | 1x |
xlim = c(debut, fin), |
| 379 | 1x |
type = "n", |
| 380 | 1x |
xlab = "", |
| 381 | 1x |
xaxt = "n", |
| 382 | 1x |
yaxt = "n", |
| 383 | 1x |
ylab = gettext("Fishway", domain = "R-stacomiR"),
|
| 384 | 1x |
main = main, |
| 385 |
#bty="n", |
|
| 386 | 1x |
cex = 0.8 |
| 387 |
) |
|
| 388 | 1x |
r <- round(range(time.sequence), "day") |
| 389 | 1x |
graphics::axis(1, |
| 390 | 1x |
at = graphdate(seq(r[1], r[2], by = "weeks")), |
| 391 | 1x |
labels = strftime(as.POSIXlt(seq(r[1], r[2], by = "weeks")), format = "%d-%b")) |
| 392 | 1x |
if (dim(t_periodefonctdispositif_per)[1] == 0) {
|
| 393 | ! |
rect( |
| 394 | ! |
xleft = debut, |
| 395 | ! |
ybottom = 0.6, |
| 396 | ! |
xright = fin, |
| 397 | ! |
ytop = 0.9, |
| 398 | ! |
col = "grey", |
| 399 | ! |
border = NA, |
| 400 | ! |
lwd = 1 |
| 401 |
) |
|
| 402 | ! |
rect( |
| 403 | ! |
xleft = debut, |
| 404 | ! |
ybottom = 0.1, |
| 405 | ! |
xright = fin, |
| 406 | ! |
ytop = 0.4, |
| 407 | ! |
col = color_type_oper["Non connu"], |
| 408 | ! |
border = NA, |
| 409 | ! |
lwd = 1 |
| 410 |
) |
|
| 411 | ! |
legend( |
| 412 | ! |
x = "bottom", |
| 413 | ! |
legend = gettext("Func", "Stop", "Normal func", domain = "R-stacomiR"),
|
| 414 | ! |
pch = c(16, 16), |
| 415 | ! |
col = c("grey", color_type_oper["Non connu"]),
|
| 416 |
#horiz=TRUE, |
|
| 417 | ! |
ncol = 5, |
| 418 | ! |
bty = "n" |
| 419 |
) |
|
| 420 |
} else {
|
|
| 421 | 1x |
if (sum(t_periodefonctdispositif_per$per_etat_fonctionnement == 1) > 0) {
|
| 422 | 1x |
rect( |
| 423 | 1x |
xleft = graphdate(t_periodefonctdispositif_per$per_date_debut[t_periodefonctdispositif_per$per_etat_fonctionnement == |
| 424 | 1x |
1]), |
| 425 | 1x |
ybottom = 0.6, |
| 426 | 1x |
xright = graphdate(t_periodefonctdispositif_per$per_date_fin[t_periodefonctdispositif_per$per_etat_fonctionnement == |
| 427 | 1x |
1]), |
| 428 | 1x |
ytop = 0.9, |
| 429 | 1x |
col = color_etat["TRUE"], |
| 430 | 1x |
border = NA, |
| 431 | 1x |
lwd = 1 |
| 432 |
) |
|
| 433 |
} |
|
| 434 | 1x |
if (sum(t_periodefonctdispositif_per$per_etat_fonctionnement == 0) > |
| 435 | 1x |
0) {
|
| 436 | 1x |
rect( |
| 437 | 1x |
xleft = graphdate(t_periodefonctdispositif_per$per_date_debut[t_periodefonctdispositif_per$per_etat_fonctionnement == |
| 438 | 1x |
0]), |
| 439 | 1x |
ybottom = 0.6, |
| 440 | 1x |
xright = graphdate(t_periodefonctdispositif_per$per_date_fin[t_periodefonctdispositif_per$per_etat_fonctionnement == |
| 441 | 1x |
0]), |
| 442 | 1x |
ytop = 0.9, |
| 443 | 1x |
col = color_etat["FALSE"], |
| 444 | 1x |
border = NA, |
| 445 | 1x |
lwd = 1 |
| 446 |
) |
|
| 447 |
} |
|
| 448 | 1x |
listeperiode <- |
| 449 | 1x |
fun_table_per_dis( |
| 450 | 1x |
typeperiode = t_periodefonctdispositif_per$per_tar_code, |
| 451 | 1x |
tempsdebut = t_periodefonctdispositif_per$per_date_debut, |
| 452 | 1x |
tempsfin = t_periodefonctdispositif_per$per_date_fin, |
| 453 | 1x |
libelle = t_periodefonctdispositif_per$libelle, |
| 454 | 1x |
color = color_type_oper[t_periodefonctdispositif_per$libelle], |
| 455 | 1x |
date = FALSE |
| 456 |
) |
|
| 457 | ||
| 458 |
|
|
| 459 | 1x |
for (j in 1:length(listeperiode)) {
|
| 460 | ||
| 461 | 5x |
rect( |
| 462 | 5x |
xleft = graphdate(listeperiode[[j]]$debut), |
| 463 | 5x |
ybottom = 0.1, |
| 464 | 5x |
xright = graphdate(listeperiode[[j]]$fin), |
| 465 | 5x |
ytop = 0.4, |
| 466 | 5x |
col = listeperiode[[j]]$color, |
| 467 | 5x |
border = NA, |
| 468 | 5x |
lwd = 1 |
| 469 |
) |
|
| 470 |
} |
|
| 471 | 1x |
legend ( |
| 472 | 1x |
x = debut, |
| 473 | 1x |
y = 0.6, |
| 474 | 1x |
legend = gettext("Func.", "Stop", domain = "R-stacomiR"),
|
| 475 | 1x |
pch = c(15, 15), |
| 476 | 1x |
col = color_etat, |
| 477 | 1x |
bty = "n", |
| 478 | 1x |
horiz = TRUE, |
| 479 | 1x |
text.width = (fin - debut) / 6 , |
| 480 | 1x |
cex = 0.8 |
| 481 |
) |
|
| 482 | 1x |
legend ( |
| 483 | 1x |
x = debut, |
| 484 | 1x |
y = 0.1, |
| 485 | 1x |
legend = names(color_type_oper), |
| 486 | 1x |
pch = c(15, 15), |
| 487 | 1x |
col = color_type_oper, |
| 488 | 1x |
bty = "n", |
| 489 | 1x |
horiz = TRUE, |
| 490 | 1x |
text.width = (fin - debut) / 8, |
| 491 | 1x |
cex = 0.7 |
| 492 |
) |
|
| 493 | 1x |
text( |
| 494 | 1x |
x = debut, |
| 495 | 1x |
y = 0.95, |
| 496 | 1x |
label = gettext("Fishway operation", domain = "R-stacomiR"),
|
| 497 | 1x |
font = 4, |
| 498 | 1x |
pos = 4 |
| 499 |
) |
|
| 500 | 1x |
text( |
| 501 | 1x |
x = debut, |
| 502 | 1x |
y = 0.45, |
| 503 | 1x |
label = gettext("Shutdowns types for this fishway", domain = "R-stacomiR"),
|
| 504 | 1x |
font = 4, |
| 505 | 1x |
pos = 4 |
| 506 |
) |
|
| 507 |
} |
|
| 508 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 509 |
# PLOT OF TYPE BOX (plot.type=4) |
|
| 510 |
#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 511 | 5x |
} else if (plot.type == "4") {
|
| 512 | 2x |
if (is.null(main)) |
| 513 | 2x |
main <- gettextf("Fishway operation %s", report_df@df@df_selected)
|
| 514 |
|
|
| 515 |
#report_df<-r_df; require(RGtk2); require(lubridate);require(ggplot2);main=NULL;silent=FALSE;plot.type="4" |
|
| 516 | 2x |
t_periodefonctdispositif_per = report_df@data |
| 517 | 2x |
tpp <- |
| 518 | 2x |
split_per_day( |
| 519 | 2x |
t_periodefonctdispositif_per, |
| 520 | 2x |
horodatedebut = "per_date_debut", |
| 521 | 2x |
horodatefin = "per_date_fin" |
| 522 |
) |
|
| 523 |
|
|
| 524 | 2x |
g <- ggplot(tpp) + |
| 525 | 2x |
geom_rect( |
| 526 | 2x |
aes( |
| 527 | 2x |
xmin = xmin, |
| 528 | 2x |
xmax = xmax, |
| 529 | 2x |
ymin = Hdeb, |
| 530 | 2x |
ymax = Hfin, |
| 531 | 2x |
col = libelle, |
| 532 | 2x |
fill = libelle |
| 533 |
), |
|
| 534 | 2x |
alpha = 0.5 |
| 535 |
) + |
|
| 536 | 2x |
scale_fill_manual( |
| 537 | 2x |
"type", |
| 538 | 2x |
values = color_type_oper, |
| 539 | 2x |
labels = gettext( |
| 540 | 2x |
"Normal oper", |
| 541 | 2x |
"Operational stop", |
| 542 | 2x |
"Stop", |
| 543 | 2x |
"Dysfunct", |
| 544 | 2x |
"Unknown", |
| 545 | 2x |
domain = "R-stacomiR" |
| 546 |
) |
|
| 547 |
) + |
|
| 548 | 2x |
scale_colour_manual( |
| 549 | 2x |
"type", |
| 550 | 2x |
values = color_type_oper, |
| 551 | 2x |
labels = gettext( |
| 552 | 2x |
"Normal oper", |
| 553 | 2x |
"Operational stop", |
| 554 | 2x |
"Stop", |
| 555 | 2x |
"Dysfunct", |
| 556 | 2x |
"Unknown", |
| 557 | 2x |
domain = "R-stacomiR" |
| 558 |
) |
|
| 559 |
) + |
|
| 560 | 2x |
ylab("Heure") + theme(
|
| 561 | 2x |
plot.background = element_rect(fill = "black"), |
| 562 | 2x |
panel.background = element_rect(fill = "black"), |
| 563 | 2x |
legend.background = element_rect(fill = "black"), |
| 564 | 2x |
panel.grid.major = element_blank(), |
| 565 | 2x |
panel.grid.minor = element_blank(), |
| 566 | 2x |
text = element_text(colour = "white"), |
| 567 | 2x |
line = element_line(colour = "grey50"), |
| 568 | 2x |
legend.key = element_rect(fill = "black", colour = "black"), |
| 569 | 2x |
axis.text = element_text(colour = "white") |
| 570 |
) |
|
| 571 |
|
|
| 572 | 2x |
print(g) |
| 573 |
|
|
| 574 |
} |
|
| 575 | 5x |
return(invisible(NULL)) |
| 576 |
} |
|
| 577 |
) |
|
| 578 | ||
| 579 | ||
| 580 |
#' Internal use, function used in the graphical interface to create a barchart for report_df class |
|
| 581 |
#' |
|
| 582 |
#' @note The program cuts periods which overlap between two month |
|
| 583 |
#' @param ... additional parameters |
|
| 584 |
#' @return Nothing, called for its side effect of plotting data |
|
| 585 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 586 |
#' @keywords internal |
|
| 587 |
funbarchartDF = function(...) {
|
|
| 588 | ! |
report_df <- get("report_df", envir = envir_stacomi)
|
| 589 | ! |
report_df <- charge(report_df) |
| 590 | ! |
report_df <- connect(report_df) |
| 591 | ! |
if (nrow(report_df@data) == 0) {
|
| 592 | ! |
funout(gettext("No data for this fishway\n", domain = "R-stacomiR"),
|
| 593 | ! |
arret = TRUE) |
| 594 |
} |
|
| 595 | ! |
plot(report_df, plot.type = 1, silent = FALSE) |
| 596 | ! |
return(invisible(NULL)) |
| 597 |
} |
|
| 598 | ||
| 599 | ||
| 600 |
#' Internal use barchart for report_df class from the graphical interface |
|
| 601 |
#' |
|
| 602 |
#' @note The program cuts periods which overlap between two month |
|
| 603 |
#' @param ... additional parameters |
|
| 604 |
#' @return Nothing, called for its side effect of plotting data |
|
| 605 |
#' @keywords internal |
|
| 606 |
funbarchart1DF = function(...) {
|
|
| 607 | ! |
report_df <- get("report_df", envir = envir_stacomi)
|
| 608 | ! |
report_df <- charge(report_df) |
| 609 | ! |
report_df <- connect(report_df) |
| 610 | ! |
if (nrow(report_df@data) == 0) {
|
| 611 | ! |
funout(gettext("No data for this fishway\n", domain = "R-stacomiR"),
|
| 612 | ! |
arret = TRUE) |
| 613 |
} |
|
| 614 | ! |
plot(report_df, plot.type = 2, silent = FALSE) |
| 615 | ! |
return(invisible(NULL)) |
| 616 |
} |
|
| 617 | ||
| 618 |
#' Internal use, rectangles to describe the DF work for report_df class, |
|
| 619 |
#' |
|
| 620 |
#' @param ... additional parameters |
|
| 621 |
#' @return Nothing, called for its side effect of plotting data |
|
| 622 |
#' @keywords internal |
|
| 623 |
funboxDF = function(...) {
|
|
| 624 | ! |
report_df <- get("report_df", envir = envir_stacomi)
|
| 625 | ! |
report_df <- charge(report_df) |
| 626 | ! |
report_df <- connect(report_df) |
| 627 |
|
|
| 628 | ! |
if (nrow(report_df@data) == 0) {
|
| 629 | ! |
funout(gettext("No data for this fishway\n", domain = "R-stacomiR"),
|
| 630 | ! |
arret = TRUE) |
| 631 |
} |
|
| 632 | ! |
plot(report_df, plot.type = 3, silent = FALSE) |
| 633 | ! |
return(invisible(NULL)) |
| 634 |
|
|
| 635 |
} |
|
| 636 | ||
| 637 |
#' Function to plot calendar like graph, internal use |
|
| 638 |
#' @param ... additional parameters |
|
| 639 |
#' @return Nothing, called for its side effect of plotting data |
|
| 640 |
#' @keywords internal |
|
| 641 |
funchartDF = function(...) {
|
|
| 642 | ! |
report_df <- get("report_df", envir = envir_stacomi)
|
| 643 | ! |
report_df <- charge(report_df) |
| 644 | ! |
report_df <- connect(report_df) |
| 645 |
|
|
| 646 | ! |
if (nrow(report_df@data) == 0) {
|
| 647 | ! |
funout(gettext("No data for this fishway\n", domain = "R-stacomiR"),
|
| 648 | ! |
arret = TRUE) |
| 649 |
} |
|
| 650 | ! |
plot(report_df, plot.type = 4, silent = FALSE) |
| 651 | ! |
return(invisible(NULL)) |
| 652 |
|
|
| 653 |
} |
|
| 654 | ||
| 655 |
#' Table output for report_df class |
|
| 656 |
#' @param ... additional parameters |
|
| 657 |
#' @return Nothing, called for its side effect of running summary |
|
| 658 |
#' @keywords internal |
|
| 659 |
funtableDF = function(...) {
|
|
| 660 | ! |
report_df <- get("report_df", envir = envir_stacomi)
|
| 661 | ! |
report_df <- charge(report_df) |
| 662 | ! |
report_df <- connect(report_df) |
| 663 |
|
|
| 664 | ! |
if (nrow(report_df@data) == 0) {
|
| 665 | ! |
funout(gettext("No data for this fishway\n", domain = "R-stacomiR"),
|
| 666 | ! |
arret = TRUE) |
| 667 |
} |
|
| 668 | ! |
summary(report_df) |
| 669 | ! |
return(invisible(NULL)) |
| 670 |
} |
|
| 671 | ||
| 672 | ||
| 673 |
#' Method to print the command line of the object |
|
| 674 |
#' @param x An object of class report_df |
|
| 675 |
#' @param ... Additional parameters passed to print |
|
| 676 |
#' @return Nothing, called for its side effect of printing data |
|
| 677 |
#' @author cedric.briand |
|
| 678 |
#' @aliases print.report_df |
|
| 679 |
#' @export |
|
| 680 |
setMethod( |
|
| 681 |
"print", |
|
| 682 |
signature = signature("report_df"),
|
|
| 683 |
definition = function(x, ...) {
|
|
| 684 | 1x |
sortie1 <- "report_df=new('report_df')\n"
|
| 685 | 1x |
sortie2 <- stringr::str_c( |
| 686 | 1x |
"report_df=choice_c(report_df,", |
| 687 | 1x |
"df=", |
| 688 | 1x |
x@df@df_selected, |
| 689 |
",", |
|
| 690 | 1x |
"horodatedebut=", |
| 691 | 1x |
shQuote(as.character(x@horodatedebut@horodate)), |
| 692 |
",", |
|
| 693 | 1x |
"horodatefin=", |
| 694 | 1x |
shQuote(as.character(x@horodatefin@horodate)), |
| 695 |
")" |
|
| 696 |
) |
|
| 697 |
# removing backslashes |
|
| 698 | 1x |
funout(stringr::str_c(sortie1, sortie2), ...) |
| 699 | 1x |
return(invisible(NULL)) |
| 700 |
} |
|
| 701 |
) |
|
| 702 | ||
| 703 | ||
| 704 |
#' summary for report_df, write csv and html output, and prints summary statistics |
|
| 705 |
#' @param object An object of class \code{\link{report_df-class}}
|
|
| 706 |
#' @param silent Should the program stay silent or display messages, default FALSE |
|
| 707 |
#' @param ... Additional parameters (not used there) |
|
| 708 |
#' @return Nothing, called for its side effect of writing html, csv files and printing summary |
|
| 709 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 710 |
#' @aliases summary.report_df |
|
| 711 |
#' @export |
|
| 712 |
setMethod( |
|
| 713 |
"summary", |
|
| 714 |
signature = signature(object = "report_df"), |
|
| 715 |
definition = function(object, silent = FALSE, ...) {
|
|
| 716 |
#report_df<-r_df; |
|
| 717 | 1x |
report_df <- object |
| 718 | 1x |
t_periodefonctdispositif_per <- report_df@data # on recupere le data.frame |
| 719 | 1x |
t_periodefonctdispositif_per$per_date_debut <- as.character(t_periodefonctdispositif_per$per_date_debut) |
| 720 | 1x |
t_periodefonctdispositif_per$per_date_fin <- as.character(t_periodefonctdispositif_per$per_date_fin) |
| 721 | 1x |
annee <- paste(unique(strftime( |
| 722 | 1x |
as.POSIXlt(t_periodefonctdispositif_per$per_date_debut), |
| 723 | 1x |
"%Y" |
| 724 | 1x |
)), collapse = "+") |
| 725 | 1x |
path1 <- file.path( |
| 726 | 1x |
path.expand(get("datawd", envir = envir_stacomi)),
|
| 727 | 1x |
paste( |
| 728 | 1x |
"t_periodefonctdispositif_per_DF_", |
| 729 | 1x |
report_df@df@df_selected, |
| 730 |
"_", |
|
| 731 | 1x |
annee, |
| 732 | 1x |
".csv", |
| 733 | 1x |
sep = "" |
| 734 |
), |
|
| 735 | 1x |
fsep = "\\" |
| 736 |
) |
|
| 737 | 1x |
res <- tryCatch( |
| 738 | 1x |
write.table( |
| 739 | 1x |
t_periodefonctdispositif_per, |
| 740 | 1x |
file = path1, |
| 741 | 1x |
row.names = FALSE, |
| 742 | 1x |
col.names = TRUE, |
| 743 | 1x |
sep = ";" |
| 744 | 1x |
), error = function(e) e, |
| 745 | 1x |
finally = |
| 746 | 1x |
if (!silent) funout(gettextf("Writing of %s \n", path1, domain = "R-stacomiR"))
|
| 747 |
) |
|
| 748 | 1x |
if (inherits(res, "simpleError")) {
|
| 749 | ! |
warnings("The table could not be reported, please modify datawd with options(stacomiR.path='path/to/directory'")
|
| 750 |
} else {
|
|
| 751 |
|
|
| 752 | 1x |
path1html <- file.path( |
| 753 | 1x |
path.expand(get("datawd", envir = envir_stacomi)),
|
| 754 | 1x |
paste( |
| 755 | 1x |
"t_periodefonctdispositif_per_DF_", |
| 756 | 1x |
report_df@df@df_selected, |
| 757 |
"_", |
|
| 758 | 1x |
annee, |
| 759 | 1x |
".html", |
| 760 | 1x |
sep = "" |
| 761 |
), |
|
| 762 | 1x |
fsep = "\\" |
| 763 |
) |
|
| 764 | 1x |
if (!silent) |
| 765 | 1x |
funout(gettextf( |
| 766 | 1x |
"Writing of %s this might take a while, please be patient ...\n", |
| 767 | 1x |
path1html |
| 768 |
)) |
|
| 769 | 1x |
funhtml( |
| 770 | 1x |
t_periodefonctdispositif_per, |
| 771 | 1x |
caption = paste( |
| 772 | 1x |
"t_periodefonctdispositif_per_DF_", |
| 773 | 1x |
report_df@df@df_selected, |
| 774 |
"_", |
|
| 775 | 1x |
annee, |
| 776 | 1x |
sep = "" |
| 777 |
), |
|
| 778 | 1x |
top = TRUE, |
| 779 | 1x |
outfile = path1html, |
| 780 | 1x |
clipboard = FALSE, |
| 781 | 1x |
append = FALSE, |
| 782 | 1x |
digits = 2 |
| 783 |
) |
|
| 784 |
} |
|
| 785 | 1x |
t_periodefonctdispositif_per <- report_df@data |
| 786 | 1x |
print(gettextf("summary statistics for DF=%s", report_df@df@df_selected))
|
| 787 | 1x |
print(gettextf("df_code=%s", report_df@df@data[report_df@df@data$df ==
|
| 788 | 1x |
report_df@df@df_selected, "df_code"])) |
| 789 | 1x |
duree <- |
| 790 | 1x |
difftime( |
| 791 | 1x |
t_periodefonctdispositif_per$per_date_fin, |
| 792 | 1x |
t_periodefonctdispositif_per$per_date_debut, |
| 793 | 1x |
units = "day" |
| 794 |
) |
|
| 795 | 1x |
sommes <- |
| 796 | 1x |
tapply(duree, t_periodefonctdispositif_per$per_tar_code, sum) |
| 797 | 1x |
perc <- round(100 * sommes / as.numeric(sum(duree))) |
| 798 | 1x |
sommes <- round(sommes, 2) |
| 799 | 1x |
funout(gettext("Duration in days (operation type):", domain = "R-stacomiR"))
|
| 800 | 1x |
funout(paste( |
| 801 | 1x |
gettext( |
| 802 | 1x |
"Normal oper", |
| 803 | 1x |
"Operational stop", |
| 804 | 1x |
"Stop", |
| 805 | 1x |
"Dysfunct", |
| 806 | 1x |
"Unknown", |
| 807 | 1x |
domain = "R-stacomiR" |
| 808 |
), |
|
| 809 |
" :", |
|
| 810 | 1x |
sommes, |
| 811 |
"(",
|
|
| 812 | 1x |
perc, |
| 813 |
"%)", |
|
| 814 | 1x |
sep = "" |
| 815 |
)) |
|
| 816 | 1x |
sommes <- |
| 817 | 1x |
tapply(duree, |
| 818 | 1x |
t_periodefonctdispositif_per$per_etat_fonctionnement, |
| 819 | 1x |
sum) |
| 820 | 1x |
perc <- round(100 * sommes / as.numeric(sum(duree))) |
| 821 | 1x |
sommes <- round(sommes, 2) |
| 822 | 1x |
funout(gettext("Duration in days (operation):", domain = "R-stacomiR"))
|
| 823 | 1x |
funout(paste(rev( |
| 824 | 1x |
gettext("Func.", "Stop", domain = "R-stacomiR")
|
| 825 |
), |
|
| 826 |
" :", |
|
| 827 | 1x |
sommes, "(", perc, "%)", sep = ""))
|
| 828 | 1x |
return(invisible(NULL)) |
| 829 |
} |
|
| 830 |
) |
| 1 |
#' Migration report along with quantitative and |
|
| 2 |
#' qualitative characteristics |
|
| 3 |
#' |
|
| 4 |
#' Migration along with qualitative or quantitative characteristics or both |
|
| 5 |
#' (e.g.) weight of eels according to the size class per period of time, weight |
|
| 6 |
#' of fish according to gender, number of fish per age class. This class does not split migration evenly over |
|
| 7 |
#' time period. So, unlike calculations made in class report_mig and report_mig_mult |
|
| 8 |
#' the whole time span of the migration operation is not considered, only the date of beginning of |
|
| 9 |
#' the operation is used to perform calculations. |
|
| 10 |
#' |
|
| 11 |
#' @include ref_parquan.R |
|
| 12 |
#' @include ref_parqual.R |
|
| 13 |
#' @include ref_choice.R |
|
| 14 |
#' @include report_sample_char.R |
|
| 15 |
#' @note The main difference between this class and \link{report_sample_char-class} is that this class allows to
|
|
| 16 |
#' select (or not) the samples, and that it handles quantitative and qualitative parameters separately. |
|
| 17 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 18 |
#' \code{new('report_mig_char', ...)}. they are loaded by the interface
|
|
| 19 |
#' using interface_report_mig_char function. |
|
| 20 |
#' @slot calcdata A 'list' of calculated data, filled in by the calcule method |
|
| 21 |
#' @slot data A \code{data.frame} inherited from \link{report_sample_char-class}
|
|
| 22 |
#' @slot dc An object of class \link{ref_dc-class} inherited from \link{report_sample_char-class}
|
|
| 23 |
#' @slot taxa An object of class \link{ref_taxa-class} inherited from \link{report_sample_char-class}
|
|
| 24 |
#' @slot stage An object of class \link{ref_stage-class} inherited from \link{report_sample_char-class}
|
|
| 25 |
#' @slot horodatedebut An object of class \link{ref_horodate-class} inherited from \link{report_sample_char-class}
|
|
| 26 |
#' @slot horodatefin An object of class \link{ref_horodate-class} inherited from \link{report_sample_char-class}
|
|
| 27 |
#' @slot par An object of class \link{ref_par-class} inherited from \link{report_sample_char-class}
|
|
| 28 |
#' @slot echantillon An object of class \link{ref_choice-class}, vector of choice
|
|
| 29 |
#' @slot parquan An object of class \link{ref_parquan-class}, quantitative parameter
|
|
| 30 |
#' @slot parqual An object of class \link{ref_parqual-class}, qualitative parameter
|
|
| 31 |
#' @family report Objects |
|
| 32 |
#' @aliases report_mig_char |
|
| 33 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 34 |
#' @concept report Object |
|
| 35 |
#' @example inst/examples/report_mig_char-example.R |
|
| 36 |
#' @keywords classes |
|
| 37 |
#' @export |
|
| 38 |
setClass(Class = "report_mig_char", |
|
| 39 |
representation = representation( |
|
| 40 |
echantillon = "ref_choice", |
|
| 41 |
calcdata = "list", |
|
| 42 |
parqual = "ref_parqual", |
|
| 43 |
parquan = "ref_parquan"), |
|
| 44 |
prototype = list( |
|
| 45 |
data = list(), |
|
| 46 |
echantillon = new("ref_choice", listechoice = c("with", "without"),
|
|
| 47 |
selectedvalue = "with"), |
|
| 48 |
calcdata = list(), |
|
| 49 |
parqual = new("ref_parqual"),
|
|
| 50 |
parquan = new("ref_parquan")),
|
|
| 51 |
contains = "report_sample_char") |
|
| 52 | ||
| 53 | ||
| 54 |
setValidity("report_mig_char", function(object) {
|
|
| 55 |
retValue = "" |
|
| 56 |
rep4 <- length(object@taxa) == 1 |
|
| 57 |
if (!rep4) |
|
| 58 |
retValue = gettext("This report should be for just one taxa")
|
|
| 59 |
rep5 <- length(object@parqual) == 1 | length(object@parquan) == 1 |
|
| 60 |
if (!rep5) |
|
| 61 |
retValue = gettext("length(object@parqual)==1|length(object@parquan)==1 not TRUE")
|
|
| 62 |
return(ifelse(rep4 & rep5, TRUE, retValue)) |
|
| 63 |
}) |
|
| 64 | ||
| 65 | ||
| 66 |
#' command line interface for report_mig_char class |
|
| 67 |
#' @param object An object of class \link{report_mig_char-class}
|
|
| 68 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 69 |
#' @param taxa '2220=Salmo salar', can be a vector with several values |
|
| 70 |
#' these should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 71 |
#' @param stage The stages selected, can be a vector with several values |
|
| 72 |
#' @param parquan Quantitative parameter |
|
| 73 |
#' @param parqual Qualitative parameter |
|
| 74 |
#' @param horodatedebut The starting date as a character, formats like \code{\%Y-\%m-\%d} or \code{\%d-\%m-\%Y} can be used as input
|
|
| 75 |
#' @param horodatefin The finishing date of the report, for this class this will be used to calculate the number of daily steps |
|
| 76 |
#' @param echantillon 'with' can be 'without', checking without modifies the query |
|
| 77 |
#' in the connect method so that subsamples are not allowed |
|
| 78 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 79 |
#' @return An object of class \link{report_sea_age-class}
|
|
| 80 |
#' The choice_c method fills in the data slot for classes \link{ref_dc-class}, \link{ref_taxa-class}, \link{ref_stage-class}, \link{ref_par-class} and two slots of \link{ref_horodate-class} and then
|
|
| 81 |
#' uses the choice_c methods of these object to select the data. |
|
| 82 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 83 |
#' @aliases choice_c.report_mig_char |
|
| 84 |
setMethod("choice_c", signature = signature("report_mig_char"), definition = function(object,
|
|
| 85 |
dc, taxa, stage, parquan = NULL, parqual = NULL, horodatedebut, horodatefin, |
|
| 86 |
echantillon = c("with","without"), silent = FALSE) {
|
|
| 87 | 7x |
echantillon <- match.arg(echantillon) |
| 88 |
# code for debug using example |
|
| 89 |
# horodatedebut='2012-01-01';horodatefin='2013-12-31';dc=c(107,108,101);taxa=2220;stage=c('5','11','BEC','BER','IND');parquan=c('1786','1785','C001','A124');parqual='COHO';silent=FALSE
|
|
| 90 | 7x |
r_mig_char <- object |
| 91 | 7x |
r_mig_char@dc = charge(r_mig_char@dc) |
| 92 | 7x |
r_mig_char@dc <- choice_c(object = r_mig_char@dc, dc) |
| 93 | 7x |
r_mig_char@taxa <- charge_with_filter(object = r_mig_char@taxa, r_mig_char@dc@dc_selected) |
| 94 | 7x |
r_mig_char@taxa <- choice_c(r_mig_char@taxa, taxa) |
| 95 | 7x |
r_mig_char@stage <- charge_with_filter(object = r_mig_char@stage, r_mig_char@dc@dc_selected, |
| 96 | 7x |
r_mig_char@taxa@taxa_selected) |
| 97 | 7x |
r_mig_char@stage <- choice_c(r_mig_char@stage, stage, silent = silent) |
| 98 | 7x |
r_mig_char@parquan <- charge_with_filter(object = r_mig_char@parquan, dc_selected = r_mig_char@dc@dc_selected, |
| 99 | 7x |
taxa_selected = r_mig_char@taxa@taxa_selected, stage_selected = r_mig_char@stage@stage_selected) |
| 100 | 7x |
if (!is.null(parquan)) |
| 101 | 7x |
r_mig_char@parquan <- choice_c(r_mig_char@parquan, parquan, silent = silent) |
| 102 |
# the method choice_c is written in ref_par, and each time |
|
| 103 | 7x |
assign("ref_parquan", r_mig_char@parquan, envir_stacomi)
|
| 104 | 7x |
r_mig_char@parqual <- charge_with_filter(object = r_mig_char@parqual, r_mig_char@dc@dc_selected, |
| 105 | 7x |
r_mig_char@taxa@taxa_selected, r_mig_char@stage@stage_selected) |
| 106 | 7x |
if (!is.null(parqual)) {
|
| 107 | ! |
r_mig_char@parqual <- choice_c(r_mig_char@parqual, parqual, silent = silent) |
| 108 | ! |
r_mig_char@parqual <- charge_complement(r_mig_char@parqual) |
| 109 |
} |
|
| 110 | 7x |
assign("ref_parqual", r_mig_char@parqual, envir_stacomi)
|
| 111 | 7x |
r_mig_char@horodatedebut <- choice_c(object = r_mig_char@horodatedebut, nomassign = "bmC_date_debut", |
| 112 | 7x |
funoutlabel = gettext("Beginning date has been chosen\n", domain = "R-stacomiR"),
|
| 113 | 7x |
horodate = horodatedebut, silent = silent) |
| 114 | 7x |
r_mig_char@horodatefin <- choice_c(r_mig_char@horodatefin, nomassign = "bmC_date_fin", |
| 115 | 7x |
funoutlabel = gettext("Ending date has been chosen\n", domain = "R-stacomiR"),
|
| 116 | 7x |
horodate = horodatefin, silent = silent) |
| 117 | 7x |
r_mig_char@echantillon <- charge(r_mig_char@echantillon, vecteur = c("with","without"), label = "essai",
|
| 118 | 7x |
selected = as.integer(1)) |
| 119 | 7x |
r_mig_char@echantillon <- choice_c(r_mig_char@echantillon, selectedvalue = echantillon) |
| 120 | 7x |
validObject(r_mig_char) |
| 121 | 7x |
return(r_mig_char) |
| 122 |
}) |
|
| 123 | ||
| 124 |
#' charge method for report_mig_char |
|
| 125 |
#' |
|
| 126 |
#' Used by the graphical interface to collect and test objects in the environment envir_stacomi, |
|
| 127 |
#' fills also the data slot by the connect method. It is not necessary to run the charge method |
|
| 128 |
#' if the choice is made from the command line using the choice_c method. |
|
| 129 |
#' @param object An object of class \link{report_mig_char-class}
|
|
| 130 |
#' @param silent Default FALSE, if TRUE the program should not display messages |
|
| 131 |
#' @return \link{report_mig_char-class} with slot filled from values assigned in \code{envir_stacomi} environment
|
|
| 132 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 133 |
#' @aliases charge.report_mig_char |
|
| 134 |
#' @keywords internal |
|
| 135 |
setMethod("charge", signature = signature("report_mig_char"), definition = function(object,
|
|
| 136 |
silent = FALSE) {
|
|
| 137 | 1x |
r_mig_char <- object |
| 138 | 1x |
if (exists("bmC_date_debut", envir_stacomi)) {
|
| 139 | 1x |
r_mig_char@horodatedebut@horodate <- get("bmC_date_debut", envir_stacomi)
|
| 140 |
} else {
|
|
| 141 | ! |
funout(gettext("You need to choose the starting date\n", domain = "R-stacomiR"),
|
| 142 | ! |
arret = TRUE) |
| 143 |
} |
|
| 144 | 1x |
if (exists("bmC_date_fin", envir_stacomi)) {
|
| 145 | 1x |
r_mig_char@horodatefin@horodate <- get("bmC_date_fin", envir_stacomi)
|
| 146 |
} else {
|
|
| 147 | ! |
funout(gettext("You need to choose the ending date\n", domain = "R-stacomiR"),
|
| 148 | ! |
arret = TRUE) |
| 149 |
} |
|
| 150 |
|
|
| 151 | 1x |
if (exists("ref_dc", envir_stacomi)) {
|
| 152 | 1x |
r_mig_char@dc <- get("ref_dc", envir_stacomi)
|
| 153 |
} else {
|
|
| 154 | ! |
funout(gettext("You need to choose a counting device, clic on validate\n",
|
| 155 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 156 |
} |
|
| 157 | 1x |
if (exists("ref_taxa", envir_stacomi)) {
|
| 158 | 1x |
r_mig_char@taxa <- get("ref_taxa", envir_stacomi)
|
| 159 |
} else {
|
|
| 160 | ! |
funout(gettext("You need to choose a taxa, clic on validate\n", domain = "R-stacomiR"),
|
| 161 | ! |
arret = TRUE) |
| 162 |
} |
|
| 163 | 1x |
if (exists("ref_stage", envir_stacomi)) {
|
| 164 | 1x |
r_mig_char@stage <- get("ref_stage", envir_stacomi)
|
| 165 |
} else {
|
|
| 166 | ! |
funout(gettext("You need to choose a stage, clic on validate\n", domain = "R-stacomiR"),
|
| 167 | ! |
arret = TRUE) |
| 168 |
} |
|
| 169 |
|
|
| 170 | 1x |
if (exists("refchoice", envir_stacomi)) {
|
| 171 | ! |
r_mig_char@echantillon <- get("refchoice", envir_stacomi)
|
| 172 |
} else {
|
|
| 173 | 1x |
r_mig_char@echantillon@listechoice <- gettext("with", domain = "R-stacomiR")
|
| 174 | 1x |
r_mig_char@echantillon@selected <- as.integer(1) |
| 175 |
} |
|
| 176 |
|
|
| 177 | 1x |
if (!(exists("ref_parquan", envir_stacomi) | exists("ref_parqual", envir_stacomi))) {
|
| 178 | ! |
funout(gettext("You need to choose at least one parameter qualitative or quantitative\n",
|
| 179 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 180 |
} |
|
| 181 |
|
|
| 182 | 1x |
if (exists("ref_parquan", envir_stacomi)) {
|
| 183 | 1x |
r_mig_char@parquan <- get("ref_parquan", envir_stacomi)
|
| 184 |
} |
|
| 185 | 1x |
if (exists("ref_parqual", envir_stacomi)) {
|
| 186 | 1x |
r_mig_char@parqual <- get("ref_parqual", envir_stacomi)
|
| 187 |
} |
|
| 188 |
|
|
| 189 | 1x |
stopifnot(validObject(r_mig_char, test = TRUE)) |
| 190 | 1x |
return(r_mig_char) |
| 191 |
}) |
|
| 192 | ||
| 193 |
#' connect method for report_mig_char |
|
| 194 |
#' |
|
| 195 |
#' |
|
| 196 |
#' uses the report_mig_mult method |
|
| 197 |
#' @param object An object of class \link{report_mig_char-class}
|
|
| 198 |
#' @param silent Boolean default FALSE, if TRUE information messages not displayed |
|
| 199 |
#' @return An object of class \link{report_mig_char-class} with list in \code{@data$parquan} and \code{@data$parqual} filled in from the database
|
|
| 200 |
#' @aliases connect.report_mig_char |
|
| 201 |
setMethod("connect", signature = signature("report_mig_char"), definition = function(object,
|
|
| 202 |
silent = FALSE) {
|
|
| 203 | 6x |
r_mig_char <- object |
| 204 | 6x |
if (r_mig_char@echantillon@selectedvalue == "without") {
|
| 205 | ! |
echantillons = " AND lot_pere IS NULL" |
| 206 |
} else {
|
|
| 207 | 6x |
echantillons = "" |
| 208 |
} |
|
| 209 |
# data can be selected but not in the database or the inverse |
|
| 210 | 6x |
parquan <- intersect(r_mig_char@parquan@par_selected, r_mig_char@parquan@data$par_code) |
| 211 | 6x |
parqual <- intersect(r_mig_char@parqual@par_selected, r_mig_char@parqual@data$par_code) |
| 212 | 6x |
if (length(parquan) == 0 & length(parqual) == 0) {
|
| 213 | ! |
stop("You need to choose at least one quantitative or qualitative attribute")
|
| 214 |
} else {
|
|
| 215 | 6x |
if (length(parqual) != 0) |
| 216 |
{
|
|
| 217 |
# caracteristique qualitative |
|
| 218 | ! |
req = new("RequeteDB")
|
| 219 |
# this query will get characteristics from lot_pere when null |
|
| 220 | ! |
req@sql = paste("SELECT ", " ope_date_debut,", " ope_date_fin,",
|
| 221 | ! |
" lot_methode_obtention,", " lot_identifiant ,", " lot_effectif,", |
| 222 | ! |
" car_val_identifiant,", " ope_dic_identifiant,", " lot_tax_code,", |
| 223 | ! |
" lot_std_code,", " car_par_code", " FROM ", get_schema(), "vue_ope_lot_ech_parqual", " WHERE ope_dic_identifiant in ", |
| 224 | ! |
vector_to_listsql(r_mig_char@dc@dc_selected), echantillons, |
| 225 | ! |
" AND lot_tax_code in ", vector_to_listsql(r_mig_char@taxa@taxa_selected), |
| 226 | ! |
" AND lot_std_code in ", vector_to_listsql(r_mig_char@stage@stage_selected), |
| 227 | ! |
" AND car_par_code in ", vector_to_listsql(parqual), " AND (ope_date_debut, ope_date_fin) OVERLAPS (TIMESTAMP '", |
| 228 | ! |
r_mig_char@horodatedebut@horodate, "', TIMESTAMP '", r_mig_char@horodatefin@horodate, |
| 229 | ! |
"')", sep = "") |
| 230 | ! |
r_mig_char@data[["parqual"]] <- query(req)@query |
| 231 | 6x |
} # end if (parqual) |
| 232 | 6x |
if (length(parquan) != 0) |
| 233 |
{
|
|
| 234 |
# Caracteristique quantitative |
|
| 235 | 6x |
req = new("RequeteDB")
|
| 236 |
# we round the date to be consistent with daily values from the |
|
| 237 | 6x |
req@sql = paste("SELECT ", " ope_date_debut,", " ope_date_fin,",
|
| 238 | 6x |
" lot_methode_obtention,", " lot_identifiant ,", " lot_effectif,", |
| 239 | 6x |
" car_valeur_quantitatif,", " ope_dic_identifiant,", " lot_tax_code,", |
| 240 | 6x |
" lot_std_code,", " car_par_code", " FROM ", get_schema(), |
| 241 | 6x |
"vue_ope_lot_ech_parquan", " WHERE ope_dic_identifiant in ", |
| 242 | 6x |
vector_to_listsql(r_mig_char@dc@dc_selected), echantillons, |
| 243 | 6x |
" AND lot_tax_code in ", vector_to_listsql(r_mig_char@taxa@taxa_selected), |
| 244 | 6x |
" AND lot_std_code in ", vector_to_listsql(r_mig_char@stage@stage_selected), |
| 245 | 6x |
" AND car_par_code in ", vector_to_listsql(parquan), " AND (ope_date_debut, ope_date_fin) OVERLAPS (TIMESTAMP '", |
| 246 | 6x |
r_mig_char@horodatedebut@horodate, "', TIMESTAMP '", r_mig_char@horodatefin@horodate, |
| 247 | 6x |
"')", sep = "") |
| 248 |
|
|
| 249 | 6x |
r_mig_char@data[["parquan"]] <- query(req)@query |
| 250 | 6x |
} # end if (parquan) |
| 251 | 6x |
} # end else |
| 252 | 6x |
return(r_mig_char) |
| 253 |
}) |
|
| 254 | ||
| 255 | ||
| 256 |
#' Turns a continuous parameter into discrete values |
|
| 257 |
#' |
|
| 258 |
#' The parm name becomes "parm_discrete". New values are created in the `data[["parqual"]]` slot |
|
| 259 |
#' of the report and the parqual slot is updated |
|
| 260 |
#' |
|
| 261 |
#' @param object An object of class \link{ref_parquan-class}
|
|
| 262 |
#' @param par The code of a quantitative parameter |
|
| 263 |
#' @param silent Default FALSE, if TRUE the program should not display messages |
|
| 264 |
#' @param ... Additional parms to the cut method \link[base]{cut}
|
|
| 265 |
#' @return An object of class \link{ref_parquan-class} with lines removed from \code{r@data[["parquan"]]}
|
|
| 266 |
#' and added (after transformation to qualitative values) in \code{r@data[["parqal"]]}
|
|
| 267 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 268 |
setMethod("setasqualitative", signature = signature("report_mig_char"), definition = function(object,
|
|
| 269 |
par, silent = FALSE, ...) {
|
|
| 270 | 8x |
r_mig_char <- object |
| 271 |
# par <-'A124' ========= initial checks ================ |
|
| 272 | 8x |
if (!inherits(par , "character")) |
| 273 | 8x |
stop("par should be a character")
|
| 274 | 8x |
if (nrow(r_mig_char@data[["parquan"]]) == 0) |
| 275 | 8x |
funout(gettext("No data for quantitative parameter, perhaps you forgot to run the calcule method"))
|
| 276 | 8x |
if (!par %in% r_mig_char@parquan@par_selected) |
| 277 | 8x |
funout(gettextf("The parameter %s is not in the selected parameters", par),
|
| 278 | 8x |
arret = TRUE) |
| 279 | 8x |
if (!par %in% r_mig_char@parquan@data$par_code) |
| 280 | 8x |
funout(gettextf("No data for this parameter : %s, nothing to do", par), arret = TRUE)
|
| 281 | ||
| 282 |
# r_mig_char@data[["parqual"]] in report_mig_char ----------------------------- |
|
| 283 |
|
|
| 284 | 8x |
newtabqual <- r_mig_char@data[["parquan"]] |
| 285 | 8x |
lignes_du_par <- newtabqual$car_par_code == par |
| 286 | 8x |
newtabqual <- newtabqual[lignes_du_par, ] |
| 287 | 8x |
nbnaquan <- sum(is.na(newtabqual$car_valeur_quantitatif)) |
| 288 | 8x |
newtabqual$car_valeur_quantitatif <- cut(newtabqual$car_valeur_quantitatif, ...) |
| 289 | 8x |
nbnaqual <- sum(is.na(newtabqual$car_valeur_quantitatif)) |
| 290 | ||
| 291 | 8x |
if (all(is.na(newtabqual$car_valeur_quantitatif))) stop("Only NA produced, please check the bounds")
|
| 292 | 7x |
if (nbnaqual > nbnaquan) warning(sprintf("You are producing %s NA values, maybe change your limits",nbnaqual - nbnaquan))
|
| 293 |
# newtabqual$car_valeur_quantitatif<-cut(newtabqual$car_valeur_quantitatif,breaks=c(0,1.5,2.5,10),label=c('1','2','3'))
|
|
| 294 | 7x |
newtabqual <- chnames(newtabqual, "car_valeur_quantitatif", "car_val_identifiant") |
| 295 | 7x |
newtabqual$car_par_code <- paste0(newtabqual$car_par_code,"_discrete") |
| 296 | 7x |
new_car_code <- newtabqual$car_par_code[1] # e.g "A124_qualitatif" |
| 297 | 7x |
new_car_nom <- paste0(r_mig_char@parquan@data[r_mig_char@parquan@data$par_code==par,"par_nom"], " (qual)") |
| 298 | ||
| 299 | 7x |
tabqual <- r_mig_char@data[["parqual"]] |
| 300 |
# remove first lines already processed earlier in valqual with the same parm |
|
| 301 | 7x |
if (!is.null(tabqual)){
|
| 302 | 2x |
tabqual <- tabqual[!tabqual$car_par_code %in% new_car_code,] |
| 303 |
} |
|
| 304 | 7x |
r_mig_char@data[["parqual"]] <- rbind(tabqual, newtabqual) |
| 305 |
# Adding the par to parqual |
|
| 306 |
|
|
| 307 |
# valqual slot in parqual ----------------------------- |
|
| 308 |
|
|
| 309 | 7x |
tabvalqual <- r_mig_char@parqual@valqual |
| 310 | 7x |
if (!is.null(tabvalqual)){
|
| 311 | 7x |
tabvalqual <- tabvalqual[!tabvalqual$val_qual_code %in% new_car_code,] |
| 312 |
} |
|
| 313 | 7x |
tabvalqual <- rbind( |
| 314 | 7x |
tabvalqual, |
| 315 | 7x |
data.frame(val_identifiant = levels(newtabqual$car_val_identifiant), |
| 316 | 7x |
val_qal_code = new_car_code, |
| 317 | 7x |
val_rang = 1:length(levels(newtabqual$car_val_identifiant)), |
| 318 | 7x |
val_libelle = levels(newtabqual$car_val_identifiant)) |
| 319 |
) |
|
| 320 | 7x |
r_mig_char@parqual@valqual <- tabvalqual |
| 321 |
|
|
| 322 |
# data slot in parqual ----------------------------- |
|
| 323 | ||
| 324 | 7x |
tabdata <- r_mig_char@parqual@data |
| 325 | 7x |
if (!is.null(tabdata)){
|
| 326 | 7x |
tabdata <- tabdata[!tabdata$par_code %in% new_car_code,] |
| 327 |
} |
|
| 328 | 7x |
tabdata <- rbind(tabdata, |
| 329 | 7x |
c("par_code"=new_car_code, "par_nom"=new_car_nom,"par_unite"=NA, "par_nature"=NA,"par_definition"=NA,"qual_valeurs_possibles"=NA)
|
| 330 |
) |
|
| 331 | 7x |
colnames(tabdata) <- c("par_code", "par_nom", "par_unite", "par_nature", "par_definition", "qal_valeurs_possibles")
|
| 332 | 7x |
r_mig_char@parqual@data <-tabdata |
| 333 |
|
|
| 334 |
# selected parm in parqual ----------------------------- |
|
| 335 | ||
| 336 | 7x |
r_mig_char@parqual@par_selected <- unique(c(r_mig_char@parqual@par_selected, new_car_code)) |
| 337 |
|
|
| 338 | 7x |
if (!silent) |
| 339 | 7x |
funout(gettextf("%s lines have been converted from quantitative to qualitative parameters",
|
| 340 | 7x |
nrow(newtabqual))) |
| 341 | 7x |
return(r_mig_char) |
| 342 |
}) |
|
| 343 | ||
| 344 | ||
| 345 |
# TODO create a dataframe with only one line per fish for all parameters |
|
| 346 |
#' Computes data to a standard format for the summary and plot methods. |
|
| 347 |
#' |
|
| 348 |
#' Merges the content of the list elements 'parqual' and 'parquan' in the data slot, and creates |
|
| 349 |
#' a single dataframe with one line per qualitative and quantitative pair. This methods allow to |
|
| 350 |
#' cross one quantity (e.g. length) with a qualitative parameter (e.g. sex). |
|
| 351 |
#' |
|
| 352 |
#' @param object An object of class \link{report_mig_char-class}
|
|
| 353 |
#' @param silent Boolean default FALSE, if TRUE information messages not displayed |
|
| 354 |
#' @return An object of class \link{report_mig_char-class} with slot \code{@calcdata} filled
|
|
| 355 |
#' @aliases calcule.report_mig_char |
|
| 356 |
setMethod("calcule", signature = signature("report_mig_char"), definition = function(object,
|
|
| 357 |
silent = FALSE) {
|
|
| 358 | 9x |
r_mig_char <- object |
| 359 | 9x |
qual <- r_mig_char@data[["parqual"]] |
| 360 | 9x |
quan <- r_mig_char@data[["parquan"]] |
| 361 | 9x |
if (is.null(qual) & is.null(quan)) |
| 362 | 9x |
stop("cannot perform calcule method, no data in either qualitative or quantitative parameters")
|
| 363 | 9x |
if (!is.null(qual)) |
| 364 | 9x |
qual <- chnames(qual, "car_par_code", "car_par_code_qual") |
| 365 | 9x |
if (!is.null(quan)) |
| 366 | 9x |
quan <- chnames(quan, "car_par_code", "car_par_code_quan") |
| 367 | 9x |
if (is.null(qual)) {
|
| 368 | 4x |
quaa <- quan |
| 369 | 4x |
quaa$car_par_code_qual = NA |
| 370 | 9x |
} else if (is.null(quan)) {
|
| 371 | ! |
quaa <- qual |
| 372 | ! |
quaa$car_par_code_quan = NA |
| 373 |
} else {
|
|
| 374 | 5x |
quaa <- merge(qual, quan, by = c("ope_dic_identifiant", "lot_identifiant",
|
| 375 | 5x |
"ope_date_debut", "ope_date_fin", "lot_methode_obtention", "lot_effectif", |
| 376 | 5x |
"lot_tax_code", "lot_std_code"), all.x = TRUE, all.y = TRUE) |
| 377 |
} |
|
| 378 | 9x |
quaa = fun_date_extraction(data = quaa, nom_coldt = "ope_date_debut") |
| 379 | 9x |
quaa <- quaa[order(quaa$ope_dic_identifiant, quaa$lot_tax_code, quaa$lot_std_code, |
| 380 | 9x |
quaa$ope_date_debut), ] |
| 381 | 9x |
r_mig_char@calcdata <- quaa |
| 382 | 9x |
if (!silent) |
| 383 | 9x |
funout(gettext("The calculated data are in slot calcdata"))
|
| 384 | 9x |
assign("r_mig_char", r_mig_char, envir_stacomi)
|
| 385 | 9x |
return(r_mig_char) |
| 386 |
}) |
|
| 387 | ||
| 388 | ||
| 389 |
#' plot method for report_mig_char |
|
| 390 |
#' |
|
| 391 |
#' |
|
| 392 |
#' @param x An object of class report_mig_char |
|
| 393 |
#' @param plot.type One of 'qual', 'quant' 'crossed' default to qual |
|
| 394 |
#' @param color_parm A named vector for the colors of either parameters (if plot.type=quant) or levels for |
|
| 395 |
#' parameters (if plot.type=qual). |
|
| 396 |
#' @param silent Boolean default FALSE, if TRUE information messages not displayed |
|
| 397 |
#' @param ... Additional parameters |
|
| 398 |
#' @return Nothing, called for its side effect of plotting data |
|
| 399 |
#' @aliases plot.report_mig_char |
|
| 400 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 401 |
#' @export |
|
| 402 |
setMethod("plot", signature = signature(x = "report_mig_char", y = "missing"),
|
|
| 403 |
definition = function(x, |
|
| 404 |
color_parm = NULL, plot.type = "qual", silent = FALSE, ...) {
|
|
| 405 | 10x |
r_mig_char <- x |
| 406 | 10x |
if (nrow(r_mig_char@calcdata) == 0) |
| 407 | 10x |
stop("no data in calcdata, have you forgotten to run calculations ?")
|
| 408 |
# transformation du tableau de donnees color_parm<-c('age 1'='red','age
|
|
| 409 |
# 2'='blue','age 3'='green') color_parm<-c('C001'='red')
|
|
| 410 | 9x |
if (plot.type == "qual") |
| 411 |
{
|
|
| 412 | 1x |
parlevels <- r_mig_char@parqual@valqual$val_identifiant |
| 413 | 1x |
if (nrow(r_mig_char@parqual@valqual)==0) stop("No data loaded in qualitative parameters")
|
| 414 | 1x |
cs <- colortable(color = color_parm, vec = parlevels, palette = "Dark2") |
| 415 | 1x |
cs <- stacomirtools::chnames(cs, "name", "car_val_identifiant") |
| 416 | 1x |
calcdata <- r_mig_char@calcdata |
| 417 | 1x |
calcdata <- merge(calcdata, cs) |
| 418 | 1x |
g <- ggplot(calcdata) + geom_bar(aes(x = mois, y = lot_effectif, fill = color), |
| 419 | 1x |
stat = "identity") + xlab(gettext("Month")) + ylab(gettext("Number")) +
|
| 420 | 1x |
scale_fill_identity(name = gettext("Classes"), labels = cs[, "car_val_identifiant"],
|
| 421 | 1x |
breaks = cs[, "color"], guide = "legend") + theme_bw() |
| 422 |
|
|
| 423 | 1x |
assign("g", g, envir_stacomi)
|
| 424 | 1x |
if (!silent) |
| 425 | 1x |
funout(gettext("Writing the graphical object into envir_stacomi environment : write g=get(\"g\",envir_stacomi) \n",
|
| 426 | 1x |
domain = "R-stacomiR")) |
| 427 | 1x |
print(g) |
| 428 | 9x |
} #end plot.type = 'qual' |
| 429 | 9x |
if (plot.type == "quant") |
| 430 |
{
|
|
| 431 | 4x |
calcdata <- r_mig_char@calcdata |
| 432 | 4x |
calcdata$car_par_code_quan[is.na(calcdata$car_par_code_quan)] <- "NA" |
| 433 | 4x |
the_parms <- unique(calcdata$car_par_code_quan) |
| 434 | 4x |
cs <- colortable(color = color_parm, vec = the_parms, palette = "Dark2") |
| 435 | 4x |
cs <- stacomirtools::chnames(cs, "name", "car_par_code_quan") |
| 436 | 4x |
calcdata <- merge(calcdata, cs) |
| 437 | 4x |
g <- ggplot(calcdata) + geom_point(aes(x = ope_date_debut, y = car_valeur_quantitatif, |
| 438 | 4x |
col = color), stat = "identity") + xlab(gettext("Month")) + ylab(gettext("Quantitative parameter")) +
|
| 439 | 4x |
scale_colour_identity(name = gettext("Param"), labels = cs[, "car_par_code_quan"],
|
| 440 | 4x |
breaks = cs[, "color"], guide = "legend") + theme_bw() |
| 441 | 4x |
assign("g", g, envir_stacomi)
|
| 442 | 4x |
if (!silent) |
| 443 | 4x |
funout(gettext("Writing the graphical object into envir_stacomi environment : write g=get(\"g\",envir_stacomi) \n",
|
| 444 | 4x |
domain = "R-stacomiR")) |
| 445 | 4x |
print(g) |
| 446 | 9x |
} #end plot.type='quant' |
| 447 | 9x |
if (plot.type == "crossed") |
| 448 |
{
|
|
| 449 | 4x |
parlevels <- r_mig_char@parqual@valqual$val_identifiant |
| 450 |
|
|
| 451 | 4x |
cs <- colortable(color = color_parm, vec = parlevels, palette = "Dark2") |
| 452 | 4x |
cs <- stacomirtools::chnames(cs, "name", "car_val_identifiant") |
| 453 | 4x |
calcdata <- r_mig_char@calcdata |
| 454 |
#calcdata$car_val_identifiant |
|
| 455 | 4x |
calcdata <- merge(calcdata, cs) |
| 456 | 4x |
if (length(unique(calcdata$car_par_code_quan))==1){
|
| 457 | 3x |
label <- paste( |
| 458 | 3x |
r_mig_char@parquan@data[r_mig_char@parquan@par_selected ==r_mig_char@parquan@data$par_code,"par_nom"], |
| 459 |
" (",
|
|
| 460 | 3x |
r_mig_char@parquan@data[r_mig_char@parquan@par_selected ==r_mig_char@parquan@data$par_code,"par_unite"], |
| 461 | 3x |
")", sep="") |
| 462 | 3x |
g <- ggplot(calcdata) + geom_point(aes(x = ope_date_debut, y = car_valeur_quantitatif, |
| 463 | 3x |
col = color), stat = "identity") + xlab(gettext("Month")) + ylab(label) +
|
| 464 | 3x |
scale_colour_identity(name = gettext("Param"), labels = cs[, "car_val_identifiant"],
|
| 465 | 3x |
breaks = cs[, "color"], guide = "legend") + theme_bw() |
| 466 |
} else {
|
|
| 467 | 1x |
g <- ggplot(calcdata) + geom_point(aes(x = ope_date_debut, y = car_valeur_quantitatif, |
| 468 | 1x |
col = color), stat = "identity") + xlab(gettext("Month")) + ylab(gettext("Quantitative parameter")) +
|
| 469 | 1x |
scale_colour_identity(name = gettext("Param"), labels = cs[, "car_val_identifiant"],
|
| 470 | 1x |
breaks = cs[, "color"], guide = "legend") + |
| 471 | 1x |
facet_wrap(~car_par_code_quan, scales="free_y") + |
| 472 | 1x |
theme_bw() |
| 473 |
} |
|
| 474 | 4x |
assign("g", g, envir_stacomi)
|
| 475 | 4x |
if (!silent) |
| 476 | 4x |
funout(gettext("Writing the graphical object into envir_stacomi environment : write g=get(\"g\",envir_stacomi) \n",
|
| 477 | 4x |
domain = "R-stacomiR")) |
| 478 | 4x |
print(g) |
| 479 | 9x |
} #end plot.type='xyplot' |
| 480 | 9x |
return(invisible(NULL)) |
| 481 |
}) |
|
| 482 | ||
| 483 | ||
| 484 |
#' summary for report_mig_char |
|
| 485 |
#' @param object An object of class \code{\link{report_mig_char-class}}
|
|
| 486 |
#' @param silent Should the program stay silent or display messages, default FALSE |
|
| 487 |
#' @param ... Additional parameters |
|
| 488 |
#' @return A table with the summary |
|
| 489 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 490 |
#' @aliases summary.report_mig_char |
|
| 491 |
#' @export |
|
| 492 |
setMethod("summary", signature = signature(object = "report_mig_char"), definition = function(object,
|
|
| 493 |
silent = FALSE, ...) {
|
|
| 494 | 5x |
r_mig_char <- object |
| 495 | 5x |
bm <- r_mig_char@calcdata |
| 496 | 5x |
if (nrow(bm) == 0) |
| 497 | 5x |
stop("No data in slot calcdata, did you forget to run the calcule method ?")
|
| 498 | 5x |
if (length(unique(bm$annee)) == 1) {
|
| 499 | 3x |
table = round(tapply(bm$lot_effectif, list(bm$mois, bm$car_val_identifiant), |
| 500 | 3x |
sum), 1) |
| 501 | 3x |
table <- rbind(table, colSums(table, na.rm = TRUE)) |
| 502 | 3x |
rownames(table)[nrow(table)] <- gettext("Sum")
|
| 503 | 3x |
table <- as.data.frame(table) |
| 504 |
} else {
|
|
| 505 | 2x |
table = round(tapply(bm$lot_effectif, list(bm$annee, bm$mois, bm$car_val_identifiant), |
| 506 | 2x |
sum), 1) |
| 507 | 2x |
ftable2data.frame <- function(x, ...) {
|
| 508 | 2x |
y <- format(x, quote = FALSE) |
| 509 | 2x |
z <- data.frame(y[-1, ], stringsAsFactors = FALSE) |
| 510 | 2x |
names(z) <- y[1, ] |
| 511 | 2x |
z |
| 512 |
} |
|
| 513 | 2x |
table <- ftable2data.frame(ftable(table)) |
| 514 |
} |
|
| 515 | 5x |
return(table) |
| 516 |
}) |
|
| 517 | ||
| 518 | ||
| 519 |
#' xtable function for \link{report_mig_char-class}
|
|
| 520 |
#' create an xtable objet to be later used by the print.xtable method. |
|
| 521 |
#' @param x, an object of class 'report_mig_char' |
|
| 522 |
#' @param caption, see xtable |
|
| 523 |
#' @param label, see xtable |
|
| 524 |
#' @param align, see xtable, overidden if NULL |
|
| 525 |
#' @param digits, see xtable |
|
| 526 |
#' @param display see xtable |
|
| 527 |
#' @param auto see xtable |
|
| 528 |
#' @param ... Additional parameters |
|
| 529 |
#' @return A xtable |
|
| 530 |
#' @aliases xtable.report_mig_char |
|
| 531 |
#' @export |
|
| 532 |
setMethod("xtable", signature = signature("report_mig_char"), definition = function(x,
|
|
| 533 |
caption = NULL, label = NULL, align = NULL, ...) {
|
|
| 534 | 3x |
r_mig_char <- x |
| 535 | 3x |
dat = r_mig_char@data |
| 536 | 3x |
dc = stringr::str_c(r_mig_char@dc@dc_selected, collapse = " ") |
| 537 | 3x |
tax = stringr::str_c(r_mig_char@taxa@taxa_selected, collapse = " ") |
| 538 | 3x |
std = stringr::str_c(r_mig_char@stage@stage_selected, collapse = " ") |
| 539 |
|
|
| 540 | 3x |
dat <- summary(r_mig_char, silent = TRUE) |
| 541 |
|
|
| 542 | 3x |
xt <- xtable::xtable(dat, ...) |
| 543 | 3x |
if (is.null(align)) {
|
| 544 | 3x |
align <- c("l", rep("r", ncol(dat)))
|
| 545 | 3x |
align(xt) <- align |
| 546 |
} |
|
| 547 | 3x |
if (is.null(display)) {
|
| 548 | 3x |
display = c("s", rep("f", ncol(dat)))
|
| 549 | 3x |
display(xt) <- display |
| 550 |
} |
|
| 551 | 3x |
if (is.null(caption)) {
|
| 552 | 3x |
caption = gettextf("Summary for dc %s, taxa %s, stage %s.", dc, tax,
|
| 553 | 3x |
std) |
| 554 | 3x |
caption(xt) <- caption |
| 555 |
} |
|
| 556 | 3x |
return(xt) |
| 557 |
}) |
|
| 558 |
| 1 |
#' Trend of wet weight in glass eel |
|
| 2 |
#' |
|
| 3 |
#' In trapping ladders, glass eel are seldom counted, as they are too tiny to handle and too numerous to count. |
|
| 4 |
#' The usual operation is to weight them, or to use a bucket to measure their volume. These weights or volumes will later |
|
| 5 |
#' need to be converted to numbers. The glass eel weight may follow a seasonal pattern. It's the case for Anguilla anguilla |
|
| 6 |
#' glass eel in the Bay of Biscay. Weights can be modelled using sine wave curves, or more complex gam models. |
|
| 7 |
#' This class has a model method to try those models, which can also be used to extact coefficients manually |
|
| 8 |
#' to manually test more complex models. |
|
| 9 |
#' Some plots are provided to display the coefficients stored in the database, and the model results. A parameter provided in |
|
| 10 |
#' the graphical interface or in the command line (slot liste) takes values '1', '>1', 'tous' which mean respectively use |
|
| 11 |
#' only individual sample of glass eels, or use 'group weights' which can be more close to the real weight of glass eel |
|
| 12 |
#' during counts as glass eel are not completely drained from their water during handling to preserve their mucus. The list choice |
|
| 13 |
#' 'tous' means that both individual and group weights are selected. |
|
| 14 |
#' @include ref_coe.R |
|
| 15 |
#' @note In this class some tools are available to import glass eel measurement from |
|
| 16 |
#' experimental fishing in the estuary. For the charge method dates for the |
|
| 17 |
#' request are from august to august (a glass eel season) |
|
| 18 |
#' @slot data A \code{'data.frame'} data for report lot
|
|
| 19 |
#' @slot calcdata A list containing two processed data frames, data and coe |
|
| 20 |
#' @slot dc Object of class \code{\link{ref_dc-class}}, the counting device
|
|
| 21 |
#' @slot start_year Object of class \code{\link{ref_year-class}}. ref_year allows to choose the year of beginning
|
|
| 22 |
#' @slot end_year Object of class \code{\link{ref_year-class}}
|
|
| 23 |
#' ref_year allows to choose last year of the report |
|
| 24 |
#' @slot coe Object of class \code{\link{ref_coe-class}} class loading coefficient
|
|
| 25 |
#' of conversion between quantity (weights or volumes of glass eel) and numbers |
|
| 26 |
#' @slot liste Object of class \code{\link{ref_list-class}} ref_list referential
|
|
| 27 |
#' class choose within a list, here the choice is whether subsamples or not. Subsamples |
|
| 28 |
#' in the stacomi database are samples with a non null value for parent sample. Migration |
|
| 29 |
#' counts are never made on subsamples but those can be integrated to calculate mean weights. |
|
| 30 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 31 |
#' @family report Objects |
|
| 32 |
#' @keywords classes |
|
| 33 |
#' @example inst/examples/report_ge_weight-example.R |
|
| 34 |
#' @aliases report_ge_weight |
|
| 35 |
#' @export |
|
| 36 |
setClass(Class = "report_ge_weight", representation = representation(data = "data.frame", |
|
| 37 |
calcdata = "list", dc = "ref_dc", start_year = "ref_year", end_year = "ref_year", |
|
| 38 |
coe = "ref_coe", liste = "ref_list"), prototype = prototype(data = data.frame(), |
|
| 39 |
calcdata = list(), dc = new("ref_dc"), start_year = new("ref_year"), end_year = new("ref_year"),
|
|
| 40 |
coe = new("ref_coe"), liste = new("ref_list")))
|
|
| 41 | ||
| 42 |
#' connect method for report_Poids_moyen |
|
| 43 |
#' |
|
| 44 |
#' The connect method adapts queries according to user choices, mean weight |
|
| 45 |
#' w is calculated as car_valeur_quantitatif/lot_effectif. These coefficients are stored in the database, |
|
| 46 |
#' and the connect method loads them from the table using the \link{ref_coe-class}
|
|
| 47 |
#' @param object An object of class \link{report_ge_weight-class}
|
|
| 48 |
#' @param silent Should the method be silent |
|
| 49 |
#' @return An object of class \link{report_ge_weight-class} with slots data and coe filled from the database
|
|
| 50 |
#' @note dates for the request are from august to august (a glass eel season) |
|
| 51 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 52 |
#' @aliases connect.report_ge_weight |
|
| 53 |
setMethod("connect", signature = signature("report_ge_weight"), definition = function(object, silent=TRUE) {
|
|
| 54 |
# object<-r_gew loading mean weights |
|
| 55 | 3x |
requete = new("RequeteDBwheredate")
|
| 56 | 3x |
requete@datedebut = strptime(paste(object@start_year@year_selected, "-08-01", |
| 57 | 3x |
sep = ""), format = "%Y-%m-%d") |
| 58 | 3x |
requete@datefin = strptime(paste(object@end_year@year_selected, "-08-01", |
| 59 | 3x |
sep = ""), format = "%Y-%m-%d") |
| 60 | 3x |
requete@colonnedebut = "ope_date_debut" |
| 61 | 3x |
requete@colonnefin = "ope_date_fin" |
| 62 | 3x |
requete@select = paste("SELECT lot_identifiant,ope_date_debut,ope_date_fin,lot_effectif,car_valeur_quantitatif as poids,",
|
| 63 | 3x |
" (car_valeur_quantitatif/lot_effectif) AS w,", " (ope_date_fin-ope_date_debut)/2 AS duree,", |
| 64 | 3x |
" ope_date_debut+(ope_date_fin-ope_date_debut)/2 as datemoy,", " date_part('year', ope_date_debut) as annee,",
|
| 65 | 3x |
" date_part('month',ope_date_debut) as mois", " FROM ", get_schema(), "vue_lot_ope_car_qan", sep = "")
|
| 66 | 3x |
requete@and = paste(" AND ope_dic_identifiant=", object@dc@dc_selected, " AND std_libelle='civelle'",
|
| 67 | 3x |
ifelse(object@liste@selectedvalue == "tous", "", paste(" AND lot_effectif",
|
| 68 | 3x |
object@liste@selectedvalue)), " AND upper(car_methode_obtention::text) = 'MESURE'::text", |
| 69 | 3x |
" AND car_par_code='A111'", sep = "") |
| 70 | 3x |
requete <- stacomirtools::query(requete) |
| 71 | 3x |
object@data <- requete@query |
| 72 |
# loading conversion coefficients |
|
| 73 | 3x |
object@coe@datedebut = requete@datedebut |
| 74 | 3x |
object@coe@datefin = requete@datefin |
| 75 | 3x |
object@coe <- charge(object@coe) |
| 76 | 3x |
if (!silent){
|
| 77 | ! |
funout(gettext("The query to load the coefficients of conversion is finished\n",
|
| 78 | ! |
domain = "R-stacomiR")) |
| 79 | ! |
funout(gettextf("%1.0f lines found for the conversion coefficients\n", nrow(object@coe),
|
| 80 | ! |
domain = "R-stacomiR")) |
| 81 |
} |
|
| 82 | 3x |
assign(x = "report_ge_weight", value = object, envir = envir_stacomi) |
| 83 | 3x |
return(object) |
| 84 |
}) |
|
| 85 | ||
| 86 | ||
| 87 |
#' command line interface for \link{report_ge_weight-class}
|
|
| 88 |
#' @param object An object of class \link{report_ge_weight-class}
|
|
| 89 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 90 |
#' @param start_year The starting the first year, passed as character or integer |
|
| 91 |
#' @param end_year the finishing year, must be > start_year (minimum one year in august to the next in august) |
|
| 92 |
#' @param selectedvalue A character to select and object in the \link{ref_list-class}
|
|
| 93 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 94 |
#' @return An object of class \link{report_ge_weight-class} with data selected
|
|
| 95 |
#' The choice_c method fills in the data slot for classes \link{ref_dc-class} \link{ref_year-class}
|
|
| 96 |
#' \link{ref_coe-class} \link{ref_list-class}
|
|
| 97 |
#' @aliases choice_c.report_ge_weight |
|
| 98 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 99 |
setMethod("choice_c", signature = signature("report_ge_weight"), definition = function(object,
|
|
| 100 |
dc, start_year, end_year, selectedvalue, silent = FALSE) {
|
|
| 101 |
# code for debug using example |
|
| 102 |
# dc=c(5,6);start_year='2015';end_year='2016';selectedvalue='>1';silent=FALSE |
|
| 103 | 4x |
if (length(selectedvalue) != 1) |
| 104 | 4x |
stop("selectedvalue must be of length one")
|
| 105 | 4x |
r_gew <- object |
| 106 | 4x |
stopifnot(end_year > start_year) |
| 107 | 4x |
r_gew@dc = charge(r_gew@dc) |
| 108 |
# loads and verifies the dc this will set dc_selected slot |
|
| 109 | 4x |
r_gew@dc <- choice_c(object = r_gew@dc, dc) |
| 110 |
# only taxa present in the report_mig are use |
|
| 111 | 4x |
r_gew@start_year <- charge(object = r_gew@start_year, objectreport = "report_ge_weight") |
| 112 | 4x |
r_gew@start_year <- choice_c(object = r_gew@start_year, nomassign = "start_year", |
| 113 | 4x |
annee = start_year, silent = silent) |
| 114 | 4x |
r_gew@end_year@data <- r_gew@start_year@data |
| 115 | 4x |
r_gew@end_year <- choice_c(object = r_gew@end_year, nomassign = "end_year", annee = end_year, |
| 116 | 4x |
silent = silent) |
| 117 | 4x |
r_gew@liste = charge(object = r_gew@liste, listechoice = c("=1", ">1", "tous"),
|
| 118 | 4x |
label = gettext("choice of number in sample (one, several,all)", domain = "R-stacomiR")) # choix de la categorie d'effectif)
|
| 119 | 4x |
r_gew@liste <- choice_c(r_gew@liste, selectedvalue = selectedvalue) |
| 120 | 4x |
assign("report_ge_weight", r_gew, envir = envir_stacomi)
|
| 121 | 4x |
return(r_gew) |
| 122 |
}) |
|
| 123 | ||
| 124 | ||
| 125 | ||
| 126 | ||
| 127 |
#' Calcule method for report_ge_weight |
|
| 128 |
#' @param object An object of class \link{report_ge_weight-class}
|
|
| 129 |
#' @param silent Boolean, if TRUE, information messages are not displayed, only warnings and errors |
|
| 130 |
#' @return An object of class \link{report_ge_weight-class} with \code{@calcdata[["data"]]} (essentially a selection of
|
|
| 131 |
#' columns and renaming from \code{@data}) and \code{coe} daily coefficients extracted from the database
|
|
| 132 |
#' \code{@calcdata[["coe"]]} and prepared for graphs
|
|
| 133 |
#' @aliases calcule.report_ge_weight |
|
| 134 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 135 |
setMethod("calcule", signature = signature("report_ge_weight"), definition = function(object,
|
|
| 136 |
silent = FALSE) {
|
|
| 137 | 2x |
r_gew <- object |
| 138 | 2x |
donnees <- r_gew@data |
| 139 | 2x |
coeff <- r_gew@coe@data |
| 140 | 2x |
coeff$w <- 1/coeff$coe_valeur_coefficient |
| 141 | 2x |
coeff$date <- as.POSIXct(coeff$coe_date_debut) |
| 142 | 2x |
if (!silent) |
| 143 | 2x |
funout(gettext("To obtain the table, type : report_ge_weight=get('report_ge_weight',envir_stacomi)@data\n",
|
| 144 | 2x |
domain = "R-stacomiR")) |
| 145 |
# changement des noms |
|
| 146 | 2x |
donnees <- stacomirtools::chnames(donnees, c("lot_identifiant", "ope_date_debut",
|
| 147 | 2x |
"ope_date_fin", "lot_effectif", "poids", "w", "duree", "datemoy"), c("lot",
|
| 148 | 2x |
"date", "date_fin", "effectif", "poids", "w", "time.sequence", "date")) |
| 149 |
# correction de manques d'effectifs dans la base |
|
| 150 | 2x |
if (sum(is.na(donnees$effectif)) > 0) |
| 151 | 2x |
warnings(gettextf("size is missing, lots %s", paste(unique(donnees$lot[is.na(donnees$effectif)]),
|
| 152 | 2x |
collapse = " "), domain = "R-stacomiR")) |
| 153 | 2x |
r_gew@calcdata[["data"]] <- donnees[, c(8, 6, 4, 1)] |
| 154 | 2x |
r_gew@calcdata[["coe"]] <- coeff[order(coeff$date), c(10, 9)] |
| 155 | 2x |
assign("report_ge_weight", r_gew, envir = envir_stacomi)
|
| 156 | 2x |
return(r_gew) |
| 157 |
}) |
|
| 158 | ||
| 159 | ||
| 160 |
#' Plot method for report_ge_weight' |
|
| 161 |
#' @note the model method provides plots for the fitted models |
|
| 162 |
#' @param x An object of class \link{report_ge_weight-class}
|
|
| 163 |
#' @param plot.type Default '1'. '1' plot of mean weight of glass eel against the mean date of operation (halfway between start, |
|
| 164 |
#' and end of operation). The ggplot 'p' can be accessed from envir_stacomi using \code{get('p',envir_stacomi)}. '2' standard plot of current coefficent.
|
|
| 165 |
#' '3' same as '1' but with size according to number. |
|
| 166 |
#' @param silent Stops displaying the messages |
|
| 167 |
#' @return Nothing, called for its side effect of plotting data |
|
| 168 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 169 |
#' @aliases plot.report_ge_weight |
|
| 170 |
#' @export |
|
| 171 |
setMethod("plot", signature(x = "report_ge_weight", y = "missing"), definition = function(x,
|
|
| 172 |
plot.type = 1, silent = FALSE) {
|
|
| 173 |
# plot.type='1';silent=FALSE r_gew=get('report_ge_weight',envir_stacomi)
|
|
| 174 | 6x |
r_gew <- x |
| 175 | 6x |
don <- r_gew@calcdata$data |
| 176 | 6x |
coe <- r_gew@calcdata$coe |
| 177 |
####################' |
|
| 178 |
# ggplot |
|
| 179 | 6x |
if (plot.type == 1) {
|
| 180 | 2x |
p <- ggplot2::qplot(x = date, y = w, data = don) |
| 181 | 2x |
print(p) |
| 182 | 2x |
assign("p", p, envir = envir_stacomi)
|
| 183 | 2x |
if (!silent) |
| 184 | 2x |
funout(gettext("ggplot object p assigned to envir_stacomi", domain = "R-stacomiR"))
|
| 185 |
####################' |
|
| 186 |
# standard plot |
|
| 187 | 6x |
} else if (plot.type == 2) {
|
| 188 | 2x |
if (length(r_gew@liste@selectedvalue) == 0) |
| 189 | 2x |
stop("Internal error, the value has not been selected before launching plot")
|
| 190 | 2x |
type_poids = switch(r_gew@liste@selectedvalue, `>1` = gettext("wet weights",
|
| 191 | 2x |
domain = "R-stacomiR"), `=1` = gettext("dry weights", domain = "R-stacomiR"),
|
| 192 | 2x |
tous = gettext("wet and dry weights", domain = "R-stacomiR"))
|
| 193 | 2x |
plot(x = don$date, y = don$w, xlab = gettext("date", domain = "R-stacomiR"),
|
| 194 | 2x |
ylab = gettext("mean weights", domain = "R-stacomiR"), col = "red", main = gettextf("Seasonal trend of %s, from %s to %s",
|
| 195 | 2x |
type_poids, r_gew@start_year@year_selected, r_gew@end_year@year_selected, |
| 196 | 2x |
domain = "R-stacomiR"), sub = "Trend of wet weights") |
| 197 | 2x |
coe <- coe[order(coe$date), ] |
| 198 | 2x |
points(coe$date, coe$w, type = "l", col = "black", lty = 2) |
| 199 |
# legend('topright',c('Obs.', 'Coeff base'),
|
|
| 200 |
# col=c('black','cyan'),pch='o',cex = 0.8)
|
|
| 201 |
|
|
| 202 |
####################' |
|
| 203 |
# geom_point + size |
|
| 204 | 6x |
} else if (plot.type == 3) {
|
| 205 | 2x |
p <- ggplot2::qplot(x = date, y = w, data = don) |
| 206 | 2x |
print(p + aes(size = effectif)) |
| 207 | 2x |
assign("p", p, envir = envir_stacomi)
|
| 208 | 2x |
if (!silent) |
| 209 | 2x |
funout(gettext("object p assigned to envir_stacomi", domain = "R-stacomiR"))
|
| 210 |
} |
|
| 211 | 6x |
return(invisible(NULL)) |
| 212 |
}) |
|
| 213 | ||
| 214 | ||
| 215 |
#' model method for report_ge_weight' |
|
| 216 |
#' this method uses samples collected over the season to model the variation in weight of |
|
| 217 |
#' glass eel or yellow eels. |
|
| 218 |
#' @param object An object of class \link{report_ge_weight-class}
|
|
| 219 |
#' @param model.type default 'seasonal', 'seasonal1','seasonal2','manual'. |
|
| 220 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 221 |
#' @return An object of class \link{report_ge_weight-class} with \code{@calcdata[["import_coe"]]} filled.
|
|
| 222 |
#' @details |
|
| 223 |
#' Depending on model.type several models are produced |
|
| 224 |
#' \itemize{
|
|
| 225 |
#'\item{model.type='seasonal'.}{ The simplest model uses a seasonal variation, it is
|
|
| 226 |
#' fitted with a sine wave curve allowing a cyclic variation |
|
| 227 |
#' w ~ a*cos(2*pi*(d'-T)/365)+b with a period T. The modified day d' used is this model is set |
|
| 228 |
#' at 1 the 1st of august doy = d' + d0; d0 = 212, doy=julian days} |
|
| 229 |
#'\item{model.type='seasonal1'.}{ A time component is introduced in the model, which allows
|
|
| 230 |
#' for a long term variation along with the seasonal variation. This long term variation is |
|
| 231 |
#' is fitted with a gam, the time variable is set at zero at the beginning of the first day of observed values. |
|
| 232 |
#' The seasonal variation is modeled on the same modified julian time as model.type='seasonal' |
|
| 233 |
#' but here we use a cyclic cubic spline cc, which allows to return at the value of d0=0 at d=365. |
|
| 234 |
#' This model was considered as the best to model size variations by Diaz & Briand in prep. but using a large set of values |
|
| 235 |
#' over years.} |
|
| 236 |
#'\item{model.type='seasonal2'.}{The seasonal trend in the previous model is now modelled with a sine
|
|
| 237 |
#' curve similar to the sine curve used in seasonal. The formula for this is \eqn{sin(\omega vt) + cos(\omega vt)}{sin(omega vt) + cos(omega vt)},
|
|
| 238 |
#' where vt is the time index variable \eqn{\omega}{omega} is a constant that describes how the index variable relates to the full period
|
|
| 239 |
#' (here, \eqn{2\pi/365=0.0172}{2pi/365=0.0172}). The model is written as following \eqn{w~cos(0.0172*doy)+sin(0.0172*doy)+s(time).}}
|
|
| 240 |
#'\item{model.type='manual'.}{ The dataset don (the raw data), coe (the coefficients already present in the
|
|
| 241 |
#' database, and newcoe the dataset to make the predictions from, are written to the environment envir_stacomi. |
|
| 242 |
#' please see example for further description on how to fit your own model, build the table of coefficients, |
|
| 243 |
#' and write it to the database.} |
|
| 244 |
#' } |
|
| 245 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 246 |
#' @aliases model.report_ge_weight |
|
| 247 |
setMethod("model", signature(object = "report_ge_weight"), definition = function(object,
|
|
| 248 |
model.type = "seasonal", silent = FALSE) {
|
|
| 249 |
# r_gew=get('report_ge_weight',envir_stacomi);silent=TRUE;require(ggplot2)
|
|
| 250 |
# r_gew <- bilPM |
|
| 251 | 3x |
r_gew <- object |
| 252 | 3x |
don <- r_gew@calcdata$data |
| 253 | 3x |
coe <- r_gew@calcdata$coe |
| 254 | 3x |
seq = seq(as.Date(r_gew@coe@datedebut), as.Date(r_gew@coe@datefin), by = "day") |
| 255 | 3x |
origine <- as.POSIXct(trunc(min(don$date), "day")) |
| 256 |
# season starting in november |
|
| 257 | 3x |
fndate <- function(data) {
|
| 258 | 6x |
if (!"date" %in% colnames(data)) |
| 259 | 6x |
stop("date should be in colnames(data)")
|
| 260 | 6x |
if (!inherits(data$date[1], "POSIXct")) |
| 261 | 6x |
stop("date should be POSIXct")
|
| 262 | 6x |
data$year <- lubridate::year(data$date) |
| 263 |
# lubridate::yday(lubridate::dmy(01082008)) |
|
| 264 | 6x |
data$yday = lubridate::yday(data$date) |
| 265 | 6x |
data$doy = data$yday - 212 # year begins in august to be consistent with the class |
| 266 | 6x |
data$season <- stringr::str_c(lubridate::year(data$date) - 1, "-", lubridate::year(data$date)) # year-1-year |
| 267 | 6x |
data$season[data$doy > 0] <- stringr::str_c(lubridate::year(data$date), "-", |
| 268 | 6x |
lubridate::year(data$date) + 1)[data$doy > 0] # for november and december it's year - year+1 |
| 269 | 6x |
data$yearbis <- data$year # same as season but with a numeric |
| 270 | 6x |
data$yearbis[data$doy > 0] <- data$yearbis[data$doy > 0] + 1 # same as season but a numeric |
| 271 | 6x |
data$doy[data$doy < 0] <- data$doy[data$doy < 0] + 365 |
| 272 | 6x |
data$time = as.numeric(data$date - origine) |
| 273 | 6x |
return(data) |
| 274 |
} |
|
| 275 | 3x |
don$date <- as.POSIXct(as.Date(don$date)) # bug the tz in CEST and GMT don't fit well |
| 276 |
# and the range of time between don and newcoe becomes extremely different |
|
| 277 | 3x |
don <- fndate(don) |
| 278 | 3x |
newcoe = data.frame(date = seq, mean_weight = NA, number = NA, lot = NA, yday = lubridate::yday(seq)) |
| 279 | 3x |
newcoe$date = as.POSIXct(newcoe$date) |
| 280 | 3x |
newcoe = fndate(newcoe) |
| 281 |
|
|
| 282 | 3x |
if (model.type == "seasonal") {
|
| 283 | 2x |
result <- data.frame(season = unique(don$season), year = unique(don$yearbis), |
| 284 | 2x |
a = NA, T = NA, b = NA) |
| 285 | 2x |
for (seas in unique(don$season)) {
|
| 286 |
# seas<-unique(don$season)[1] |
|
| 287 | 13x |
if (!silent){
|
| 288 | 7x |
print(seas) |
| 289 | 7x |
print("___________")
|
| 290 |
} |
|
| 291 |
# regression one per season, taking T as adjusted previously |
|
| 292 | 13x |
year = result[result$season == seas, "year"] |
| 293 | 13x |
g0 <- nls(formula = w ~ a * cos(2 * pi * (doy - T)/365) + b, data = don[don$season == |
| 294 | 13x |
seas, ], start = list(a = 0.08, T = 73.7, b = 0.29)) |
| 295 |
# getting the results into a table result |
|
| 296 | 13x |
result[result$season == seas, c("a", "T", "b")] <- coef(g0)
|
| 297 | 13x |
if (!silent){
|
| 298 | 7x |
print(summary(g0)) |
| 299 | 7x |
print("AIC:")
|
| 300 | 7x |
print(AIC(g0)) |
| 301 |
} |
|
| 302 |
# what is the size in december ? I'm just using the formula from |
|
| 303 |
# Guerault and Desaunay |
|
| 304 |
# result[result$season==seas,'pred_weight']<-coef(g0)['a']*cos(2*pi*(50-T)/365)+coef(g0)['b'] |
|
| 305 |
# dataframe for prediction, I will bind them to get a final |
|
| 306 |
# dataframe (predatafull) for the graph below |
|
| 307 | 13x |
predatay <- newcoe[newcoe$season == seas, ] |
| 308 | 13x |
predatay$pred_weight <- predict(g0, newdata = predatay) |
| 309 | 13x |
if (seas == unique(don$season)[1]) {
|
| 310 | 2x |
predata <- predatay |
| 311 | 13x |
} else predata <- rbind(predata, predatay) |
| 312 |
} |
|
| 313 | 2x |
if (!silent) print(result) |
| 314 | 2x |
assign("result", result, envir_stacomi)
|
| 315 | 2x |
if (!silent) |
| 316 | 2x |
funout(gettext("Model equations assigned to envir_stacomi (result)",
|
| 317 | 2x |
domain = "R-stacomiR")) |
| 318 |
|
|
| 319 | 2x |
p <- ggplot(don) + geom_jitter(aes(x = doy, y = w), col = "aquamarine4") + |
| 320 | 2x |
facet_wrap(~season) + geom_line(aes(x = doy, y = pred_weight), data = predata) + |
| 321 |
# geom_line(aes(x=doy,y=pred_weight),color='green',size=1,data=predatafull[predatafull$doy==50,])+ |
|
| 322 | 2x |
theme_minimal() + theme(panel.border = element_blank(), axis.line = element_line()) + |
| 323 | 2x |
xlab(gettext("Day in the season, starting 1st of august", domain = "R-StacomiR"))
|
| 324 |
|
|
| 325 | 2x |
print(p) |
| 326 | 2x |
assign("p", p, envir = envir_stacomi)
|
| 327 | 2x |
if (!silent) |
| 328 | 2x |
funout(gettext("ggplot object p assigned to envir_stacomi", domain = "R-stacomiR"))
|
| 329 |
|
|
| 330 |
|
|
| 331 |
# fm <- stats::nls(formula=w ~ a*cos(2*pi*(doy-T)/365)+b |
|
| 332 |
# ,data=don,start=list(a=0.1,T=73,b=0.3)) pred<-stats::predict(fm, |
|
| 333 |
# newdata=newcoe) com=gettextf('sinusoidal model,
|
|
| 334 |
# a.cos(2.pi.(jour-T)/365)+b a=%s t=%s |
|
| 335 |
# b=%s',round(coef(fm),2)[1],round(coef(fm),2)[2],round(coef(fm),2)[3]) |
|
| 336 |
# plot(r_gew,plot.type=2) points(as.POSIXct(newcoe$date),pred, |
|
| 337 |
# col='magenta') legend('topright',c('Obs.', 'Coeff base','Mod'),
|
|
| 338 |
# col=c('black','cyan','magenta'),pch='o',cex = 0.8)
|
|
| 339 |
# mtext(com,side=3,line=0.5) |
|
| 340 |
|
|
| 341 | 2x |
result_to_text <- stringr::str_c(sapply(t(result[, c(1, 3, 4, 5)]), as.character), |
| 342 | 2x |
collapse = " ") |
| 343 |
|
|
| 344 |
# setting text for comment (lines inserted into the database) |
|
| 345 | 2x |
com = stringr::str_c("w ~ a*cos(2*pi*(doy-T)/365)+b with a period T.", " The julian time d0 used is this model is set at zero 1st of November doy = d + d0; d0 = 305.",
|
| 346 | 2x |
" Coefficients for the model (one line per season): season, a, T, b =", |
| 347 | 2x |
result_to_text) |
| 348 |
|
|
| 349 | 3x |
} else if (model.type == "seasonal1") {
|
| 350 |
|
|
| 351 | 1x |
g1 = mgcv::gam(w ~ s(yday, bs = "cc") + s(time), data = don, knots = list(yday = c(1, |
| 352 | 1x |
365))) |
| 353 |
# the knots=list(yday=c(1,365) is necessary for a smooth construction |
|
| 354 |
# of the model |
|
| 355 | 1x |
summary(g1) |
| 356 | 1x |
plot(g1, pages = 1) |
| 357 | 1x |
predata <- newcoe |
| 358 | 1x |
pred <- predict(g1, newdata = predata, se.fit = TRUE, type="response") |
| 359 | 1x |
predata$pred_weight <- pred$fit |
| 360 | 1x |
predata$pred_weight_lwr <- pred$fit - 1.96 * pred$se.fit |
| 361 | 1x |
predata$pred_weight_upr <- pred$fit + 1.96 * pred$se.fit |
| 362 | 1x |
p <- ggplot(don) + geom_jitter(aes(x = date, y = w), col = "aquamarine4") + |
| 363 | 1x |
geom_line(aes(x = date, y = pred_weight), data = predata) + geom_ribbon(data = predata, |
| 364 | 1x |
aes(x = date, ymin = pred_weight_lwr, ymax = pred_weight_upr), alpha = 0.3, |
| 365 | 1x |
fill = "saddlebrown") + scale_x_datetime(date_breaks = "years", date_minor_breaks = "month") + |
| 366 | 1x |
theme_minimal() + theme(panel.border = element_blank(), axis.line = element_line()) + |
| 367 | 1x |
xlab("Date")
|
| 368 | 1x |
if (!silent) print(p) |
| 369 | 1x |
assign("p", p, envir = envir_stacomi)
|
| 370 | 1x |
assign("g1", g1, envir = envir_stacomi)
|
| 371 | 1x |
if (!silent) |
| 372 | 1x |
funout(gettext("ggplot object p assigned to envir_stacomi", domain = "R-stacomiR"))
|
| 373 | 1x |
if (!silent) |
| 374 | 1x |
funout(gettext("gam model g1 assigned to envir_stacomi", domain = "R-stacomiR"))
|
| 375 | 1x |
com = "model seasonal1 = gam(w~s(yday,bs='cc')+s(time), knots = list(yday = c(1, 365)))" |
| 376 |
|
|
| 377 | 3x |
} else if (model.type == "seasonal2") {
|
| 378 |
|
|
| 379 |
## seasonal effects with a continuous sine-cosine wave,. The |
|
| 380 |
## formula for this is 'sin(omegavt) + cos(omegavt)', |
|
| 381 |
## where vt is the time index variable |
|
| 382 |
## \tomega is a constant that describes how the index |
|
| 383 |
## variable relates to the full period (here, |
|
| 384 |
## 2pi/365=0.0172). |
|
| 385 | ! |
g2 = mgcv::gam(w ~ cos(0.0172 * doy) + sin(0.0172 * doy) + s(time), data = don) |
| 386 | ! |
print(gettext("One model per year, doy starts in august", domain = "R-stacomiR"))
|
| 387 | ! |
summary(g2) |
| 388 | ! |
plot(g2, pages = 1) |
| 389 | ! |
predata <- newcoe |
| 390 | ! |
pred <- predict(g2, newdata = predata, se.fit = TRUE, type="response") |
| 391 | ! |
predata$pred_weight <- pred$fit |
| 392 | ! |
predata$pred_weight_lwr <- pred$fit - 1.96 * pred$se.fit |
| 393 | ! |
predata$pred_weight_upr <- pred$fit + 1.96 * pred$se.fit |
| 394 | ! |
p <- ggplot(don) + geom_jitter(aes(x = date, y = w), col = "aquamarine4") + |
| 395 | ! |
geom_line(aes(x = date, y = pred_weight), data = predata) + geom_ribbon(data = predata, |
| 396 | ! |
aes(x = date, ymin = pred_weight_lwr, ymax = pred_weight_upr), alpha = 0.8, |
| 397 | ! |
fill = "wheat") + |
| 398 | ! |
scale_x_datetime(date_breaks = "years", date_minor_breaks = "month") + |
| 399 | ! |
theme_minimal() + |
| 400 | ! |
theme(panel.border = element_blank(), axis.line = element_line()) + |
| 401 | ! |
xlab("Date")
|
| 402 | ! |
if (!silent) print(p) |
| 403 | ! |
assign("p", p, envir = envir_stacomi)
|
| 404 | ! |
assign("g2", g2, envir = envir_stacomi)
|
| 405 | ! |
if (!silent) |
| 406 | ! |
funout(gettext("ggplot object p assigned to envir_stacomi", domain = "R-stacomiR"))
|
| 407 | ! |
if (!silent) |
| 408 | ! |
funout(gettext("gam model g2 assigned to envir_stacomi", domain = "R-stacomiR"))
|
| 409 |
|
|
| 410 |
## comparison |
|
| 411 |
## with Guerault and Desaunay (summary table in latex) |
|
| 412 | ! |
gamma = as.numeric(sqrt(g2$coefficients["cos(0.0172 * doy)"]^2 + g2$coefficients["sin(0.0172 * doy)"]^2)) #0.386 |
| 413 |
# compared with 0.111 |
|
| 414 | ! |
phi = round(as.numeric(atan2(g2$coefficients["sin(0.0172 * doy)"], g2$coefficients["cos(0.0172 * doy)"]) - |
| 415 | ! |
pi/2)) # -0.82 |
| 416 |
# time is centered on zero |
|
| 417 | ! |
s0 = as.numeric(g2$coefficients["(Intercept)"]) #7.04 (compared with 6.981) |
| 418 | ! |
summary_harmonic <- data.frame(source = c("Vilaine 1991-1993, Guerault et Desaunay",
|
| 419 | ! |
"This model"), `$\\gamma$` = c(0.0375, gamma), `$s_0$` = c(0.263, s0), |
| 420 | ! |
`$\\phi$` = c(319, 305 - phi)) |
| 421 |
# need to repass colnames |
|
| 422 | ! |
colnames(summary_harmonic) = c("source", "$\\gamma$", "$s_0(cm)$", "$\\phi$")
|
| 423 | ! |
xt_summary_harmonic <- xtable(summary_harmonic, caption = gettext("Comparison of the coefficients obtained by \\citet{desaunay_seasonal_1997} and in the present modelling of estuarine samples.",
|
| 424 | ! |
domain = "R-stacomiR"), label = gettext("summary_harmonic", domain = "R-stacomiR"),
|
| 425 | ! |
digits = c(0, 0, 3, 3, 0)) |
| 426 | ! |
tabname <- stringr::str_c(get("datawd", envir = envir_stacomi), "/summary_harmonic.tex")
|
| 427 | ! |
o <- print(xt_summary_harmonic, file = tabname, table.placement = "htbp", |
| 428 | ! |
caption.placement = "top", NA.string = "", include.rownames = FALSE, |
| 429 | ! |
tabular.environment = "tabularx", width = "0.6\\textwidth", sanitize.colnames.function = function(x) {
|
| 430 | ! |
x |
| 431 |
}) |
|
| 432 |
|
|
| 433 | ! |
funout(gettextf("summary coefficients written in %s", tabname, domain = "R-stacomiR"))
|
| 434 | ! |
com = stringr::str_c("model seasonal2 = gam(w~cos(0.0172*doy)+sin(0.0172*doy)+s(time), knots = list(yday = c(1, 365))),Desaunay's gamma=",
|
| 435 | ! |
round(gamma, 3), ", phi=", phi, ", s0=", round(s0, 3)) |
| 436 |
|
|
| 437 |
|
|
| 438 | 3x |
} else if (model.type == "manual") {
|
| 439 | ! |
if (!silent) |
| 440 | ! |
funout(gettext("Table for predictions newcoe assigned to envir_stacomi",
|
| 441 | ! |
domain = "R-stacomiR")) |
| 442 | ! |
assign("newcoe", newcoe, envir = envir_stacomi)
|
| 443 | ! |
if (!silent) |
| 444 | ! |
funout(gettext("Table of data don assigned to envir_stacomi", domain = "R-stacomiR"))
|
| 445 | ! |
assign("don", don, envir = envir_stacomi)
|
| 446 | ! |
if (!silent) |
| 447 | ! |
funout(gettext("Table of current coefficients coe assigned to envir_stacomi",
|
| 448 | ! |
domain = "R-stacomiR")) |
| 449 | ! |
assign("coe", coe, envir = envir_stacomi)
|
| 450 |
} |
|
| 451 |
|
|
| 452 | 3x |
if (model.type != "manual") {
|
| 453 | 3x |
import_coe = data.frame(coe_tax_code = "2038", coe_std_code = "CIV", coe_qte_code = 1, |
| 454 | 3x |
coe_date_debut = Hmisc::roundPOSIXt(predata$date, digits = "days"), coe_date_fin = Hmisc::roundPOSIXt(predata$date, |
| 455 | 3x |
digits = "days") + as.difftime(1, units = "days"), coe_valeur_coefficient = 1/predata$pred_weight, |
| 456 | 3x |
coe_commentaires = com) |
| 457 |
# will write only if the database is present |
|
| 458 | 3x |
if (get("database_expected", envir_stacomi)) {
|
| 459 | 1x |
fileout = paste(get("datawd", envir = envir_stacomi), "import_coe", r_gew@start_year@year_selected,
|
| 460 | 1x |
r_gew@end_year@year_selected, ".csv", sep = "") |
| 461 | 1x |
utils::write.table(import_coe, file = fileout, row.names = FALSE, sep = ";") |
| 462 | 1x |
if (! silent){
|
| 463 | ! |
funout(paste(gettextf("data directory :%s", fileout, domain = "R-stacomiR")))
|
| 464 |
} |
|
| 465 |
} |
|
| 466 | 3x |
assign("import_coe", import_coe, envir = envir_stacomi)
|
| 467 | 3x |
if (! silent){
|
| 468 | 2x |
funout(gettext("To obtain the table, type : import_coe=get(\"import_coe\",envir_stacomi)",
|
| 469 | 2x |
domain = "R-stacomiR")) |
| 470 |
} |
|
| 471 | 3x |
r_gew@calcdata[["import_coe"]] <- import_coe |
| 472 |
} |
|
| 473 | 3x |
return(r_gew) |
| 474 |
}) |
|
| 475 | ||
| 476 | ||
| 477 | ||
| 478 | ||
| 479 |
#' Method to write data to the stacomi database for \link{report_ge_weight-class}
|
|
| 480 |
#' |
|
| 481 |
#' Data will be written in tj_coefficientconversion_coe table, if the class retrieves some data |
|
| 482 |
#' from the database, those will be deleted first. |
|
| 483 |
#' @param object An object of class \link{report_ge_weight-class}
|
|
| 484 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 485 |
#' @return Nothing, called for its side effect of writing to the database |
|
| 486 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 487 |
#' @aliases write_database.report_ge_weight |
|
| 488 |
setMethod("write_database", signature = signature("report_ge_weight"), definition = function(object,
|
|
| 489 |
silent = FALSE) {
|
|
| 490 |
|
|
| 491 | 1x |
r_gew <- object |
| 492 | 1x |
if (!"import_coe" %in% names(r_gew@calcdata)) |
| 493 | 1x |
funout(gettext("Attention, you must fit a model before trying to write the predictions in the database",
|
| 494 | 1x |
domain = "R-stacomiR"), arret = TRUE) |
| 495 |
# first delete existing data from the database |
|
| 496 | 1x |
supprime(r_gew@coe, tax = 2038, std = "CIV", silent = silent) |
| 497 | 1x |
import_coe <- r_gew@calcdata$import_coe |
| 498 | 1x |
import_coe$coe_org_code <- get_org() |
| 499 | 1x |
import_coe$coe_date_debut <- as.Date(import_coe$coe_date_debut)+1 # to avoid day change with POSIXct and database |
| 500 | 1x |
import_coe$coe_date_fin <- as.Date(import_coe$coe_date_fin)+1 |
| 501 | 1x |
con <- new("ConnectionDB")
|
| 502 | 1x |
con <- connect(con) |
| 503 | 1x |
on.exit(pool::poolClose(con@connection)) |
| 504 | 1x |
sql <- "DROP TABLE IF EXISTS import_coe" |
| 505 | 1x |
pool::dbExecute(con@connection, statement = sql) |
| 506 | 1x |
pool::dbWriteTable(con@connection, |
| 507 | 1x |
name = "import_coe", |
| 508 | 1x |
value=import_coe, |
| 509 | 1x |
temporary=TRUE) |
| 510 | 1x |
sql <- stringr::str_c("INSERT INTO ", get_schema(), "tj_coefficientconversion_coe (",
|
| 511 | 1x |
"coe_tax_code,coe_std_code,coe_qte_code,coe_date_debut,coe_date_fin,coe_valeur_coefficient, |
| 512 | 1x |
coe_commentaires,coe_org_code)", |
| 513 | 1x |
" SELECT coe_tax_code,coe_std_code,coe_qte_code,coe_date_debut,coe_date_fin,coe_valeur_coefficient::real, |
| 514 | 1x |
coe_commentaires,coe_org_code FROM import_coe;") |
| 515 | 1x |
pool::dbExecute(con@connection, statement = sql) |
| 516 | 1x |
if (!silent){
|
| 517 | ! |
funout(gettext(sprintf("You have written %s rows in the database",nrow(import_coe)),
|
| 518 | ! |
domain = "R-stacomiR")) |
| 519 |
} |
|
| 520 | 1x |
return(invisible(NULL)) |
| 521 |
}) |
|
| 522 | ||
| 523 | ||
| 524 |
| 1 |
#' Migration reports for multiple DC / species / stages |
|
| 2 |
#' |
|
| 3 |
#' Migration counts for several Fish counting devices (DC), several taxa and several stages. |
|
| 4 |
#' This migration count can be built either by the graphical interface or from the command line |
|
| 5 |
#' (see examples). |
|
| 6 | ||
| 7 |
#' @note A Migration report comes from a migration monitoring : the fishes are monitored in a section of river, this section is |
|
| 8 |
#' called a control station (station). Most often, there is a dam, one or several fishways (DF) which comprise one or several counting devices (DC). |
|
| 9 |
#' On each counting device, the migration is recorded. It can be either an instant recording (video control) or the use of traps, |
|
| 10 |
#' Operations are monitoring operations during a period. For each operation, several species of fishes can be recorded (samples). In the case of migratory |
|
| 11 |
#' fishes the stage of development is important as it may indicate generic migrations, to and fro, between the river and the sea. |
|
| 12 |
#' |
|
| 13 |
#' Hence a Multiple Migration report is built from several one or several counting devices (DC), one or several Taxa (Taxon), one or several stages |
|
| 14 |
#' (stage). The migration can be also recorded not as numbers, but in the case of glass eels, as weight, which will be later transformed to number, |
|
| 15 |
#' from daily conversion coefficients. The methods in this class test whether the counts are numbers or another type of quantity. |
|
| 16 |
#' This class makes different calculations than report_mig, it does not handle escapement coefficients, |
|
| 17 |
#' it uses quantities other than numbers if necessary (only used for glass eel in the connect method). |
|
| 18 |
#' @slot dc An object of class \code{ref_dc-class}
|
|
| 19 |
#' @slot taxa An object of class \code{\link{ref_taxa-class}}
|
|
| 20 |
#' @slot stage An object of class \code{\link{ref_stage-class}}
|
|
| 21 |
#' @slot timestep An object of class \code{\link{ref_timestep_daily-class}}
|
|
| 22 |
#' @slot data A data.frame containing raw data filled by the connect method |
|
| 23 |
#' @slot calcdata A 'list' of calculated daily data, one per dc, filled in by the calcule method |
|
| 24 |
#' @slot coef_conversion A data frame of daily weight to number conversion coefficients, filled in by the connect |
|
| 25 |
#' method if any weight are found in the data slot. |
|
| 26 |
#' @slot time.sequence A POSIXt time sequence |
|
| 27 |
#' @family report Objects |
|
| 28 |
#' @aliases report_mig_mult |
|
| 29 |
#' @keywords classes |
|
| 30 |
#' @example inst/examples/report_mig_mult-example.R |
|
| 31 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 32 |
#' @export |
|
| 33 |
setClass(Class = "report_mig_mult", representation = representation(dc = "ref_dc", |
|
| 34 |
taxa = "ref_taxa", stage = "ref_stage", timestep = "ref_timestep_daily", data = "data.frame", |
|
| 35 |
calcdata = "list", coef_conversion = "data.frame", time.sequence = "POSIXct"), |
|
| 36 |
prototype = prototype(dc = new("ref_dc"), taxa = new("ref_taxa"), stage = new("ref_stage"),
|
|
| 37 |
timestep = new("ref_timestep_daily"), data = data.frame(), calcdata = list(),
|
|
| 38 |
coef_conversion = data.frame(), time.sequence = as.POSIXct(Sys.time()))) |
|
| 39 | ||
| 40 |
setValidity("report_mig_mult", function(object) {
|
|
| 41 |
rep1 = length(object@dc) >= 1 |
|
| 42 |
rep2 = length(object@taxa) >= 1 |
|
| 43 |
rep3 = length(object@stage) >= 1 |
|
| 44 |
return(ifelse(rep1 & rep2 & rep3, TRUE, c(1:6)[!c(rep1, rep2, rep3)])) |
|
| 45 |
}) |
|
| 46 | ||
| 47 | ||
| 48 | ||
| 49 | ||
| 50 |
#' charge method for report_mig_mult |
|
| 51 |
#' |
|
| 52 |
#' Unique the other report classes where the charge method is only used by the graphical interface |
|
| 53 |
#' to collect and test objects in the environment envir_stacomi, and see if the right choices have |
|
| 54 |
#' been made in the graphical interface, this method is used to load data on migration control operations |
|
| 55 |
#' fishway operations, and counting devices operations as data from those are displayed in the main plots. |
|
| 56 |
#' |
|
| 57 |
#' @param object An object of class \link{report_mig_mult-class}
|
|
| 58 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 59 |
#' @return An object of class \link{report_mig_mult-class} with slots filled from values assigned in \code{envir_stacomi} environment
|
|
| 60 |
#' @aliases charge.report_mig_mult |
|
| 61 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 62 |
setMethod("charge", signature = signature("report_mig_mult"), definition = function(object,
|
|
| 63 |
silent = FALSE) {
|
|
| 64 | 10x |
report_mig_mult <- object |
| 65 | 10x |
if (exists("ref_dc", envir_stacomi)) {
|
| 66 | 10x |
report_mig_mult@dc <- get("ref_dc", envir_stacomi)
|
| 67 | 10x |
dc <- report_mig_mult@dc@dc_selected |
| 68 | 10x |
df <- report_mig_mult@dc@data$df[report_mig_mult@dc@data$dc %in% dc] |
| 69 |
} else {
|
|
| 70 | ! |
funout(gettext("You need to choose a counting device, clic on validate\n",
|
| 71 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 72 |
} |
|
| 73 | 10x |
if (exists("ref_taxa", envir_stacomi)) {
|
| 74 | 10x |
report_mig_mult@taxa <- get("ref_taxa", envir_stacomi)
|
| 75 |
} else {
|
|
| 76 | ! |
funout(gettext("You need to choose a taxa, clic on validate\n", domain = "R-stacomiR"),
|
| 77 | ! |
arret = TRUE) |
| 78 |
} |
|
| 79 | 10x |
if (exists("ref_stage", envir_stacomi)) {
|
| 80 | 10x |
report_mig_mult@stage <- get("ref_stage", envir_stacomi)
|
| 81 |
} else {
|
|
| 82 | ! |
funout(gettext("You need to choose a stage, clic on validate\n", domain = "R-stacomiR"),
|
| 83 | ! |
arret = TRUE) |
| 84 |
} |
|
| 85 | 10x |
if (exists("timestep", envir_stacomi)) {
|
| 86 | 10x |
report_mig_mult@timestep <- get("timestep", envir_stacomi)
|
| 87 |
} else {
|
|
| 88 | ! |
funout(gettext("Attention, no time step selected, compunting with default value\n",
|
| 89 | ! |
domain = "R-stacomiR"), arret = FALSE) |
| 90 | ! |
warning("Attention, no time step selected, compunting with default value\n")
|
| 91 |
} |
|
| 92 |
################################# loading data for other classes associated |
|
| 93 |
################################# with report_mig_mult |
|
| 94 | 10x |
assign("report_dc_date_debut", get("timestep", envir_stacomi)@dateDebut, envir_stacomi)
|
| 95 | 10x |
assign("report_dc_date_fin", as.POSIXlt(end_date(get("timestep", envir_stacomi))),
|
| 96 | 10x |
envir_stacomi) |
| 97 | 10x |
assign("report_df_date_debut", get("timestep", envir_stacomi)@dateDebut, envir_stacomi)
|
| 98 | 10x |
assign("report_df_date_fin", as.POSIXlt(end_date(get("timestep", envir_stacomi))),
|
| 99 | 10x |
envir_stacomi) |
| 100 | 10x |
assign("report_ope_date_debut", get("timestep", envir_stacomi)@dateDebut, envir_stacomi)
|
| 101 | 10x |
assign("report_ope_date_fin", as.POSIXlt(end_date(get("timestep", envir_stacomi))),
|
| 102 | 10x |
envir_stacomi) |
| 103 |
|
|
| 104 | 10x |
report_ope <- get("report_ope", envir = envir_stacomi)
|
| 105 | 10x |
report_ope <- charge(report_ope) |
| 106 |
# charge will search for ref_dc (possible multiple choice), |
|
| 107 |
# report_ope_date_debut and report_ope_date_fin in envir_stacomi |
|
| 108 | 10x |
report_dc <- get("report_dc", envir = envir_stacomi)
|
| 109 |
# charge will search for ref_dc (possible multiple choice), |
|
| 110 |
# report_dc_date_debut and report_dc_date_fin in envir_stacomi |
|
| 111 | 10x |
report_dc <- charge(report_dc) |
| 112 | 10x |
ref_df = new("ref_df")
|
| 113 | 10x |
ref_df <- charge(ref_df) |
| 114 | 10x |
ref_df <- choice_c(ref_df, df) |
| 115 | 10x |
assign("ref_df", ref_df, envir = envir_stacomi)
|
| 116 | 10x |
report_df <- get("report_df", envir = envir_stacomi)
|
| 117 |
# charge will search for ref_df (possible multiple choice), |
|
| 118 |
# report_df_date_debut and report_df_date_fin in envir_stacomi |
|
| 119 | 10x |
report_df <- charge(report_df) |
| 120 |
# the object are assigned to the envir_stacomi for later use by the connect |
|
| 121 |
# method |
|
| 122 | 10x |
assign("report_df", report_df, envir = envir_stacomi)
|
| 123 | 10x |
assign("report_dc", report_dc, envir = envir_stacomi)
|
| 124 | 10x |
assign("report_ope", report_ope, envir = envir_stacomi)
|
| 125 | 10x |
stopifnot(validObject(report_mig_mult, test = TRUE)) |
| 126 |
# connect will load, coefficients, DF, DC, operations |
|
| 127 | 10x |
return(report_mig_mult) |
| 128 |
}) |
|
| 129 | ||
| 130 | ||
| 131 |
#' command line interface for report_mig_mult class |
|
| 132 |
#' |
|
| 133 |
#' The choice_c method fills in the data slot for ref_dc, ref_taxa, ref_stage and then |
|
| 134 |
#' uses the choice_c methods of these object to 'select' the data. |
|
| 135 |
#' @param object An object of class \link{report_mig-class}
|
|
| 136 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 137 |
#' @param taxa Either a species name in latin or the SANDRE code for species (ie 2038=Anguilla anguilla), |
|
| 138 |
#' these should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 139 |
#' @param stage A stage code matching the ref.tr_stadedeveloppement_std table in the stacomi database see \link{choice_c,ref_stage-method}
|
|
| 140 |
#' @param datedebut The starting date as a character, formats like \code{\%Y-\%m-\%d} or \code{\%d-\%m-\%Y} can be used as input
|
|
| 141 |
#' @param datefin The finishing date of the report, for this class this will be used to calculate the number of daily steps. |
|
| 142 |
#' @param silent Should messages be hided default FALSE |
|
| 143 |
#' @return An object of class \link{report_mig_mult-class} with data selected
|
|
| 144 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 145 |
#' @aliases choice_c.report_mig_mult |
|
| 146 |
setMethod("choice_c", signature = signature("report_mig_mult"), definition = function(object,
|
|
| 147 |
dc, taxa, stage, datedebut, datefin, silent = FALSE) {
|
|
| 148 | 13x |
report_mig_mult <- object |
| 149 | 13x |
report_df = new("report_df")
|
| 150 | 13x |
assign("report_df", report_df, envir = envir_stacomi)
|
| 151 | 13x |
report_dc = new("report_dc")
|
| 152 | 13x |
assign("report_dc", report_dc, envir = envir_stacomi)
|
| 153 | 13x |
report_ope = new("report_ope")
|
| 154 | 13x |
assign("report_ope", report_ope, envir = envir_stacomi)
|
| 155 | 13x |
report_mig_mult@dc = charge(report_mig_mult@dc) |
| 156 |
# loads and verifies the dc |
|
| 157 | 13x |
report_mig_mult@dc <- choice_c(object = report_mig_mult@dc, dc) |
| 158 |
# only taxa present in the report_mig are used |
|
| 159 | 12x |
report_mig_mult@taxa <- charge_with_filter(object = report_mig_mult@taxa, |
| 160 | 12x |
report_mig_mult@dc@dc_selected) |
| 161 | 12x |
report_mig_mult@taxa <- choice_c(report_mig_mult@taxa, taxa) |
| 162 | 12x |
report_mig_mult@stage <- charge_with_filter(object = report_mig_mult@stage, |
| 163 | 12x |
dc_selected = report_mig_mult@dc@dc_selected, |
| 164 | 12x |
taxa_selected = report_mig_mult@taxa@taxa_selected) |
| 165 | 12x |
report_mig_mult@stage <- choice_c(report_mig_mult@stage, stage) |
| 166 | 12x |
report_mig_mult@timestep <- choice_c(report_mig_mult@timestep, datedebut, datefin) |
| 167 | 12x |
assign("report_mig_mult", report_mig_mult, envir = envir_stacomi)
|
| 168 | 12x |
if (!silent) |
| 169 | 12x |
funout(gettext("Choice made, and object report_mig_mult assigned in envir_stacomi"),
|
| 170 | 12x |
domain = "R-stacomiR") |
| 171 | 12x |
return(report_mig_mult) |
| 172 |
}) |
|
| 173 | ||
| 174 |
#' #' Transforms migration per period to daily migrations, and performs the conversion from weights to number is data |
|
| 175 |
#' are stored as weights (glass eel). This calculation is performed in a loop for all dc. |
|
| 176 |
#' |
|
| 177 |
#' The calculation must be launched once data are filled by the connect method. Currently the negative argument |
|
| 178 |
#' has no effect. |
|
| 179 |
#' |
|
| 180 |
#' @param object An object of class \link{report_mig_mult-class}
|
|
| 181 |
#' @param negative a boolean indicating if a separate sum must be done for positive and negative values, if true, positive and negative counts return |
|
| 182 |
#' different rows |
|
| 183 |
#' @param silent Default FALSE, should messages be stopped |
|
| 184 |
#' @note The class does not handle escapement rates, though structurally those are present in the database. If you |
|
| 185 |
#' want to use those you will have to do the calculation manually from the data in \code{report_mig_mult@data}.
|
|
| 186 |
#' @return report_mig_mult with a list in slot calcdata. For each dc one will find a list with the following elements |
|
| 187 |
#' \describe{
|
|
| 188 |
#' \item{method}{In the case of instantaneous periods (video counting) the sum of daily values is done by the \link{fun_report_mig_mult} method and the value indicated in method is 'sum'.
|
|
| 189 |
#' If any migration monitoring period is longer than a day, then the migration is split using the \link{fun_report_mig_mult_overlaps} function and the value indicated in the
|
|
| 190 |
#' method is 'overlaps' as the latter method uses the overlap package to split migration period.} |
|
| 191 |
#' \item{data}{the calculated data. If weight are present, the columns display weight or numbers, the total number is
|
|
| 192 |
#' 'Effectif_total' and corresponds to the addition of numbers and numbers converted from weight, |
|
| 193 |
#' the total weight is 'Poids_total'+'poids_depuis_effectifs' and corresponds to weighed glass eel plus glass eel number converted in weights. |
|
| 194 |
#' CALCULE corresponds to calulated number, MESURE to measured numbers, EXPERT to punctual expertise of migration (for instance measured in other path, or known migration |
|
| 195 |
#' of fishes passing the dam but not actually counted, PONCTUEL to fishes counted by visual identification but not by the counting apparatus (in case of technical problem for instance)} |
|
| 196 |
#' \item{contient_poids}{A boolean which indicates, in the case of glass eel, that the function \link{fun_weight_conversion} has been run to convert the weights to numbers using the weight
|
|
| 197 |
#' to number coefficients in the database (see link{report_ge_weight}).}
|
|
| 198 |
#' \item{negative}{A parameter indicating if negative migration (downstream in the case of upstream migration devices) have been converted to positive numbers,
|
|
| 199 |
#' not developed yet}} |
|
| 200 |
#' @aliases calcule.report_mig_mult |
|
| 201 |
setMethod("calcule", signature = signature("report_mig_mult"), definition = function(object,
|
|
| 202 |
negative = FALSE, silent = FALSE) {
|
|
| 203 |
|
|
| 204 |
# report_mig_mult<-r_mig_mult; negative=FALSE |
|
| 205 | 8x |
if (!silent) |
| 206 | 8x |
funout(gettext("Starting migration summary ... be patient\n", domain = "R-stacomiR"))
|
| 207 | 8x |
report_mig_mult <- object |
| 208 | 8x |
debut = report_mig_mult@timestep@dateDebut |
| 209 | 8x |
fin = end_date(report_mig_mult@timestep) |
| 210 | 8x |
time.sequence <- seq.POSIXt(from = debut, to = fin, by = as.numeric(report_mig_mult@timestep@step_duration)) |
| 211 | 8x |
report_mig_mult@time.sequence <- time.sequence |
| 212 | 8x |
lestableaux <- list() |
| 213 | 8x |
for (dic in unique(report_mig_mult@data$ope_dic_identifiant)) {
|
| 214 | 12x |
datasub <- report_mig_mult@data[report_mig_mult@data$ope_dic_identifiant == |
| 215 | 12x |
dic, ] |
| 216 | 12x |
datasub$duree = difftime(datasub$ope_date_fin, datasub$ope_date_debut, units = "days") |
| 217 | 12x |
if (any(datasub$duree > (report_mig_mult@timestep@step_duration/86400))) {
|
| 218 |
#---------------------- |
|
| 219 |
# reports with overlaps |
|
| 220 |
#---------------------- |
|
| 221 | 9x |
data <- fun_report_mig_mult_overlaps(time.sequence = time.sequence, datasub = datasub, |
| 222 | 9x |
negative = negative) |
| 223 |
# to remain compatible with report mig : |
|
| 224 | 9x |
data$taux_d_echappement = -1 |
| 225 | 9x |
lestableaux[[stringr::str_c("dc_", dic)]][["data"]] <- data
|
| 226 | 9x |
lestableaux[[stringr::str_c("dc_", dic)]][["method"]] <- "overlaps"
|
| 227 | 9x |
contient_poids <- "poids" %in% datasub$type_de_quantite |
| 228 | 9x |
lestableaux[[stringr::str_c("dc_", dic)]][["contient_poids"]] <- contient_poids
|
| 229 |
|
|
| 230 | 9x |
lestableaux[[stringr::str_c("dc_", dic)]][["negative"]] <- negative
|
| 231 | 9x |
if (contient_poids) {
|
| 232 | 6x |
coe <- report_mig_mult@coef_conversion[, c("coe_date_debut", "coe_valeur_coefficient")]
|
| 233 | 6x |
data$coe_date_debut <- as.Date(data$debut_pas) |
| 234 | 6x |
data <- merge(data, coe, by = "coe_date_debut") |
| 235 | 6x |
data <- data[, -1] # removing coe_date_debut |
| 236 | 6x |
data <- fun_weight_conversion(tableau = data, time.sequence = report_mig_mult@time.sequence, |
| 237 | 6x |
silent) |
| 238 |
} |
|
| 239 |
|
|
| 240 | 9x |
lestableaux[[stringr::str_c("dc_", dic)]][["data"]] <- data
|
| 241 |
|
|
| 242 |
} else {
|
|
| 243 |
#---------------------- |
|
| 244 |
# report simple |
|
| 245 |
#---------------------- |
|
| 246 | 3x |
mydata <- fun_report_mig_mult(time.sequence = time.sequence, datasub = datasub, |
| 247 | 3x |
negative = negative) |
| 248 | 3x |
mydata$taux_d_echappement = -1 |
| 249 | 3x |
mydata$coe_valeur_coefficient = NA |
| 250 | 3x |
contient_poids <- "poids" %in% datasub$type_de_quantite |
| 251 | 3x |
if (contient_poids) {
|
| 252 |
# at this tage data for coe_valeur_coefficient are null, we |
|
| 253 |
# remove the column before merging |
|
| 254 | ! |
mydata <- mydata[, -match("coe_valeur_coefficient", colnames(mydata))]
|
| 255 | ! |
coe <- report_mig_mult@coef_conversion[, c("coe_date_debut", "coe_valeur_coefficient")]
|
| 256 | ! |
mydata$coe_date_debut <- as.Date(mydata$debut_pas) |
| 257 | ! |
mydata2 <- merge(mydata, coe, by = "coe_date_debut") |
| 258 | ! |
mydata2 <- mydata2[, -match("coe_date_debut", colnames(mydata2))] # removing coe_date_debut
|
| 259 | ! |
data <- fun_weight_conversion(tableau = mydata2, time.sequence = report_mig_mult@time.sequence, |
| 260 | ! |
silent) |
| 261 |
} else {
|
|
| 262 | 3x |
data <- mydata |
| 263 |
} |
|
| 264 | 3x |
lestableaux[[stringr::str_c("dc_", dic)]][["data"]] <- data
|
| 265 | 3x |
lestableaux[[stringr::str_c("dc_", dic)]][["method"]] <- "sum"
|
| 266 | 3x |
lestableaux[[stringr::str_c("dc_", dic)]][["contient_poids"]] <- contient_poids
|
| 267 | 3x |
lestableaux[[stringr::str_c("dc_", dic)]][["negative"]] <- negative
|
| 268 |
} |
|
| 269 | 8x |
} # end for dic |
| 270 |
# TODO developper une methode pour sumneg |
|
| 271 | 8x |
report_mig_mult@calcdata <- lestableaux |
| 272 | 8x |
assign("report_mig_mult", report_mig_mult, envir_stacomi)
|
| 273 | 8x |
if (!silent) {
|
| 274 | ! |
funout(gettext("The summary object is stored in environment envir_stacomi, write report_mig_mult=get(\"report_mig_mult\",envir_stacomi) \n",
|
| 275 | ! |
domain = "R-stacomiR")) |
| 276 | ! |
funout(gettext("Raw data are stored in report_mig_mult@data, processed data in report_mig_mult@calcdata\\n\n",
|
| 277 | ! |
domain = "R-stacomiR")) |
| 278 |
} |
|
| 279 | 8x |
return(report_mig_mult) |
| 280 |
}) |
|
| 281 | ||
| 282 |
#' connect method for report_mig_mult |
|
| 283 |
#' |
|
| 284 |
#' this method loads data from the database for report_mig but also fills the table of conversion coefficient, if |
|
| 285 |
#' the taxa is eel. It also calls connect method for \link{report_df-class},
|
|
| 286 |
#' \link{report_dc-class} and \link{report_ope-class} associated with the report
|
|
| 287 |
#' and used by the \link{fungraph} and \link{fungraph_glasseel} functions. As a side effect it assigns
|
|
| 288 |
#' objects \link{report_dc-class}, \link{report_df-class} and \link{report_ope-class} in environment \code{envir_stacomi}
|
|
| 289 | ||
| 290 | ||
| 291 |
#' @param object An object of class \link{report_mig_mult-class}
|
|
| 292 |
#' @param silent Boolean, if TRUE messages are not displayed |
|
| 293 |
#' @return An object of class \link{report_mig_mult-class} with slot @data filled from the database
|
|
| 294 |
#' @aliases connect.report_mig_mult |
|
| 295 |
setMethod("connect", signature = signature("report_mig_mult"), definition = function(object,
|
|
| 296 |
silent = FALSE) {
|
|
| 297 |
# recuperation du report_mig report_mig_mult<-bmM |
|
| 298 |
|
|
| 299 | 20x |
report_mig_mult <- object |
| 300 |
|
|
| 301 |
# retrieve the argument of the function and passes it to report_mig_mult |
|
| 302 |
# easier to debug |
|
| 303 | 20x |
req = new("RequeteDBwheredate")
|
| 304 | 20x |
req@colonnedebut <- "ope_date_debut" |
| 305 | 20x |
req@colonnefin <- "ope_date_fin" |
| 306 |
# we round the date to be consistent with daily values from the |
|
| 307 | 20x |
req@datedebut = report_mig_mult@timestep@dateDebut |
| 308 | 20x |
req@datefin = as.POSIXlt(end_date(report_mig_mult@timestep) + as.difftime("23:59:59"))
|
| 309 | 20x |
if (length(report_mig_mult@dc@dc_selected) == 0) |
| 310 | 20x |
stop("DC has length zero, are you connected to the right schema, do you use the right dc number ?")
|
| 311 | 20x |
dc = vector_to_listsql(report_mig_mult@dc@dc_selected) |
| 312 | 20x |
if (length(report_mig_mult@taxa@taxa_selected) == 0) |
| 313 | 20x |
stop("Taxa has length zero, are you connected to the right schema, do you use the right taxa ?")
|
| 314 | 20x |
tax = vector_to_listsql(report_mig_mult@taxa@taxa_selected) |
| 315 | 20x |
if (length(report_mig_mult@stage@stage_selected) == 0) |
| 316 | 20x |
stop("Stage has length zero, are you connected to the right schema, do you use the right stage ?")
|
| 317 | 20x |
std = vector_to_listsql(report_mig_mult@stage@stage_selected) |
| 318 | 20x |
sch = get_schema() |
| 319 | 20x |
req@select = stringr::str_c("SELECT
|
| 320 | 20x |
ope_identifiant, |
| 321 | 20x |
lot_identifiant, |
| 322 | 20x |
ope_date_debut, |
| 323 | 20x |
ope_date_fin, |
| 324 | 20x |
ope_dic_identifiant, |
| 325 | 20x |
lot_tax_code, |
| 326 | 20x |
lot_std_code, |
| 327 | 20x |
CASE WHEN lot_effectif is not NULL then lot_effectif |
| 328 | 20x |
WHEN lot_effectif is null then lot_quantite |
| 329 | 20x |
end as value, |
| 330 | 20x |
case when lot_effectif is not NULL then 'effectif' |
| 331 | 20x |
when lot_effectif is null and lot_qte_code='1' then 'poids' |
| 332 | 20x |
when lot_effectif is null and lot_qte_code='2' then 'volume' |
| 333 | 20x |
else 'quantite' end as type_de_quantite, |
| 334 | 20x |
lot_dev_code, |
| 335 | 20x |
lot_methode_obtention", |
| 336 | 20x |
" FROM ", sch, "t_operation_ope", " JOIN ", sch, "t_lot_lot on lot_ope_identifiant=ope_identifiant") |
| 337 |
# removing character marks |
|
| 338 | 20x |
req@select <- stringr::str_replace_all(req@select, "[\r\n\t]", "") |
| 339 |
# the where clause is returned by DBWheredate |
|
| 340 | 20x |
req@and = stringr::str_c(" AND ope_dic_identifiant in", dc, " AND lot_tax_code in ",
|
| 341 | 20x |
tax, " AND lot_std_code in ", std, " AND lot_lot_identifiant IS NULL") |
| 342 | 20x |
req <- stacomirtools::query(req) |
| 343 | 20x |
report_mig_mult@data = req@query |
| 344 | 20x |
if (!silent) |
| 345 | 20x |
cat(stringr::str_c("data collected from the database nrow=", nrow(report_mig_mult@data),
|
| 346 | 20x |
"\n")) |
| 347 |
# recuperation des coefficients si il y a des civelles dans le report |
|
| 348 | 20x |
if (2038 %in% report_mig_mult@taxa@taxa_selected) {
|
| 349 | 14x |
req = new("RequeteDBwheredate")
|
| 350 | 14x |
req@select = paste("select * from", sch, "tj_coefficientconversion_coe")
|
| 351 | 14x |
req@datedebut = as.POSIXlt(report_mig_mult@timestep@dateDebut) |
| 352 | 14x |
req@datefin = as.POSIXlt(end_date(report_mig_mult@timestep)) |
| 353 | 14x |
req@colonnedebut <- "coe_date_debut" |
| 354 | 14x |
req@colonnefin <- "coe_date_fin" |
| 355 | 14x |
req@and <- c("and coe_tax_code='2038'", "and coe_std_code='CIV'")
|
| 356 | 14x |
req@order_by <- "order by coe_date_debut" |
| 357 | 14x |
req <- stacomirtools::query(req) |
| 358 | 14x |
report_mig_mult@coef_conversion <- req@query |
| 359 |
|
|
| 360 |
} |
|
| 361 | 20x |
stopifnot(validObject(report_mig_mult, test = TRUE)) |
| 362 |
|
|
| 363 |
#######################'' |
|
| 364 |
# connect method for associated classes |
|
| 365 | 20x |
report_ope <- get("report_ope", envir = envir_stacomi)
|
| 366 | 20x |
report_dc <- get("report_dc", envir = envir_stacomi)
|
| 367 | 20x |
report_df <- get("report_df", envir = envir_stacomi)
|
| 368 | 20x |
report_ope <- connect(report_ope, silent = silent) |
| 369 | 20x |
report_dc <- connect(report_dc, silent = silent) |
| 370 | 20x |
report_df <- connect(report_df, silent = silent) |
| 371 | 20x |
assign("report_df", report_df, envir = envir_stacomi)
|
| 372 | 20x |
assign("report_dc", report_dc, envir = envir_stacomi)
|
| 373 | 20x |
assign("report_ope", report_ope, envir = envir_stacomi)
|
| 374 | 20x |
return(report_mig_mult) |
| 375 |
}) |
|
| 376 | ||
| 377 | ||
| 378 |
#' Plots of various type for report_mig_mult |
|
| 379 |
#' |
|
| 380 |
#' \itemize{
|
|
| 381 |
#' \item{plot.type='standard'}{calls \code{\link{fungraph}} and \code{\link{fungraph_glasseel}} functions to plot as many 'report_mig'
|
|
| 382 |
#' as needed, the function will test for the existence of data for one dc, one taxa, and one stage} |
|
| 383 |
#' \item{plot.type='step'}{creates Cumulated graphs for report_mig_mult. Data are summed per day for different dc taxa and stages}
|
|
| 384 |
#' \item{plot.type='multiple'}{Method to overlay graphs for report_mig_mult (multiple dc/taxa/stage in the same plot)}
|
|
| 385 |
#' } |
|
| 386 |
#' @param x An object of class report_mig_mult |
|
| 387 |
#' @param plot.type One of 'standard','step','multiple'. Defaut to \code{standard} the standard report_mig with dc and operation displayed, can also be \code{step} or
|
|
| 388 |
#' \code{multiple}
|
|
| 389 |
#' @param silent Stops most messages from being displayed |
|
| 390 |
#' @param color Default NULL, argument passed for the plot.type='standard' method. A vector of color in the following order : (1) working, (2) stopped, (3:7) 1...5 types of operation, |
|
| 391 |
#' (8:11) numbers, weight, NULL, NULL (if glass eel), (8:11) measured, calculated, expert, direct observation for other taxa. If null will be set to brewer.pal(12,'Paired')[c(8,10,4,6,1,2,3,5,7)] |
|
| 392 |
#' @param color_ope Default NULL, argument passed for the plot.type='standard' method. A vector of color for the operations. Default to brewer.pal(4,'Paired') |
|
| 393 |
#' @param ... Additional arguments passed to matplot or plot if plot.type='standard', see ... in \link{fungraph_glasseel} and \link{fungraph}
|
|
| 394 |
#' @return Nothing, called for its side effect of plotting |
|
| 395 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 396 |
#' @aliases plot.report_mig_mult |
|
| 397 |
#' @export |
|
| 398 |
setMethod("plot", signature(x = "report_mig_mult", y = "missing"), definition = function(x,
|
|
| 399 |
plot.type = "standard", color = NULL, color_ope = NULL, silent = FALSE, ...) {
|
|
| 400 |
# print('entering plot function') report_mig_mult<-r_mig_mult;silent=FALSE
|
|
| 401 | 7x |
report_mig_mult <- x |
| 402 | 7x |
the_taxa = report_mig_mult@taxa@data[report_mig_mult@taxa@data$tax_code %in% report_mig_mult@taxa@taxa_selected, ] |
| 403 | 7x |
the_stages = report_mig_mult@stage@data[report_mig_mult@stage@data$std_code %in% report_mig_mult@stage@stage_selected, ] |
| 404 | 7x |
lesdc = as.numeric(report_mig_mult@dc@dc_selected) |
| 405 |
# ==========================type=1============================= |
|
| 406 | 7x |
if (plot.type == "standard") {
|
| 407 | 2x |
if (!silent) |
| 408 | 2x |
print("plot type standard")
|
| 409 | 2x |
if (!silent) |
| 410 | 2x |
funout(gettext("Statistics about migration :\n", domain = "R-stacomiR"))
|
| 411 |
# dcnum=1;taxanum=1;stagenum=2 &&&&&&&&&&&&&&&&&&&&&&&&&debut de |
|
| 412 |
# boucle&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 413 | 2x |
compte <- 0 |
| 414 | 2x |
for (dcnum in 1:length(lesdc)) {
|
| 415 | 3x |
for (taxanum in 1:nrow(the_taxa)) {
|
| 416 | 3x |
for (stagenum in 1:nrow(the_stages)) {
|
| 417 |
# dcnum=1;taxnum=1;stagenum=1 |
|
| 418 | 5x |
taxa <- the_taxa[taxanum, "tax_nom_latin"] |
| 419 | 5x |
stage <- the_stages[stagenum, "std_libelle"] |
| 420 | 5x |
dc <- lesdc[dcnum] |
| 421 | 5x |
data <- report_mig_mult@calcdata[[stringr::str_c("dc_", dc)]][["data"]]
|
| 422 | 5x |
data <- data[data$lot_tax_code == the_taxa[taxanum, "tax_code"] & |
| 423 | 5x |
data$lot_std_code == the_stages[stagenum, "std_code"], ] |
| 424 |
|
|
| 425 | 5x |
if (!is.null(data)) {
|
| 426 | 3x |
if (nrow(data) > 0) |
| 427 |
{
|
|
| 428 |
|
|
| 429 | 3x |
if (!silent) {
|
| 430 | ! |
funout(paste("dc=", dc, taxa = taxa, stage = stage, "\n"))
|
| 431 | ! |
funout("---------------------\n")
|
| 432 |
} |
|
| 433 | 3x |
if (any(duplicated(data$No.pas))) |
| 434 | 3x |
stop("duplicated values in No.pas")
|
| 435 | 3x |
data_without_hole <- merge(data.frame(No.pas = as.numeric(strftime(report_mig_mult@time.sequence, |
| 436 | 3x |
format = "%j")) - 1, debut_pas = report_mig_mult@time.sequence), |
| 437 | 3x |
data, by = c("No.pas", "debut_pas"), all.x = TRUE)
|
| 438 | 3x |
data_without_hole$CALCULE[is.na(data_without_hole$CALCULE)] <- 0 |
| 439 | 3x |
data_without_hole$MESURE[is.na(data_without_hole$MESURE)] <- 0 |
| 440 | 3x |
data_without_hole$EXPERT[is.na(data_without_hole$EXPERT)] <- 0 |
| 441 | 3x |
data_without_hole$PONCTUEL[is.na(data_without_hole$PONCTUEL)] <- 0 |
| 442 | 3x |
compte = compte + 1 |
| 443 | 3x |
if (report_mig_mult@calcdata[[stringr::str_c("dc_", dc)]][["contient_poids"]] &
|
| 444 | 3x |
taxa == "Anguilla anguilla" & (stage == "civelle" | stage == |
| 445 | 3x |
"Anguilla jaune")) {
|
| 446 |
|
|
| 447 |
#---------------------------------- |
|
| 448 |
# report migration with weight (glass eel) |
|
| 449 |
#----------------------------------------- |
|
| 450 | 2x |
if (compte != 1) |
| 451 | 2x |
dev.new() |
| 452 | 2x |
fungraph_glasseel(report_mig = report_mig_mult, table = data_without_hole, |
| 453 | 2x |
time.sequence = report_mig_mult@time.sequence, taxa = taxa, |
| 454 | 2x |
stage = stage, dc = dc, color = color, color_ope = color_ope, |
| 455 | 2x |
silent, ...) |
| 456 |
} else {
|
|
| 457 |
|
|
| 458 |
#---------------------------------- |
|
| 459 |
# report migration standard |
|
| 460 |
#----------------------------------------- |
|
| 461 | 1x |
if (compte != 1) |
| 462 | 1x |
dev.new() |
| 463 |
# silent=TRUE |
|
| 464 | 1x |
fungraph(report_mig = report_mig_mult, tableau = data_without_hole, |
| 465 | 1x |
time.sequence = report_mig_mult@time.sequence, taxa, |
| 466 | 1x |
stage, dc, color = color, color_ope = color_ope, silent, |
| 467 |
...) |
|
| 468 |
} |
|
| 469 | 3x |
} # end nrow(data)>0 |
| 470 |
# ecriture du report journalier, ecrit aussi le report |
|
| 471 |
# mensuel fn_Ecritreport_daily(report_mig_mult) |
|
| 472 |
|
|
| 473 |
} |
|
| 474 |
} |
|
| 475 |
} |
|
| 476 |
} |
|
| 477 |
# &&&&&&&&&&&&&&&&&&&&&&&&&fin de boucle&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 478 |
} |
|
| 479 |
# ==========================type=2============================= |
|
| 480 | 7x |
if (plot.type == "step") {
|
| 481 | ||
| 482 | 2x |
grdata <- data.frame() |
| 483 | 2x |
for (i in 1:length(report_mig_mult@calcdata)) {
|
| 484 | 2x |
data <- report_mig_mult@calcdata[[i]]$data |
| 485 |
# extracting similar columns (not those calculated) |
|
| 486 | 2x |
data <- data[, c("No.pas", "debut_pas", "fin_pas", "ope_dic_identifiant",
|
| 487 | 2x |
"lot_tax_code", "lot_std_code", "MESURE", "CALCULE", "EXPERT", "PONCTUEL", |
| 488 | 2x |
"Effectif_total")] |
| 489 | 2x |
grdata <- rbind(grdata, data) |
| 490 |
} |
|
| 491 | 2x |
names(grdata) <- tolower(names(grdata)) |
| 492 | 2x |
grdata <- as.data.frame(grdata %>% |
| 493 | 2x |
dplyr::group_by(debut_pas, no.pas) %>% |
| 494 | 2x |
dplyr::summarize(effectif_total = sum(effectif_total)) %>% |
| 495 | 2x |
dplyr::arrange(debut_pas)) |
| 496 | 2x |
grdata_without_hole <- merge(data.frame(no.pas = as.numeric(strftime(report_mig_mult@time.sequence, |
| 497 | 2x |
format = "%j")) - 1, debut_pas = report_mig_mult@time.sequence), grdata, |
| 498 | 2x |
by = c("no.pas", "debut_pas"), all.x = TRUE)
|
| 499 | 2x |
grdata_without_hole <- fun_date_extraction(grdata_without_hole, nom_coldt = "debut_pas", |
| 500 | 2x |
annee = FALSE, mois = TRUE, quinzaine = TRUE, semaine = TRUE, jour_an = TRUE, |
| 501 | 2x |
jour_mois = FALSE, heure = FALSE) |
| 502 | 2x |
grdata_without_hole <- grdata_without_hole[order(grdata_without_hole$no.pas), ] |
| 503 | 2x |
grdata_without_hole$effectif_total[is.na(grdata_without_hole$effectif_total)] <- 0 |
| 504 |
|
|
| 505 | 2x |
grdata_without_hole$cumsum = cumsum(grdata_without_hole$effectif_total) |
| 506 | 2x |
annee = unique(strftime(as.POSIXlt(report_mig_mult@time.sequence), "%Y")) |
| 507 | 2x |
dis_commentaire = paste(as.character(report_mig_mult@dc@dc_selected), |
| 508 | 2x |
collapse = ",") |
| 509 | 2x |
update_geom_defaults("step", aes(size = 3))
|
| 510 |
|
|
| 511 | 2x |
p <- ggplot(grdata_without_hole) + geom_step(aes(x = debut_pas, y = cumsum, |
| 512 | 2x |
colour = mois)) + ylab(gettext("Cumulative migration", domain = "R-stacomiR")) +
|
| 513 | 2x |
theme(plot.title = element_text(size = 10, colour = "deepskyblue")) + |
| 514 | 2x |
xlab("mois") + scale_colour_manual(values = c(`01` = "#092360", `02` = "#1369A2",
|
| 515 | 2x |
`03` = "#0099A9", `04` = "#009780", `05` = "#67B784", `06` = "#CBDF7C", |
| 516 | 2x |
`07` = "#FFE200", `08` = "#DB9815", `09` = "#E57B25", `10` = "#F0522D", |
| 517 | 2x |
`11` = "#912E0F", `12` = "#33004B")) + ggtitle(gettextf("Cumulative count %s, %s, %s, %s",
|
| 518 | 2x |
dis_commentaire, paste(the_taxa$tax_nom_latin, collapse=", "), paste(the_stages$std_libelle, collapse=","), annee)) |
| 519 | 2x |
print(p) |
| 520 | 2x |
assign("p", p, envir = envir_stacomi)
|
| 521 | 2x |
assign("grdata", grdata_without_hole, envir_stacomi)
|
| 522 | 2x |
if (!silent) |
| 523 | 2x |
funout(gettext("The plot has been assigned to p in envir_stacomi,write p<-get('p',envir_stacomi) to retrieve the object"))
|
| 524 | 2x |
if (!silent) |
| 525 | 2x |
funout(gettext("The data for the plot have been assigned to envir_stacomi,write grdata<-get('grdata',envir_stacomi) to retrieve the object"))
|
| 526 |
|
|
| 527 |
} |
|
| 528 |
# ==========================type=3============================= |
|
| 529 | 7x |
if (plot.type == "multiple") {
|
| 530 | 3x |
grdata <- fun_aggreg_for_plot(report_mig_mult) |
| 531 | 3x |
if (length(unique(grdata$taxa)) == 1 & length(unique(grdata$stage)) == 1) {
|
| 532 | 1x |
p <- ggplot(grdata, aes(x = debut_pas, y = effectif_total), fill = "black") + |
| 533 | 1x |
geom_bar(position = "stack", stat = "identity") + facet_grid(DC ~ |
| 534 | 1x |
., scales = "free_y") |
| 535 | 3x |
} else if (length(unique(grdata$taxa)) == 1) {
|
| 536 | 2x |
p <- ggplot(grdata, aes(x = debut_pas, y = effectif_total, fill = stage)) + |
| 537 | 2x |
geom_bar(position = "stack", stat = "identity") + facet_grid(DC ~ |
| 538 | 2x |
., scales = "free_y") + scale_fill_brewer(palette = "Set2") |
| 539 | 3x |
} else if (length(unique(grdata$stage)) == 1) {
|
| 540 | ! |
p <- ggplot(grdata, aes(x = debut_pas, y = effectif_total, fill = taxa)) + |
| 541 | ! |
geom_bar(position = "stack", stat = "identity") + facet_grid(DC ~ |
| 542 | ! |
., scales = "free_y") + scale_fill_brewer(palette = "Set2") |
| 543 |
} else {
|
|
| 544 | ! |
p <- ggplot(grdata, aes(x = debut_pas, y = effectif_total, fill = stage)) + |
| 545 | ! |
geom_bar(position = "stack", stat = "identity") + facet_grid(DC + |
| 546 | ! |
taxa ~ ., scales = "free_y") + scale_fill_brewer(palette = "Set2") |
| 547 |
} |
|
| 548 |
|
|
| 549 | 3x |
print(p) |
| 550 | 3x |
assign("p", p, envir = envir_stacomi)
|
| 551 | 3x |
if (!silent) |
| 552 | 3x |
funout(gettext("The plot has been assigned to p in envir_stacomi,write p<-get('p',envir_stacomi) to retrieve the object"))
|
| 553 | 3x |
assign("grdata", grdata, envir_stacomi)
|
| 554 | 3x |
if (!silent) |
| 555 | 3x |
funout(gettext("The data for the plot have been assigned to envir_stacomi,write grdata<-get('grdata',envir_stacomi) to retrieve the object"))
|
| 556 |
|
|
| 557 |
} |
|
| 558 |
# ==========================end / type=3============================= |
|
| 559 | 7x |
return(invisible(NULL)) |
| 560 |
}) |
|
| 561 | ||
| 562 | ||
| 563 |
#' summary for report_mig_mult |
|
| 564 |
#' calls functions funstat and funtable to create migration overviews |
|
| 565 |
#' and generate csv and html output in the user data directory |
|
| 566 |
#' @param object An object of class \code{\link{report_mig_mult-class}}
|
|
| 567 |
#' @param silent Should the program stay silent or display messages, default FALSE |
|
| 568 |
#' @param ... Additional parameters (not used there) |
|
| 569 |
#' @return Nothing, runs funstat and funtable method for each DC |
|
| 570 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 571 |
#' @aliases summary.report_mig_mult |
|
| 572 |
#' @export |
|
| 573 |
setMethod("summary", signature = signature(object = "report_mig_mult"), definition = function(object,
|
|
| 574 |
silent = FALSE, ...) {
|
|
| 575 |
# report_mig_mult<-r_mig_mult; silent<-FALSE |
|
| 576 | 4x |
report_mig_mult <- object |
| 577 | 4x |
the_taxa = report_mig_mult@taxa@data[report_mig_mult@taxa@data$tax_code %in% report_mig_mult@taxa@taxa_selected, ] |
| 578 | 4x |
the_stages = report_mig_mult@stage@data[report_mig_mult@stage@data$std_code %in% report_mig_mult@stage@stage_selected, ] |
| 579 | 4x |
lesdc = as.numeric(report_mig_mult@dc@dc_selected) |
| 580 | 4x |
if (!silent) |
| 581 | 4x |
funout(gettext("Statistics about migration :\n", domain = "R-stacomiR"))
|
| 582 |
# &&&&&&&&&&&&&&&&&&&&&&&&&debut de boucle&&&&&&&&&&&&&&&&&&&&&&&&&&& |
|
| 583 |
# dcnum=2;taxanum=1;stagenum=1 |
|
| 584 | 4x |
for (dcnum in 1:length(lesdc)) {
|
| 585 | 5x |
for (taxanum in 1:nrow(the_taxa)) {
|
| 586 | 5x |
for (stagenum in 1:nrow(the_stages)) {
|
| 587 |
|
|
| 588 | 5x |
taxa = the_taxa[taxanum, "tax_nom_latin"] |
| 589 | 5x |
stage = the_stages[stagenum, "std_libelle"] |
| 590 | 5x |
DC = lesdc[dcnum] |
| 591 |
|
|
| 592 |
# preparation du jeu de donnees pour la fonction fungraph_civ |
|
| 593 |
# developpee pour la classe report_mig |
|
| 594 | 5x |
data <- report_mig_mult@calcdata[[stringr::str_c("dc_", DC)]][["data"]]
|
| 595 | 5x |
data <- data[data$lot_tax_code == the_taxa[taxanum, "tax_code"] & |
| 596 | 5x |
data$lot_std_code == the_stages[stagenum, "std_code"], ] |
| 597 |
|
|
| 598 | 5x |
if (!is.null(data)) {
|
| 599 | 4x |
if (nrow(data) > 0) {
|
| 600 |
|
|
| 601 | 4x |
if (any(duplicated(data$No.pas))) |
| 602 | 4x |
stop("duplicated values in No.pas")
|
| 603 | 4x |
data_without_hole <- merge(data.frame(No.pas = as.numeric(strftime(report_mig_mult@time.sequence, |
| 604 | 4x |
format = "%j")) - 1, debut_pas = report_mig_mult@time.sequence), |
| 605 | 4x |
data, by = c("No.pas", "debut_pas"), all.x = TRUE)
|
| 606 | 4x |
data_without_hole$CALCULE[is.na(data_without_hole$CALCULE)] <- 0 |
| 607 | 4x |
data_without_hole$MESURE[is.na(data_without_hole$MESURE)] <- 0 |
| 608 | 4x |
data_without_hole$EXPERT[is.na(data_without_hole$EXPERT)] <- 0 |
| 609 | 4x |
data_without_hole$PONCTUEL[is.na(data_without_hole$PONCTUEL)] <- 0 |
| 610 |
|
|
| 611 | 4x |
resum = funstat(tableau = data_without_hole, time.sequence = report_mig_mult@time.sequence, |
| 612 | 4x |
taxa, stage, DC, silent) |
| 613 |
# pb with posixt and xtable, removing posixt and setting |
|
| 614 |
# date instead |
|
| 615 | 4x |
data_without_hole$debut_pas <- as.Date(data_without_hole$debut_pas) |
| 616 | 4x |
data_without_hole <- data_without_hole[, -match("fin_pas", colnames(data_without_hole))]
|
| 617 | 4x |
funtable(tableau = data_without_hole, time.sequence = report_mig_mult@time.sequence, |
| 618 | 4x |
taxa, stage, DC, resum, silent) |
| 619 |
|
|
| 620 |
} |
|
| 621 |
} |
|
| 622 |
} |
|
| 623 |
} |
|
| 624 |
} |
|
| 625 |
|
|
| 626 |
|
|
| 627 |
}) |
|
| 628 | ||
| 629 | ||
| 630 | ||
| 631 |
#' Method to print the command line of the object |
|
| 632 |
#' @param x An object of class report_mig_mult |
|
| 633 |
#' @param ... Additional parameters passed to print |
|
| 634 |
#' @return NULL |
|
| 635 |
#' @author cedric.briand |
|
| 636 |
#' @aliases print.report_mig_mult |
|
| 637 |
#' @export |
|
| 638 |
setMethod("print", signature = signature("report_mig_mult"), definition = function(x,
|
|
| 639 |
...) {
|
|
| 640 | ! |
sortie1 <- "report_mig_mult=new('report_mig_mult')\n"
|
| 641 | ! |
sortie2 <- stringr::str_c("report_mig_mult=choice_c(report_mig_mult,", "dc=c(",
|
| 642 | ! |
stringr::str_c(x@dc@dc_selected, collapse = ","), "),", "taxa=c(", stringr::str_c(shQuote(x@taxa@data$tax_nom_latin),
|
| 643 | ! |
collapse = ","), "),", "stage=c(", stringr::str_c(shQuote(x@stage@stage_selected),
|
| 644 | ! |
collapse = ","), "),", "datedebut=", shQuote(strftime(x@timestep@dateDebut, |
| 645 | ! |
format = "%d/%m/%Y")), ",datefin=", shQuote(strftime(end_date(x@timestep), |
| 646 | ! |
format = "%d/%m/%Y")), ")") |
| 647 |
# removing backslashes |
|
| 648 | ! |
funout(stringr::str_c(sortie1, sortie2), ...) |
| 649 | ! |
return(invisible(NULL)) |
| 650 |
}) |
|
| 651 | ||
| 652 |
#' Function to calculate daily migration using overlaps functions |
|
| 653 |
#' |
|
| 654 |
#' Function to calculate daily migration from migration monitoring whose length is more than one day, |
|
| 655 |
#' this calculation relies on the (false) assumption that migration is evenly spread over time. |
|
| 656 |
#' @param time.sequence the time sequence to be filled in with new data |
|
| 657 |
#' @param datasub the initial dataset |
|
| 658 |
#' @param negative 'boolean', default FALSE, TRUE indicates a separate sum for negative and positive migrations |
|
| 659 |
#' to time.sequence period and summed over the new sequence. A migration operation spanning several days will |
|
| 660 |
#' be converted to 'daily' values assuming that the migration was regular over time. The function |
|
| 661 |
#' returns one row per taxa, stages, counting device. It does not account for the destination of taxa. It returns |
|
| 662 |
#' separate rows for quantities and numbers. Several columns are according to the type of measure (MESURE, CALCULE, PONCTUEL or EXPERT). |
|
| 663 |
#' @return A data.frame with daily migrations |
|
| 664 |
#' @seealso calcule,report_mig_mult-method |
|
| 665 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 666 |
#' @export |
|
| 667 |
fun_report_mig_mult_overlaps <- function(time.sequence, datasub, negative = FALSE) {
|
|
| 668 |
# browser() |
|
| 669 | 13x |
mat1 <- as.data.frame(cbind(as.numeric(time.sequence), as.numeric(time.sequence + |
| 670 | 13x |
as.difftime(1, units = "days")))) |
| 671 | 13x |
mat2 <- as.data.frame(cbind(as.numeric(datasub$ope_date_debut), as.numeric(datasub$ope_date_fin))) |
| 672 | 13x |
rownames(mat1) <- as.character(time.sequence) |
| 673 | 13x |
rownames(mat2) <- datasub$lot_identifiant |
| 674 | 13x |
imat1 <- intervals::Intervals(mat1) |
| 675 | 13x |
intervals::closed(imat1) <- c(FALSE, FALSE) |
| 676 | 13x |
imat2 <- intervals::Intervals(mat2) |
| 677 | 13x |
intervals::closed(imat2) <- c(FALSE, FALSE) |
| 678 | 13x |
listei <- intervals::interval_overlap(imat2, imat1) |
| 679 | 13x |
listei2 <- listei # copie de la liste pour l'ecraser |
| 680 | 13x |
for (i in 1:length(listei)) {
|
| 681 | 2894x |
vec <- listei[[i]] |
| 682 | 2894x |
if (length(vec) == 0) {
|
| 683 |
# pas de lot |
|
| 684 | ! |
listei2[[i]] = 0 |
| 685 | 2894x |
} else if (length(vec) == 1) {
|
| 686 |
# l'ensemble du lot est inclus dans la journee |
|
| 687 | 3x |
listei2[[i]] = 1 |
| 688 |
} else {
|
|
| 689 |
# le premier jour va du debut de l'ope e la fin de la premiere date |
|
| 690 |
# puis n-2 jour puis le dernier jour de la date de debut e la fin |
|
| 691 |
# de l'ope |
|
| 692 | 2891x |
idlot = names(listei)[i] |
| 693 | 2891x |
tps = c(difftime(time.sequence[vec[1]] + as.difftime(1, units = "days"), |
| 694 | 2891x |
datasub[datasub$lot_identifiant == idlot, "ope_date_debut"], units = "days"), |
| 695 | 2891x |
rep(1, length(vec) - 2), difftime(datasub[datasub$lot_identifiant == |
| 696 | 2891x |
idlot, "ope_date_fin"], time.sequence[vec[length(vec)]], units = "days")) |
| 697 | 2891x |
listei2[[i]] <- as.numeric(tps)/(as.numeric(sum(tps))) # on ramene a 1 |
| 698 | 2891x |
stopifnot(all.equal(as.numeric(sum(listei2[[i]])), 1)) |
| 699 |
} |
|
| 700 |
} |
|
| 701 |
|
|
| 702 |
# specific case of operations across two years In this case we want to |
|
| 703 |
# split the operation and retain only the part corresponding to the current |
|
| 704 |
# year beginning of the year initializing variable browser() |
|
| 705 | 13x |
overlapping_samples_between_year <- FALSE |
| 706 | 13x |
imat3 <- imat1[1, ] |
| 707 | 13x |
listei3 <- intervals::interval_overlap(imat2, imat3) |
| 708 |
# vector of samples (lot) which are overlapping between two years |
|
| 709 | 13x |
lots_across <- names(listei3)[vapply(listei3, function(X) length(X) > 0, NA)] |
| 710 | 13x |
if (length(lots_across) > 0) {
|
| 711 | 3x |
overlapping_samples_between_year <- TRUE |
| 712 | 3x |
for (i in 1:length(lots_across)) {
|
| 713 | 27x |
the_lot <- lots_across[i] |
| 714 | 27x |
duration_in_the_year <- as.numeric(difftime(datasub[datasub$lot_identifiant == |
| 715 | 27x |
the_lot, "ope_date_fin"], time.sequence[1], units = "days")) |
| 716 | 27x |
duration_of_the_sample <- as.numeric(difftime(datasub[datasub$lot_identifiant == |
| 717 | 27x |
the_lot, "ope_date_fin"], datasub[datasub$lot_identifiant == the_lot, |
| 718 | 27x |
"ope_date_debut"], units = "days")) |
| 719 | 27x |
listei2[[the_lot]] <- listei2[[the_lot]] * (duration_in_the_year/duration_of_the_sample) |
| 720 |
|
|
| 721 |
} |
|
| 722 |
} |
|
| 723 |
####################### end of the year |
|
| 724 | 13x |
le <- length(time.sequence) |
| 725 | 13x |
mat3 <- as.data.frame(cbind(as.numeric(time.sequence[le] + as.difftime(1, units = "days")), |
| 726 | 13x |
as.numeric(time.sequence[le] + as.difftime(2, units = "days")))) |
| 727 | 13x |
imat3 <- intervals::Intervals(mat3) |
| 728 | 13x |
listei3 <- intervals::interval_overlap(imat2, imat3) |
| 729 |
# vector of samples (lot) which are overlapping between two years |
|
| 730 | 13x |
lots_across <- names(listei3)[vapply(listei3, function(X) length(X) > 0, NA)] |
| 731 | 13x |
if (length(lots_across) > 0) {
|
| 732 | 3x |
overlapping_samples_between_year <- TRUE |
| 733 | 3x |
for (i in 1:length(lots_across)) {
|
| 734 | 4x |
the_lot <- lots_across[i] |
| 735 | 4x |
duration_in_the_year <- as.numeric(difftime(time.sequence[length(time.sequence)] + |
| 736 | 4x |
lubridate::days(1), datasub[datasub$lot_identifiant == the_lot, "ope_date_debut"], |
| 737 | 4x |
units = "days")) |
| 738 | 4x |
duration_of_the_sample <- as.numeric(difftime(datasub[datasub$lot_identifiant == |
| 739 | 4x |
the_lot, "ope_date_fin"], datasub[datasub$lot_identifiant == the_lot, |
| 740 | 4x |
"ope_date_debut"], units = "days")) |
| 741 | 4x |
listei2[[the_lot]] <- listei2[[the_lot]] * (duration_in_the_year/duration_of_the_sample) |
| 742 |
|
|
| 743 |
} |
|
| 744 |
} |
|
| 745 |
|
|
| 746 |
|
|
| 747 |
# df ['lot_identifiant','coef','ts.id'] lot_identifiant= identifiant du |
|
| 748 |
# lot, coef = part du lot dans chaque id_seq (sequence de jours), 'id_seq' |
|
| 749 |
# numero du jour creating a table with lot_identifiant, sequence, and the |
|
| 750 |
# coeff to apply |
|
| 751 | 13x |
df <- data.frame(lot_identifiant = rep(names(listei2), sapply(listei2, length)), |
| 752 | 13x |
coef = unlist(listei2), ts_id = unlist(listei)) |
| 753 |
# dataframe corresponding to the whole time sequence |
|
| 754 | 13x |
df.ts = data.frame(debut_pas = time.sequence, fin_pas = time.sequence + as.difftime(1, |
| 755 | 13x |
units = "days"), ts_id = as.numeric(strftime(time.sequence, format = "%j")), |
| 756 | 13x |
stringsAsFactors = FALSE) |
| 757 | 13x |
dfts <- merge(df.ts, df, by = "ts_id") |
| 758 | 13x |
datasub1 <- merge(dfts, datasub, by = "lot_identifiant") |
| 759 | 13x |
datasub1$value <- as.numeric(datasub1$value) # Otherwise rounded to integer |
| 760 |
# If negative negative and positive are treated separately and return one |
|
| 761 |
# row for each positive or negative value below coef is the part of the |
|
| 762 |
# operation within the current year |
|
| 763 | 13x |
if (negative) {
|
| 764 |
|
|
| 765 | ! |
the_negative <- datasub1 %>% |
| 766 | ! |
dplyr::select(debut_pas, fin_pas, value, coef, type_de_quantite, ope_dic_identifiant, |
| 767 | ! |
lot_tax_code, lot_std_code, lot_methode_obtention) %>% |
| 768 | ! |
dplyr::filter(value < 0) %>% |
| 769 | ! |
dplyr::group_by(ope_dic_identifiant, lot_tax_code, lot_std_code, lot_methode_obtention, |
| 770 | ! |
debut_pas, fin_pas, type_de_quantite) %>% |
| 771 | ! |
dplyr::summarize(value = sum(value * coef)) %>% |
| 772 | ! |
dplyr::arrange(ope_dic_identifiant, debut_pas, lot_tax_code, lot_std_code, |
| 773 | ! |
type_de_quantite) |
| 774 |
|
|
| 775 | ! |
the_positive <- datasub1 %>% |
| 776 | ! |
dplyr::select(debut_pas, fin_pas, value, coef, type_de_quantite, ope_dic_identifiant, |
| 777 | ! |
lot_tax_code, lot_std_code, lot_methode_obtention) %>% |
| 778 | ! |
dplyr::filter(value >= 0) %>% |
| 779 | ! |
dplyr::group_by(ope_dic_identifiant, lot_tax_code, lot_std_code, lot_methode_obtention, |
| 780 | ! |
debut_pas, fin_pas, type_de_quantite) %>% |
| 781 | ! |
dplyr::summarize(value = sum(value * coef)) %>% |
| 782 | ! |
dplyr::arrange(ope_dic_identifiant, debut_pas, lot_tax_code, lot_std_code, |
| 783 | ! |
type_de_quantite) |
| 784 |
|
|
| 785 | ! |
datasub2 <- as.data.frame(rbind(the_negative, the_positive)) |
| 786 |
|
|
| 787 |
} else {
|
|
| 788 | 13x |
datasub2 <- as.data.frame(datasub1 %>% |
| 789 | 13x |
dplyr::select(debut_pas, fin_pas, value, coef, type_de_quantite, ope_dic_identifiant, |
| 790 | 13x |
lot_tax_code, lot_std_code, lot_methode_obtention) %>% |
| 791 | 13x |
dplyr::group_by(ope_dic_identifiant, lot_tax_code, lot_std_code, lot_methode_obtention, |
| 792 | 13x |
debut_pas, fin_pas, type_de_quantite) %>% |
| 793 | 13x |
dplyr::summarize(value = sum(value * coef)) %>% |
| 794 | 13x |
dplyr::arrange(ope_dic_identifiant, debut_pas, lot_tax_code, lot_std_code, |
| 795 | 13x |
type_de_quantite)) |
| 796 |
|
|
| 797 |
} |
|
| 798 |
# if some samples overlap between the current year and the year arround the |
|
| 799 |
# current year, then the calculation will have hampered our numbers of a |
|
| 800 |
# small amount and the following test is not expected to be TRUE. |
|
| 801 | 13x |
if (!overlapping_samples_between_year) |
| 802 |
# note 2020 I'm getting this strange results that I don't understand |
|
| 803 |
# round(sum(datasub$value, na.rm = TRUE), 2) and |
|
| 804 |
# round(sum(datasub2$value, na.rm = TRUE), 2) are not equal: Mean |
|
| 805 |
# relative difference: 0.000996741 so rounded values by 2 digits are |
|
| 806 |
# not equal ???? # changed test to 0.1 browser() |
|
| 807 |
# 2021 same issue when running the vignette but don't see any difference in the browser() ? |
|
| 808 | 13x |
if (!abs(round(sum(datasub$value, na.rm = TRUE), 2) - round(sum(datasub2$value, |
| 809 | 13x |
na.rm = TRUE), 2)) < 0.1) warnings( |
| 810 | 13x |
paste("the numbers are different between raw numbers",
|
| 811 | 13x |
round(sum(datasub$value, na.rm = TRUE), 2), |
| 812 | 13x |
"and number recalculated per day", |
| 813 | 13x |
round(sum(datasub2$value, na.rm = TRUE),2))) |
| 814 | 13x |
datasub3 <- reshape2::dcast(datasub2, debut_pas + fin_pas + ope_dic_identifiant + |
| 815 | 13x |
lot_tax_code + lot_std_code + type_de_quantite ~ lot_methode_obtention, value.var = "value") |
| 816 | 13x |
if (!"MESURE" %in% colnames(datasub3)) |
| 817 | 13x |
datasub3$MESURE = 0 |
| 818 | 13x |
if (!"CALCULE" %in% colnames(datasub3)) |
| 819 | 13x |
datasub3$CALCULE = 0 |
| 820 | 13x |
if (!"EXPERT" %in% colnames(datasub3)) |
| 821 | 13x |
datasub3$EXPERT = 0 |
| 822 | 13x |
if (!"PONCUTEL" %in% colnames(datasub3)) |
| 823 | 13x |
datasub3$PONCTUEL = 0 |
| 824 | 13x |
datasub3$MESURE[is.na(datasub3$MESURE)] <- 0 |
| 825 | 13x |
datasub3$CALCULE[is.na(datasub3$CALCULE)] <- 0 |
| 826 | 13x |
datasub3$EXPERT[is.na(datasub3$EXPERT)] <- 0 |
| 827 | 13x |
datasub3$PONCTUEL[is.na(datasub3$PONCTUEL)] <- 0 |
| 828 |
# pour compatibilite |
|
| 829 | 13x |
datasub3 <- cbind(data.frame(No.pas = as.numeric(strftime(datasub3$debut_pas, |
| 830 | 13x |
format = "%j")) - 1), datasub3) |
| 831 | 13x |
datasub3$Effectif_total = rowSums(datasub3[, c("MESURE", "CALCULE", "EXPERT",
|
| 832 | 13x |
"PONCTUEL")]) |
| 833 | 13x |
return(datasub3) |
| 834 |
} |
|
| 835 | ||
| 836 | ||
| 837 | ||
| 838 |
#' Calculate daily migration by simple repartition |
|
| 839 |
#' |
|
| 840 |
#' Function to calculate daily migration from migration monitoring whose length is less than one day, |
|
| 841 |
#' typically video recording whose period are instant events. |
|
| 842 |
#' @param time.sequence the time sequence to be filled in with new data |
|
| 843 |
#' @param datasub the initial dataset |
|
| 844 |
#' @param negative 'boolean', default FALSE, TRUE indicates a separate sum for negative and positive migrations |
|
| 845 |
#' @return A data.frame with number summed over over the time.sequence. |
|
| 846 |
#' The function returns the same output than \link{fun_report_mig_mult_overlaps}
|
|
| 847 |
#' but is intended to work faster. In the data.frame, the total number is |
|
| 848 |
#' 'Effectif_total' and corresponds to the addition of numbers and numbers converted from weight, |
|
| 849 |
#' the total weight is 'Poids_total'+'poids_depuis_effectifs' and corresponds to weighed glass eel plus glass eel number converted in weights. |
|
| 850 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 851 |
#' @export |
|
| 852 |
fun_report_mig_mult <- function(time.sequence, datasub, negative = FALSE) {
|
|
| 853 | 10x |
df.ts = data.frame(debut_pas = time.sequence, fin_pas = time.sequence + as.difftime(1, |
| 854 | 10x |
units = "days"), ts_id = strftime(time.sequence, format = "%j"), stringsAsFactors = FALSE) |
| 855 | 10x |
datasub$ts_id <- strftime(datasub$ope_date_debut, format = "%j") |
| 856 | 10x |
datasub1 <- merge(df.ts, datasub, by = "ts_id") |
| 857 | 10x |
if (negative) {
|
| 858 |
|
|
| 859 | ! |
the_negative <- datasub1 %>% |
| 860 | ! |
dplyr::select(debut_pas, fin_pas, value, type_de_quantite, ope_dic_identifiant, |
| 861 | ! |
lot_tax_code, lot_std_code, lot_methode_obtention) %>% |
| 862 | ! |
dplyr::filter(value < 0) %>% |
| 863 | ! |
dplyr::group_by(ope_dic_identifiant, lot_tax_code, lot_std_code, lot_methode_obtention, |
| 864 | ! |
debut_pas, fin_pas, type_de_quantite) %>% |
| 865 | ! |
dplyr::summarize(value = sum(value)) %>% |
| 866 | ! |
dplyr::arrange(ope_dic_identifiant, debut_pas, lot_tax_code, lot_std_code, |
| 867 | ! |
type_de_quantite) |
| 868 |
|
|
| 869 | ! |
the_positive <- datasub1 %>% |
| 870 | ! |
dplyr::select(debut_pas, fin_pas, value, type_de_quantite, ope_dic_identifiant, |
| 871 | ! |
lot_tax_code, lot_std_code, lot_methode_obtention) %>% |
| 872 | ! |
dplyr::filter(value >= 0) %>% |
| 873 | ! |
dplyr::group_by(ope_dic_identifiant, lot_tax_code, lot_std_code, lot_methode_obtention, |
| 874 | ! |
debut_pas, fin_pas, type_de_quantite) %>% |
| 875 | ! |
dplyr::summarize(value = sum(value)) %>% |
| 876 | ! |
dplyr::arrange(ope_dic_identifiant, debut_pas, lot_tax_code, lot_std_code, |
| 877 | ! |
type_de_quantite) |
| 878 |
|
|
| 879 | ! |
datasub2 <- as.data.frame(rbind(the_negative, the_positive)) |
| 880 |
|
|
| 881 |
} else {
|
|
| 882 | 10x |
datasub2 <- as.data.frame(datasub1 %>% |
| 883 | 10x |
dplyr::select(debut_pas, fin_pas, value, type_de_quantite, ope_dic_identifiant, |
| 884 | 10x |
lot_tax_code, lot_std_code, lot_methode_obtention) %>% |
| 885 | 10x |
dplyr::group_by(ope_dic_identifiant, lot_tax_code, lot_std_code, lot_methode_obtention, |
| 886 | 10x |
debut_pas, fin_pas, type_de_quantite) %>% |
| 887 | 10x |
dplyr::summarize(value = sum(value)) %>% |
| 888 | 10x |
dplyr::arrange(ope_dic_identifiant, debut_pas, lot_tax_code, lot_std_code, |
| 889 | 10x |
type_de_quantite)) |
| 890 |
|
|
| 891 |
} |
|
| 892 |
# note 2020 I'm getting this strange results that I don't understand |
|
| 893 |
# round(sum(datasub$value, na.rm = TRUE), 2) and |
|
| 894 |
# round(sum(datasub2$value, na.rm = TRUE), 2) are not equal: Mean |
|
| 895 |
# relative difference: 0.000996741 so rounded values by 2 digits are |
|
| 896 |
# not equal ???? # changed test to 0.1 |
|
| 897 |
# 2021 same issue when running the vignette but don't see any difference in the browser() ? |
|
| 898 |
# maybe due to different time settings on the machine so it's converted to a warning |
|
| 899 | 10x |
if (!abs(round(sum(datasub$value, na.rm = TRUE), 2) - round(sum(datasub2$value, |
| 900 | 10x |
na.rm = TRUE), 2)) < 0.1) warnings( |
| 901 | 10x |
paste("the numbers are different between raw numbers",
|
| 902 | 10x |
round(sum(datasub$value, na.rm = TRUE), 2), |
| 903 | 10x |
"and number recalculated per day", |
| 904 | 10x |
round(sum(datasub2$value, na.rm = TRUE),2))) |
| 905 | 10x |
datasub3 <- reshape2::dcast(datasub2, debut_pas + fin_pas + ope_dic_identifiant + |
| 906 | 10x |
lot_tax_code + lot_std_code + type_de_quantite ~ lot_methode_obtention, value.var = "value") |
| 907 | 10x |
if (!"MESURE" %in% colnames(datasub3)) |
| 908 | 10x |
datasub3$MESURE = 0 |
| 909 | 10x |
if (!"CALCULE" %in% colnames(datasub3)) |
| 910 | 10x |
datasub3$CALCULE = 0 |
| 911 | 10x |
if (!"EXPERT" %in% colnames(datasub3)) |
| 912 | 10x |
datasub3$EXPERT = 0 |
| 913 | 10x |
if (!"PONCTUEL" %in% colnames(datasub3)) |
| 914 | 10x |
datasub3$PONCTUEL = 0 |
| 915 | 10x |
datasub3$MESURE[is.na(datasub3$MESURE)] <- 0 |
| 916 | 10x |
datasub3$CALCULE[is.na(datasub3$CALCULE)] <- 0 |
| 917 | 10x |
datasub3$EXPERT[is.na(datasub3$EXPERT)] <- 0 |
| 918 | 10x |
datasub3$PONCTUEL[is.na(datasub3$PONCTUEL)] <- 0 |
| 919 | 10x |
datasub3 <- cbind(data.frame(No.pas = as.numeric(strftime(datasub3$debut_pas, |
| 920 | 10x |
format = "%j")) - 1), datasub3) |
| 921 | 10x |
datasub3$Effectif_total = rowSums(datasub3[, c("MESURE", "CALCULE", "EXPERT",
|
| 922 | 10x |
"PONCTUEL")]) |
| 923 | 10x |
return(datasub3) |
| 924 |
} |
|
| 925 | ||
| 926 |
#' returns a table where weights and number are calculated from number and weights respectively |
|
| 927 |
#' performs a query to collect the conversion coefficients |
|
| 928 |
#' @param tableau Table issued from report_mig |
|
| 929 |
#' @param time.sequence Time sequence from report_mig |
|
| 930 |
#' @param silent If silent=TRUE do not display messages |
|
| 931 |
#' @return tableau, the data frame with weight converted to numbers |
|
| 932 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 933 |
fun_weight_conversion = function(tableau, time.sequence, silent) {
|
|
| 934 | 7x |
if (!silent) |
| 935 | 7x |
funout(gettextf("dc=%s Conversion weight / number\n", unique(tableau$ope_dic_identifiant)))
|
| 936 | 7x |
nr <- nrow(unique(tableau[, c("debut_pas", "lot_tax_code", "lot_std_code")]))
|
| 937 | 7x |
tableaupoids = subset(tableau, tableau$type_de_quantite == "poids") |
| 938 | 7x |
tableaueffectif = subset(tableau, tableau$type_de_quantite == "effectif") |
| 939 | 7x |
tableaueffectif = tableaueffectif[, c("No.pas", "lot_tax_code", "lot_std_code",
|
| 940 | 7x |
"CALCULE", "MESURE", "EXPERT", "PONCTUEL", "Effectif_total")] |
| 941 | 7x |
tableaudesdeux = tableau[, c("No.pas", "debut_pas", "fin_pas", "ope_dic_identifiant",
|
| 942 | 7x |
"lot_tax_code", "lot_std_code", "coe_valeur_coefficient")] |
| 943 | 7x |
tableaudesdeux = tableaudesdeux[!duplicated(tableaudesdeux[, c("No.pas", "lot_tax_code",
|
| 944 | 7x |
"lot_std_code")]), ] |
| 945 |
# Conversion des poids en effectifs |
|
| 946 | 7x |
tableauconvert = tableaupoids[, c("MESURE", "CALCULE", "EXPERT", "PONCTUEL",
|
| 947 | 7x |
"Effectif_total")] |
| 948 | 7x |
tableauconvert = tableauconvert * tableaupoids$coe_valeur_coefficient # les coeff sont du type 2.54 et non 0.3 |
| 949 | 7x |
if (sum(tableaupoids$coe_valeur_coefficient) == 0) |
| 950 | 7x |
funout(gettext("Careful sum=0, you didn't enter the coefficient of conversion\n",
|
| 951 | 7x |
domain = "R-stacomiR")) |
| 952 |
# creation d'une tableau (matricepoids) a 5 colonnes comprenant les |
|
| 953 |
# effectifs convertis |
|
| 954 | 7x |
matricepoids = cbind(tableaupoids[, c("No.pas", "lot_tax_code", "lot_std_code")],
|
| 955 | 7x |
tableauconvert, tableaupoids[, c("MESURE", "CALCULE", "EXPERT", "PONCTUEL",
|
| 956 | 7x |
"Effectif_total")]) |
| 957 | 7x |
dimnames(matricepoids) = list(1:length(tableaupoids[, 1]), c("No.pas", "lot_tax_code",
|
| 958 | 7x |
"lot_std_code", "MESURE", "CALCULE", "EXPERT", "PONCTUEL", "Effectif_total", |
| 959 | 7x |
"poids_MESURE", "poids_CALCULE", "poids_EXPERT", "poids_PONCTUEL", "Poids_total")) |
| 960 | 7x |
tableau = merge(tableaudesdeux, tableaueffectif, by = c("No.pas", "lot_tax_code",
|
| 961 | 7x |
"lot_std_code"), all.x = TRUE, all.y = FALSE) |
| 962 | 7x |
tableau = merge(tableau, matricepoids, all.x = TRUE, all.y = FALSE, by = c("No.pas",
|
| 963 | 7x |
"lot_tax_code", "lot_std_code"), sort = TRUE, suffixes = c(".e", ".p"))
|
| 964 |
# je vire les NA |
|
| 965 | 7x |
tableau[is.na(tableau)] = 0 |
| 966 | 7x |
tableau$MESURE = tableau$MESURE.e + tableau$MESURE.p |
| 967 | 7x |
tableau$CALCULE = tableau$CALCULE.e + tableau$CALCULE.p |
| 968 | 7x |
tableau$EXPERT = tableau$EXPERT.e + tableau$EXPERT.p |
| 969 | 7x |
tableau$PONCTUEL = tableau$PONCTUEL.e + tableau$PONCTUEL.p |
| 970 | 7x |
tableau$Effectif_total = tableau$Effectif_total.e + tableau$Effectif_total.p |
| 971 | 7x |
tableau[, "poids_depuis_effectifs"] = tableau[, "Effectif_total.e"]/tableau$coe_valeur_coefficient |
| 972 | 7x |
stopifnot(nr == nrow(tableau)) |
| 973 | 7x |
return(tableau) |
| 974 |
} |
|
| 975 | ||
| 976 |
#' Calculates a data.frame where all components within the list calcdata are aggregated |
|
| 977 |
#' and formatted for plot |
|
| 978 |
#' @param object An object of class \link{report_mig_mult-class}
|
|
| 979 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 980 |
#' @return A data.frame |
|
| 981 |
#' @export |
|
| 982 |
fun_aggreg_for_plot <- function(object) {
|
|
| 983 | 6x |
if (!inherits(object , "report_mig_mult")) |
| 984 | 6x |
stop("This function must have for argument an object of class report_mig_mult")
|
| 985 | 6x |
the_taxa = paste(object@taxa@data[object@data$tax_code %in% object@taxa@taxa_selected,"tax_nom_latin"], collapse = ",") |
| 986 | 6x |
the_stages = paste(object@stage@data[object@data$std_code %in% object@stage@stage_selected,"std_libelle"], collapse = ",") |
| 987 | 6x |
grdata <- data.frame() |
| 988 | 6x |
for (i in 1:length(object@calcdata)) {
|
| 989 | 14x |
data <- object@calcdata[[i]]$data |
| 990 |
# extracting similar columns (not those calculated) |
|
| 991 | 14x |
data <- data[, c("No.pas", "debut_pas", "fin_pas", "ope_dic_identifiant",
|
| 992 | 14x |
"lot_tax_code", "lot_std_code", "MESURE", "CALCULE", "EXPERT", "PONCTUEL", |
| 993 | 14x |
"Effectif_total")] |
| 994 | 14x |
grdata <- rbind(grdata, data) |
| 995 |
} |
|
| 996 | 6x |
names(grdata) <- tolower(names(grdata)) |
| 997 | 6x |
grdata <- fun_date_extraction(grdata, nom_coldt = "debut_pas", annee = FALSE, |
| 998 | 6x |
mois = TRUE, quinzaine = TRUE, semaine = TRUE, jour_an = TRUE, jour_mois = FALSE, |
| 999 | 6x |
heure = FALSE) |
| 1000 | 6x |
annee = unique(strftime(as.POSIXlt(object@time.sequence), "%Y")) |
| 1001 | 6x |
dis_commentaire = paste(as.character(object@dc@dc_selected), collapse = ",") |
| 1002 | 6x |
grdata <- stacomirtools::chnames(grdata, c("ope_dic_identifiant", "lot_tax_code",
|
| 1003 | 6x |
"lot_std_code"), c("DC", "taxa", "stage"))
|
| 1004 | 6x |
grdata$DC <- as.factor(grdata$DC) |
| 1005 | 6x |
grdata$taxa <- as.factor(grdata$taxa) |
| 1006 | 6x |
return(grdata) |
| 1007 |
} |
|
| 1008 | ||
| 1009 | ||
| 1010 |
| 1 |
# Nom fichier : ref_env (classe) Date de creation : 02/01/2009 15:02:40 |
|
| 2 | ||
| 3 |
#' Class 'ref_env' |
|
| 4 |
#' |
|
| 5 |
#' Enables to load measure stations and to select one of them |
|
| 6 |
#' |
|
| 7 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 8 |
#' \code{new('ref_env', ...)}.
|
|
| 9 |
#' @slot dataframe Data concerning the |
|
| 10 |
#' measure station |
|
| 11 |
#' @slot env_selected The selected measure station |
|
| 12 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 13 |
#' @keywords classes |
|
| 14 |
setClass(Class = "ref_env", representation = |
|
| 15 |
representation(data = "data.frame", |
|
| 16 |
env_selected="character"), |
|
| 17 |
prototype = prototype( |
|
| 18 |
data = data.frame(), |
|
| 19 |
env_selected=character())) |
|
| 20 | ||
| 21 |
#' Loading method for ref_env referential object |
|
| 22 |
#' @return An S4 object of class ref_env with data loaded from the database |
|
| 23 |
#' @param object An object of class \link{ref_env-class}
|
|
| 24 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 25 |
#' @examples |
|
| 26 |
#' \dontrun{
|
|
| 27 |
#' object=new('ref_env')
|
|
| 28 |
#' charge(object) |
|
| 29 |
#' } |
|
| 30 |
setMethod("charge", signature = signature("ref_env"), definition = function(object) {
|
|
| 31 | 5x |
requete = new("RequeteDB")
|
| 32 | 5x |
requete@sql = paste("SELECT stm_identifiant, stm_libelle, stm_sta_code, stm_par_code, stm_description",
|
| 33 | 5x |
" FROM ", get_schema(), "tj_stationmesure_stm", " ORDER BY stm_identifiant;", |
| 34 | 5x |
sep = "") |
| 35 | 5x |
requete@silent = TRUE |
| 36 | 5x |
requete <- stacomirtools::query(requete) |
| 37 | 5x |
object@data <- requete@query |
| 38 | 5x |
return(object) |
| 39 |
}) |
|
| 40 | ||
| 41 | ||
| 42 | ||
| 43 |
#' Command line interface to select a monitoring station |
|
| 44 |
#' |
|
| 45 |
#' the choice_c method is intended to have the same behaviour as choice (which creates a |
|
| 46 |
#' widget in the graphical interface) but from the command line. |
|
| 47 |
#' @param object an object of class ref_env |
|
| 48 |
#' @param stationMesure a character vector of the monitoring station code (corresponds to stm_libelle in the tj_stationmesure_stm table) |
|
| 49 |
#' @return an object of class \link{ref_env-class} with the monitoring station selected
|
|
| 50 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 51 |
setMethod("choice_c", signature = signature("ref_env"), definition = function(object,
|
|
| 52 |
stationMesure) {
|
|
| 53 | 5x |
if (!inherits(stationMesure, "character")) {
|
| 54 | ! |
stop("the stationmesure should be of class character")
|
| 55 |
} |
|
| 56 | 5x |
if (length(stationMesure) == 0) {
|
| 57 | ! |
stop("Select at least one value\n")
|
| 58 |
} |
|
| 59 | 5x |
if (any(is.na(stationMesure))) {
|
| 60 | ! |
stop("NA values for stationmesure")
|
| 61 |
} |
|
| 62 |
# I can use the stm_libelle as there is a unique constraint in the table |
|
| 63 | 5x |
libellemanquants <- stationMesure[!stationMesure %in% object@data$stm_libelle] |
| 64 | 5x |
if (length(libellemanquants) > 0) |
| 65 | ! |
warning(gettextf("stationmesure code not present :\n %s", stringr::str_c(libellemanquants,
|
| 66 | ! |
collapse = ", "), domain = "R-stacomiR")) |
| 67 | 5x |
object@env_selected <- object@data$stm_libelle[object@data$stm_libelle %in% stationMesure] |
| 68 | 5x |
assign("ref_env", object, envir_stacomi)
|
| 69 | 5x |
return(object) |
| 70 |
}) |
| 1 |
#' class report_env simple output of one or several environmental |
|
| 2 |
#' conditions... |
|
| 3 |
#' |
|
| 4 |
#' Annual overview of environmental conditions. This class enables to draw some plot, but will mostly used to build |
|
| 5 |
#' joined graphs crossing the information from \link{report_mig_mult-class} and \link{report_mig_env-class}
|
|
| 6 |
#' |
|
| 7 |
#' @include ref_horodate.R |
|
| 8 |
#' @include ref_env.R |
|
| 9 |
#' @include create_generic.R |
|
| 10 |
#' @include utilities.R |
|
| 11 |
#' @slot horodatedebut \link{ref_horodate-class}
|
|
| 12 |
#' @slot horodatefin \link{ref_horodate-class}
|
|
| 13 |
#' @slot stationMesure \link{ref_env-class}
|
|
| 14 |
#' @slot data \code{data.frame}
|
|
| 15 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 16 |
#' @family report Objects |
|
| 17 |
#' @keywords classes |
|
| 18 |
#' @aliases report_env |
|
| 19 |
#' @keywords classes |
|
| 20 |
#' @example inst/examples/report_env-example.R |
|
| 21 |
#' @export |
|
| 22 |
setClass(Class = "report_env", representation = representation(stationMesure = "ref_env", |
|
| 23 |
horodatedebut = "ref_horodate", horodatefin = "ref_horodate", data = "data.frame"), |
|
| 24 |
prototype = prototype(horodatedebut = new("ref_horodate"), horodatefin = new("ref_horodate"),
|
|
| 25 |
stationMesure = new("ref_env"), data = data.frame()))
|
|
| 26 | ||
| 27 | ||
| 28 |
#' connect method for report_env class |
|
| 29 |
#' @param object An object of class \link{report_env-class}
|
|
| 30 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 31 |
#' @return An object of class \link{report_env-class} with slot data filled from the database
|
|
| 32 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 33 |
#' @aliases connect.report_env |
|
| 34 |
setMethod("connect", signature = signature("report_env"), definition = function(object,
|
|
| 35 |
silent = FALSE) {
|
|
| 36 |
# object<-r_env |
|
| 37 | 4x |
if (length(object@stationMesure@env_selected)==0) warning("No measure station selected")
|
| 38 | 4x |
stm_selected <- object@stationMesure@data[object@stationMesure@data$stm_libelle %in% object@stationMesure@env_selected,"stm_identifiant"] |
| 39 | ||
| 40 | 4x |
requete = new("RequeteDBwheredate")
|
| 41 | 4x |
requete@datedebut = strptime(object@horodatedebut@horodate, format = "%Y-%m-%d") |
| 42 | 4x |
requete@datefin = strptime(object@horodatefin@horodate, format = "%Y-%m-%d") |
| 43 | 4x |
requete@colonnedebut = "env_date_debut" |
| 44 | 4x |
requete@colonnefin = "env_date_fin" |
| 45 | 4x |
requete@select = paste("SELECT", " env_date_debut,", " env_date_fin,", " env_methode_obtention,",
|
| 46 | 4x |
" val_libelle as env_val_identifiant,", " env_valeur_quantitatif,", " env_stm_identifiant", |
| 47 | 4x |
" FROM ", get_schema(), "tj_conditionenvironnementale_env", |
| 48 | 4x |
" LEFT JOIN ref.tr_valeurparametrequalitatif_val on env_val_identifiant=val_identifiant", |
| 49 | 4x |
sep = "") |
| 50 | 4x |
requete@order_by <- "ORDER BY env_stm_identifiant, env_date_debut" |
| 51 | 4x |
tmp <- vector_to_listsql(stm_selected) |
| 52 | 4x |
requete@and = paste(" AND env_stm_identifiant IN ", tmp)
|
| 53 | 4x |
requete <- stacomirtools::query(requete) |
| 54 | 4x |
object@data <- stacomirtools::killfactor(stacomirtools::getquery(requete)) |
| 55 | 4x |
if (!silent) |
| 56 | ! |
funout(gettext("Environmental conditions loading query completed\n", domain = "R-stacomiR"))
|
| 57 | 4x |
return(object) |
| 58 |
}) |
|
| 59 |
#' command line interface for report_env class |
|
| 60 |
#' |
|
| 61 |
#' The choice_c method fills in the data slot for \link{ref_env-class} by runnning the charge method of this object.
|
|
| 62 |
#' It then runs the choice method on this object. It also applies the choice method for objects of class \link{ref_horodate-class}
|
|
| 63 |
#' @param object An object of class \link{report_env-class}
|
|
| 64 |
#' @param stationMesure A character, the code of the monitoring station, which records environmental parameters \link{choice_c,ref_env-method}
|
|
| 65 |
#' @param datedebut The starting date as a character, formats like \code{\%Y-\%m-\%d} or \code{\%d-\%m-\%Y} can be used as input
|
|
| 66 |
#' @param datefin The finishing date of the report, for this class this will be used to calculate the number of daily steps. |
|
| 67 |
#' @param silent Boolean default FALSE, if TRUE information messages not displayed. |
|
| 68 |
#' @return An object of class \link{report_env-class} with data selected
|
|
| 69 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 70 |
#' @aliases choice_c.report_env |
|
| 71 |
setMethod("choice_c", signature = signature("report_env"), definition = function(object,
|
|
| 72 |
stationMesure, datedebut, datefin, silent = FALSE) {
|
|
| 73 |
# code for debug using r_mig example |
|
| 74 |
# stationmesure=c('temp_gabion','coef_maree');datedebut='2008-01-01';datefin='2008-12-31';silent=FALSE
|
|
| 75 | 4x |
r_env <- object |
| 76 | 4x |
r_env@stationMesure = charge(r_env@stationMesure) |
| 77 |
# loads and verifies the stationmesure (selects the relevant lines in the |
|
| 78 |
# table |
|
| 79 | 4x |
r_env@stationMesure <- choice_c(object = r_env@stationMesure, stationMesure) |
| 80 | 4x |
r_env@horodatedebut <- choice_c(object = r_env@horodatedebut, nomassign = "report_env_date_debut", |
| 81 | 4x |
funoutlabel = gettext("Beginning date has been chosen\n", domain = "R-stacomiR"),
|
| 82 | 4x |
horodate = datedebut, silent = silent) |
| 83 | 4x |
r_env@horodatefin <- choice_c(r_env@horodatefin, nomassign = "report_env_date_fin", |
| 84 | 4x |
funoutlabel = gettext("Ending date has been chosen\n", domain = "R-stacomiR"),
|
| 85 | 4x |
horodate = datefin, silent = silent) |
| 86 | 4x |
return(r_env) |
| 87 |
}) |
|
| 88 |
#' charge method for report_env class |
|
| 89 |
#' @param object An object of class \link{report_env-class}
|
|
| 90 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 91 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 92 |
#' @aliases charge.report_env |
|
| 93 |
#' @return An object of class \link{report_env-class} with data set from values assigned in \code{envir_stacomi} environment
|
|
| 94 |
#' @keywords internal |
|
| 95 |
setMethod("charge", signature = signature("report_env"), definition = function(object,
|
|
| 96 |
silent) {
|
|
| 97 | ||
| 98 | 2x |
if (exists("ref_env", envir_stacomi)) {
|
| 99 | 2x |
object@stationMesure <- get("ref_env", envir_stacomi)
|
| 100 |
} else {
|
|
| 101 | ! |
funout(gettext("You need to choose a monitoring station, clic on validate\n",
|
| 102 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 103 |
} |
|
| 104 | ||
| 105 | 2x |
if (exists("report_env_date_debut", envir_stacomi)) {
|
| 106 | 2x |
object@horodatedebut@horodate <- get("report_env_date_debut", envir_stacomi)
|
| 107 |
} else {
|
|
| 108 | ! |
funout(gettext("You need to choose the starting date\n", domain = "R-stacomiR"),
|
| 109 | ! |
arret = TRUE) |
| 110 |
} |
|
| 111 | ||
| 112 | 2x |
if (exists("report_env_date_fin", envir_stacomi)) {
|
| 113 | 2x |
object@horodatefin@horodate <- get("report_env_date_fin", envir_stacomi)
|
| 114 |
} else {
|
|
| 115 | ! |
funout(gettext("You need to choose the ending date\n", domain = "R-stacomiR"),
|
| 116 | ! |
arret = TRUE) |
| 117 |
} |
|
| 118 | 2x |
return(object) |
| 119 |
}) |
|
| 120 | ||
| 121 |
#' Plot method for report_env |
|
| 122 |
#' @param x An object of class \link{report_env-class}
|
|
| 123 |
#' @param silent Stops displaying the messages |
|
| 124 |
#' @return Nothing, called for its side effect of plotting data |
|
| 125 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 126 |
#' @aliases plot.report_env |
|
| 127 |
#' @export |
|
| 128 |
setMethod("plot", signature(x = "report_env", y = "missing"), definition = function(x,
|
|
| 129 |
silent = FALSE) {
|
|
| 130 |
# le dataframe contenant le res de la requete |
|
| 131 | 2x |
r_env <- x |
| 132 | 2x |
dat <- r_env@data |
| 133 | 2x |
if (length(unique(dat$env_stm_identifiant)) != 0) {
|
| 134 |
# le layout pour l'affichage des graphiques |
|
| 135 | 2x |
vplayout <- function(x, y) {
|
| 136 | 4x |
grid::viewport(layout.pos.row = x, layout.pos.col = y) |
| 137 |
} |
|
| 138 | 2x |
grid::grid.newpage() |
| 139 | 2x |
grid::pushViewport(grid::viewport(layout = grid::grid.layout(length(unique(dat$env_stm_identifiant)), |
| 140 | 2x |
1, just = "center"))) |
| 141 | 2x |
lesGraphes = list() |
| 142 | 2x |
if (length(unique(dat$env_stm_identifiant)) != nrow(r_env@stationMesure@data)) {
|
| 143 | 2x |
funout(gettext("Some monitoring stations lack associated values (no environmental data)\n",
|
| 144 | 2x |
domain = "R-stacomiR")) |
| 145 |
} |
|
| 146 | ||
| 147 |
# for all stationmesure selected |
|
| 148 | 2x |
for (i in 1:length(unique(dat$env_stm_identifiant))) {
|
| 149 |
# the identifier of the current station |
|
| 150 | 4x |
stmidentifiant <- unique(dat$env_stm_identifiant)[i] |
| 151 | ||
| 152 |
# the line of report_env@stationMesure currently processed in the |
|
| 153 |
# loop |
|
| 154 | 4x |
stm <- r_env@stationMesure@data[r_env@stationMesure@data$stm_identifiant == |
| 155 | 4x |
stmidentifiant, ] |
| 156 | ||
| 157 |
# all measures for the selected station |
|
| 158 | 4x |
nameColonne <- as.character(stm$stm_libelle) |
| 159 | 4x |
datstm <- stacomirtools::chnames(dat, "env_valeur_quantitatif", nameColonne) |
| 160 | 4x |
datstm <- datstm[datstm$env_stm_identifiant == stmidentifiant, ] |
| 161 | ||
| 162 |
# creating the plot |
|
| 163 | 4x |
g <- ggplot(datstm, aes_string(x = "env_date_debut", y = nameColonne)) |
| 164 | 4x |
g <- g + geom_line(aes_string(colour = nameColonne)) + scale_y_continuous(stm$stm_libelle) + |
| 165 | 4x |
scale_x_datetime(name = "date") |
| 166 | ||
| 167 |
# printing plot on screen |
|
| 168 | 4x |
print(g, vp = vplayout(i, 1)) |
| 169 |
} |
|
| 170 |
} else {
|
|
| 171 | ! |
funout(gettext("No environmental conditions values for selected monitoring stations (report_env.R)\n",
|
| 172 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 173 |
} |
|
| 174 | 2x |
return(invisible(NULL)) |
| 175 |
}) |
| 1 |
#' Class 'ref_par' |
|
| 2 |
#' |
|
| 3 |
#' Class enabling to load the list of parameters and select one of them |
|
| 4 |
#' @include create_generic.R |
|
| 5 |
#' @slot data A data.frame |
|
| 6 |
#' @slot par_selected A character vector corresponding to par_code |
|
| 7 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 8 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 9 |
#' @keywords classes |
|
| 10 |
#' @slot data='data.frame' the list of parameters |
|
| 11 |
#' @family referential objects |
|
| 12 |
setClass(Class = "ref_par", representation = representation(data = "data.frame", |
|
| 13 |
par_selected = "character")) |
|
| 14 | ||
| 15 | ||
| 16 |
setValidity("ref_par", method = function(object) {
|
|
| 17 |
if (length(object@par_selected) != 0) {
|
|
| 18 |
if (nrow(object@data) > 0) {
|
|
| 19 |
concord <- object@par_selected %in% object@data$par_code |
|
| 20 |
if (any(!concord)) {
|
|
| 21 |
return(paste("No data for par", object@par_selected[!concord]))
|
|
| 22 | ||
| 23 |
} else {
|
|
| 24 |
return(TRUE) |
|
| 25 |
} |
|
| 26 |
} else {
|
|
| 27 |
return("You tried to set a value for par_selected without initializing the data slot")
|
|
| 28 |
} |
|
| 29 |
} else return(TRUE) |
|
| 30 | ||
| 31 |
}) |
|
| 32 |
#' Loading method for ref_par referential objects |
|
| 33 |
#' @aliases charge.ref_par |
|
| 34 |
#' @param object An object of class \link{ref_par-class}
|
|
| 35 |
#' @return An S4 object of class ref_par |
|
| 36 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 37 |
#' @return An S4 object of class \link{ref_par-class}
|
|
| 38 |
#' @examples |
|
| 39 |
#' \dontrun{
|
|
| 40 |
#' object=new('ref_par')
|
|
| 41 |
#' charge(object) |
|
| 42 |
#' } |
|
| 43 |
setMethod("charge", signature = signature("ref_par"), definition = function(object) {
|
|
| 44 | 1x |
requete = new("RequeteDB")
|
| 45 | 1x |
requete@sql = paste("SELECT par_code, par_nom, par_unite, par_nature, par_definition from ref.tg_parametre_par")
|
| 46 | 1x |
requete <- stacomirtools::query(requete) |
| 47 |
#funout(gettext("Loading parameters query completed\n", domain = "R-stacomiR"))
|
|
| 48 | 1x |
object@data <- requete@query |
| 49 | 1x |
return(object) |
| 50 |
}) |
|
| 51 | ||
| 52 | ||
| 53 |
#' Loading method for \code{ref_par referential} objects searching only those parameters existing for a DC, a Taxa, and a stage
|
|
| 54 |
#' @aliases charge_with_filter.ref_par |
|
| 55 |
#' @param object An object of class \link{ref_par-class}
|
|
| 56 |
#' @param dc_selected A counting device selected for the report |
|
| 57 |
#' @param taxa_selected The taxa selected for the report |
|
| 58 |
#' @param stage_selected The stage selected for the report |
|
| 59 |
#' @return An S4 object of class \link{ref_par-class}
|
|
| 60 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 61 |
#' @examples |
|
| 62 |
#' \dontrun{
|
|
| 63 |
#' object=new('ref_par')
|
|
| 64 |
#' charge_with_filter(object,dc_selected=6,taxa_selected=2038,stage_selected=c('AGJ','CIV'))
|
|
| 65 |
#' } |
|
| 66 |
setMethod("charge_with_filter", signature = signature("ref_par"), definition = function(object,
|
|
| 67 |
dc_selected, taxa_selected, stage_selected) {
|
|
| 68 | 10x |
requete = new("RequeteDBwhere")
|
| 69 | 10x |
requete@select = paste("SELECT DISTINCT ON (par_code) par_code, par_nom, par_unite, par_nature, par_definition", " FROM ",
|
| 70 | 10x |
get_schema(), "tg_dispositif_dis", " JOIN ", get_schema(), "t_dispositifcomptage_dic on dis_identifiant=dic_dis_identifiant", |
| 71 | 10x |
" JOIN ", get_schema(), "t_operation_ope on ope_dic_identifiant=dic_dis_identifiant", |
| 72 | 10x |
" JOIN ", get_schema(), "t_lot_lot on lot_ope_identifiant=ope_identifiant", |
| 73 | 10x |
" JOIN ", get_schema(), "tj_caracteristiquelot_car on car_lot_identifiant=lot_identifiant", |
| 74 | 10x |
" JOIN ref.tg_parametre_par on par_code=car_par_code", sep = "") |
| 75 | 10x |
requete@where = paste("where dis_identifiant in ", vector_to_listsql(dc_selected))
|
| 76 | 10x |
requete@and = paste("and lot_tax_code in", vector_to_listsql(taxa_selected),
|
| 77 | 10x |
" and lot_std_code in ", vector_to_listsql(stage_selected), sep = "") |
| 78 | 10x |
requete@order_by = "ORDER BY par_code" |
| 79 | 10x |
requete <- stacomirtools::query(requete) |
| 80 | 10x |
object@data <- requete@query |
| 81 | 10x |
if (nrow(object@data) == 0) |
| 82 | ! |
funout(gettext("No data for selected device, taxa and stage\n", domain = "R-stacomiR"),
|
| 83 | ! |
arret = TRUE) |
| 84 | 10x |
return(object) |
| 85 |
}) |
|
| 86 | ||
| 87 | ||
| 88 |
#' Command line interface to select a parameter |
|
| 89 |
#' |
|
| 90 |
#' @aliases choice_c.ref_par |
|
| 91 |
#' @param object an object of class \link{ref_par-class}
|
|
| 92 |
#' @param par A character vector of par |
|
| 93 |
#' @param silent Default FALSE but not used there |
|
| 94 |
#' @return An object of class \link{ref_par-class}
|
|
| 95 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 96 |
setMethod("choice_c", signature = signature("ref_par"), definition = function(object,
|
|
| 97 |
par, silent = FALSE) {
|
|
| 98 | 19x |
if (inherits(par , "numeric")) {
|
| 99 | ! |
par <- as.character(par) |
| 100 |
} |
|
| 101 | 19x |
if (any(is.na(par))) |
| 102 | ! |
stop("NA values par")
|
| 103 | 19x |
object@par_selected <- par |
| 104 | 19x |
if (nrow(object@data) == 0) {
|
| 105 | ! |
stop("Internal error : tried to set a value for par_selected without initializing the data slot")
|
| 106 |
} |
|
| 107 |
# validObject(object,test=FALSE) here I don't want to generate an error if |
|
| 108 |
# parm is not present so I'm not using the validObject which will throw and |
|
| 109 |
# error |
|
| 110 | 19x |
concord <- object@par_selected %in% object@data$par_code |
| 111 | ||
| 112 | 19x |
if (any(!concord)) {
|
| 113 | 8x |
warning(paste(gettextf("No data for par %s", object@par_selected[!concord],
|
| 114 | 8x |
domain = "R-stacomiR"))) |
| 115 |
} |
|
| 116 |
# to work with daughter class |
|
| 117 | 19x |
if (inherits(object, "ref_parquan")) {
|
| 118 | 8x |
assign("ref_parquan", object, envir = envir_stacomi)
|
| 119 | 19x |
} else if (inherits(object, "ref_parqual")){
|
| 120 | 1x |
assign("ref_parqual", object, envir = envir_stacomi)
|
| 121 |
} else {
|
|
| 122 | 10x |
assign("ref_par", object, envir = envir_stacomi)
|
| 123 |
} |
|
| 124 | ||
| 125 | 19x |
return(object) |
| 126 |
}) |
|
| 127 | ||
| 128 | ||
| 129 |
| 1 |
#' Graph function for glass eel migration. Differs from fungraph as it does not |
|
| 2 |
#' draw the ggplot graph for month |
|
| 3 |
#' |
|
| 4 |
#' This graph will also plot numbers and bars according to whether the glass |
|
| 5 |
#' eel have been counted through weight or numbers |
|
| 6 |
#' |
|
| 7 |
#' |
|
| 8 |
#' @param report_mig an object of class \link{report_mig-class} or an
|
|
| 9 |
#' object of class \link{report_mig_mult-class}
|
|
| 10 |
#' @param table a data frame with the results |
|
| 11 |
#' @param time.sequence a vector POSIXt |
|
| 12 |
#' @param taxa the species |
|
| 13 |
#' @param stage the stage |
|
| 14 |
#' @param dc the counting device, default to null, only necessary for \link{report_mig_mult-class}
|
|
| 15 |
#' @param silent Message displayed or not |
|
| 16 |
#' @param color Default NULL, a vector of length 11 of color in the following order, numbers, weight, working, stopped, 1...5 types of operation, |
|
| 17 |
#' the 2 latest colors are not used but kept for consistency with fungraph |
|
| 18 |
#' for the fishway, if null will be set to brewer.pal(12,"Paired")[c(4,6,1,2,3,5,7,8,10,11,12)] |
|
| 19 |
#' @param color_ope Default NULL, a vector of color for the operations. Default to brewer.pal(4,"Paired") |
|
| 20 |
#' @param ... additional parameters passed to plot, main, ylab, cex.main, font.main, type, xlim, ylim, lty, bty, pch |
|
| 21 |
#' it is not possible to change xlim |
|
| 22 |
#' @return No return value, called for side effects |
|
| 23 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 24 |
fungraph_glasseel = function(report_mig, |
|
| 25 |
table, |
|
| 26 |
time.sequence, |
|
| 27 |
taxa, |
|
| 28 |
stage, |
|
| 29 |
dc = null, |
|
| 30 |
silent, |
|
| 31 |
color = NULL, |
|
| 32 |
color_ope = NULL, |
|
| 33 |
...) {
|
|
| 34 | 2x |
oldpar <- par(no.readonly = TRUE) |
| 35 | 2x |
on.exit(par(oldpar)) |
| 36 |
# color=null |
|
| 37 |
# color calculation |
|
| 38 | 2x |
if (is.null(color)) {
|
| 39 | 2x |
tp <- RColorBrewer::brewer.pal(12, "Paired") |
| 40 | 2x |
mypalette = c( |
| 41 | 2x |
"working" = tp[4], |
| 42 |
# green |
|
| 43 | 2x |
"stopped" = tp[6], |
| 44 |
# red |
|
| 45 | 2x |
"Fonc normal" = tp[1], |
| 46 | 2x |
"Arr ponctuel" = tp[2], |
| 47 | 2x |
"Arr maintenance" = tp[3], |
| 48 | 2x |
"Dysfonc" = tp[5], |
| 49 | 2x |
"Non connu" = tp[7], |
| 50 | 2x |
"eff" = tp[8], |
| 51 |
#orange |
|
| 52 | 2x |
"weight" = tp[10], |
| 53 |
#purple |
|
| 54 | 2x |
"unused1" = tp[11], |
| 55 | 2x |
"unused1" = tp[12] |
| 56 |
) |
|
| 57 |
} else {
|
|
| 58 | ! |
if (length(color) != 11) |
| 59 | ! |
stop("The length of color must be 11")
|
| 60 | ! |
mypalette = c( |
| 61 | ! |
"working" = color[1], |
| 62 | ! |
"stopped" = color[2], |
| 63 | ! |
"Fonc normal" = color[3], |
| 64 | ! |
"Arr ponctuel" = color[4], |
| 65 | ! |
"Arr maintenance" = color[5], |
| 66 | ! |
"Dysfonc" = color[6], |
| 67 | ! |
"Non connu" = color[7], |
| 68 | ! |
"eff" = color[8], |
| 69 | ! |
"weight" = color[9], |
| 70 | ! |
"unused1" = color[10], |
| 71 | ! |
"unused2" = color[11] |
| 72 |
) |
|
| 73 |
} |
|
| 74 |
|
|
| 75 |
|
|
| 76 | 2x |
if (is.null(color_ope)) {
|
| 77 | 2x |
if (stacomirtools::is.odd(dc)) |
| 78 | 2x |
brew = "Paired" |
| 79 |
else |
|
| 80 | 2x |
brew = "Accent" |
| 81 | 2x |
color_ope = RColorBrewer::brewer.pal(8, brew) |
| 82 |
} |
|
| 83 |
|
|
| 84 | 2x |
if (is.null(dc)) |
| 85 | 2x |
dc = report_mig@dc@dc_selected[1] |
| 86 | 2x |
annee = paste(unique(strftime(as.POSIXlt(time.sequence), "%Y")), collapse = |
| 87 |
",") |
|
| 88 | 2x |
mois = months(time.sequence) |
| 89 | 2x |
jour = strftime(as.POSIXlt(time.sequence), "%j") |
| 90 | 2x |
index = table$No.pas + 1 |
| 91 | 2x |
eff = table$Effectif_total |
| 92 | 2x |
eff.p = table$Effectif_total.p |
| 93 | 2x |
debut = unclass(as.Date(time.sequence[min(index)]))[[1]] |
| 94 | 2x |
fin = unclass(as.Date(time.sequence[max(index)]))[[1]] |
| 95 | 2x |
eff[eff == 0] <- NA #for graph need |
| 96 | 2x |
eff.p[eff.p == 0] <- NA |
| 97 | 2x |
dis_commentaire = as.character(report_mig@dc@data$dis_commentaires[report_mig@dc@data$dc %in% |
| 98 | 2x |
dc]) |
| 99 | 2x |
if (!silent) |
| 100 | 2x |
funout(gettextf("Glass eels graph %s\n", dis_commentaire))
|
| 101 |
################################### |
|
| 102 |
# Graph annuel couvrant sequence >0 |
|
| 103 |
#################################### |
|
| 104 |
|
|
| 105 | 2x |
vec <- c(rep(1, 15), rep(2, 2), rep(3, 2), 4, rep(5, 6)) |
| 106 | 2x |
mat <- matrix(vec, length(vec), 1) |
| 107 | 2x |
layout(mat) |
| 108 |
#par("bg"=grDevices::gray(0.8))
|
|
| 109 | 2x |
graphics::par("mar" = c(3, 4, 3, 2) + 0.1)
|
| 110 | 2x |
dots <- list(...) |
| 111 | 2x |
if (!"main" %in% names(dots)) |
| 112 | 2x |
main = gettextf("Glass eels graph %s, %s, %s, %s",
|
| 113 | 2x |
dis_commentaire, |
| 114 | 2x |
taxa, |
| 115 | 2x |
stage, |
| 116 | 2x |
annee, |
| 117 | 2x |
domain = "R-stacomiR") |
| 118 |
else |
|
| 119 | 2x |
main = dots[["main"]] |
| 120 | 2x |
if (!"ylab" %in% names(dots)) |
| 121 | 2x |
ylab = gettext("Number of glass eels (x1000)", domain = "R-stacomiR")
|
| 122 |
else |
|
| 123 | 2x |
ylab = dots[["ylab"]] |
| 124 | 2x |
if (!"cex.main" %in% names(dots)) |
| 125 | 2x |
cex.main = 1 |
| 126 |
else |
|
| 127 | 2x |
cex.main = dots[["cex.main"]] |
| 128 | 2x |
if (!"font.main" %in% names(dots)) |
| 129 | 2x |
font.main = 1 |
| 130 |
else |
|
| 131 | 2x |
font.main = dots[["font.main"]] |
| 132 | 2x |
if (!"type" %in% names(dots)) |
| 133 | 2x |
type = "h" |
| 134 |
else |
|
| 135 | 2x |
type = dots[["type"]] |
| 136 | 2x |
if (!"xlim" %in% names(dots)) |
| 137 | 2x |
xlim = c(debut, fin) |
| 138 |
else |
|
| 139 | 2x |
xlim = dots[["xlim"]] |
| 140 | 2x |
if (!"ylim" %in% names(dots)) |
| 141 | 2x |
ylim = c(0, max(eff / 1000, na.rm = TRUE)) * 1.2 |
| 142 |
else |
|
| 143 | 2x |
xlim = c(debut, fin)#dots[["xlim"]] # currently this argument is ignored |
| 144 | 2x |
if (!"cex" %in% names(dots)) |
| 145 | 2x |
cex = 1 |
| 146 |
else |
|
| 147 | 2x |
cex = dots[["cex"]] |
| 148 | 2x |
if (!"lty" %in% names(dots)) |
| 149 | 2x |
lty = 1 |
| 150 |
else |
|
| 151 | 2x |
lty = dots[["lty"]] |
| 152 | 2x |
if (!"pch" %in% names(dots)) |
| 153 | 2x |
pch = 16 |
| 154 |
else |
|
| 155 | 2x |
pch = dots[["pch"]] |
| 156 | 2x |
if (!"bty" %in% names(dots)) |
| 157 | 2x |
bty = "l" |
| 158 |
else |
|
| 159 | 2x |
bty = dots[["bty"]] |
| 160 | 2x |
plot( |
| 161 | 2x |
x = as.Date(time.sequence, "Europe/Paris"), |
| 162 | 2x |
y = eff / 1000, |
| 163 | 2x |
col = mypalette["eff"], |
| 164 | 2x |
type = type, |
| 165 | 2x |
xlim = xlim, |
| 166 | 2x |
ylim = ylim, |
| 167 | 2x |
lty = lty, |
| 168 | 2x |
xaxt = "n", |
| 169 | 2x |
ylab = ylab, |
| 170 |
#xlab="date", |
|
| 171 | 2x |
cex.main = cex.main, |
| 172 | 2x |
font.main = font.main, |
| 173 | 2x |
main = main, |
| 174 | 2x |
cex = cex, |
| 175 | 2x |
pch = pch, |
| 176 | 2x |
bty = bty |
| 177 |
) |
|
| 178 |
#print(plot,position = c(0, .3, 1, .9), more = TRUE) |
|
| 179 | 2x |
r <- as.Date(round(range(time.sequence), "day")) |
| 180 | 2x |
axis.Date(1, at = seq(r[1], r[2], by = "weeks"), format = "%d-%b") |
| 181 |
|
|
| 182 | 2x |
points( |
| 183 | 2x |
as.Date(time.sequence, "Europe/Paris"), |
| 184 | 2x |
eff.p / 1000, |
| 185 | 2x |
type = type, |
| 186 | 2x |
lty = lty, |
| 187 | 2x |
col = mypalette["weight"] |
| 188 |
) |
|
| 189 |
|
|
| 190 | 2x |
legend( |
| 191 | 2x |
x = "topright", |
| 192 | 2x |
inset = 0.01, |
| 193 | 2x |
legend = gettext("weighted", "counted", domain = "R-stacomiR"),
|
| 194 | 2x |
pch = c(16, 16), |
| 195 | 2x |
col = mypalette[c("weight", "eff")]
|
| 196 |
) |
|
| 197 |
###################################### |
|
| 198 |
# text labels for numbers and weights |
|
| 199 |
###################################### |
|
| 200 | 2x |
text( |
| 201 | 2x |
x = debut + (fin - debut) / 8, |
| 202 | 2x |
y = max(eff / 1000, na.rm = TRUE) * 1.15, |
| 203 | 2x |
labels = paste(round( |
| 204 | 2x |
sum(table$poids_depuis_effectifs, na.rm = TRUE) / 1000, 2 |
| 205 | 2x |
), " kg"), |
| 206 | 2x |
col = mypalette["eff"], |
| 207 | 2x |
adj = 1, |
| 208 | 2x |
cex = cex |
| 209 |
) |
|
| 210 | 2x |
text( |
| 211 | 2x |
x = debut + 3 * (fin - debut) / 8 , |
| 212 | 2x |
y = max(eff / 1000, na.rm = TRUE) * 1.15, |
| 213 | 2x |
labels = paste("N=", round(
|
| 214 | 2x |
sum(table$Effectif_total.e, na.rm = TRUE) |
| 215 |
)), |
|
| 216 | 2x |
col = mypalette["eff"], |
| 217 | 2x |
adj = 1, |
| 218 | 2x |
cex = cex |
| 219 |
) |
|
| 220 | 2x |
text( |
| 221 | 2x |
x = debut + (fin - debut) / 8, |
| 222 | 2x |
y = max(eff / 1000, na.rm = TRUE) * 1.2, |
| 223 | 2x |
labels = paste(round( |
| 224 | 2x |
sum(table$Poids_total, na.rm = TRUE) / 1000, 2 |
| 225 | 2x |
), " kg"), |
| 226 | 2x |
col = mypalette["weight"], |
| 227 | 2x |
adj = 1, |
| 228 | 2x |
cex = cex |
| 229 |
) |
|
| 230 | 2x |
text( |
| 231 | 2x |
x = debut + 3 * (fin - debut) / 8, |
| 232 | 2x |
y = max(eff / 1000, na.rm = TRUE) * 1.2, |
| 233 | 2x |
labels = paste("N=", round(sum(eff.p, na.rm = TRUE))),
|
| 234 | 2x |
col = mypalette["weight"], |
| 235 | 2x |
adj = 1, |
| 236 | 2x |
cex = cex |
| 237 |
) |
|
| 238 | 2x |
text( |
| 239 | 2x |
x = debut + 3 + (fin - debut) / 8, |
| 240 | 2x |
y = max(eff / 1000, na.rm = TRUE) * 1.1, |
| 241 | 2x |
labels = paste(round( |
| 242 | 2x |
sum(table$Poids_total, table$poids_depuis_effectifs, na.rm = TRUE) / 1000, |
| 243 | 2x |
2 |
| 244 | 2x |
), " kg"), |
| 245 | 2x |
col = "black", |
| 246 | 2x |
adj = 1, |
| 247 | 2x |
cex = cex |
| 248 |
) |
|
| 249 | 2x |
text( |
| 250 | 2x |
x = debut + 3 * (fin - debut) / 8, |
| 251 | 2x |
y = max(eff / 1000, na.rm = TRUE) * 1.1, |
| 252 | 2x |
labels = paste("N=", round(sum(eff, na.rm = TRUE))),
|
| 253 | 2x |
col = "black", |
| 254 | 2x |
adj = 1, |
| 255 | 2x |
cex = cex |
| 256 |
) |
|
| 257 | 2x |
segments( |
| 258 | 2x |
x0 = debut, |
| 259 | 2x |
y0 = max(eff / 1000, na.rm = TRUE) * 1.125, |
| 260 | 2x |
x1 = debut + 3 * (fin - debut) / 8, |
| 261 | 2x |
y1 = max(eff / 1000, na.rm = TRUE) * 1.125 |
| 262 |
) |
|
| 263 |
|
|
| 264 |
|
|
| 265 | 2x |
report_ope <- get("report_ope", envir = envir_stacomi)
|
| 266 | 2x |
t_operation_ope <- |
| 267 | 2x |
report_ope@data[report_ope@data$ope_dic_identifiant == dc, ] |
| 268 | 2x |
dif = difftime(t_operation_ope$ope_date_fin, |
| 269 | 2x |
t_operation_ope$ope_date_debut, |
| 270 | 2x |
units = "days") |
| 271 |
|
|
| 272 | 2x |
if (!silent) {
|
| 273 | ! |
funout(gettextf( |
| 274 | ! |
"number of operations =%s\n", |
| 275 | ! |
nrow(t_operation_ope), |
| 276 | ! |
domain = "R-stacomiR" |
| 277 |
)) |
|
| 278 | ! |
funout(gettextf("average trapping time = %sdays\n", round(mean(
|
| 279 | ! |
as.numeric(dif) |
| 280 | ! |
), 2), domain = "R-stacomiR")) |
| 281 | ! |
funout(gettextf("maximum term = %sdays\n", round(max(
|
| 282 | ! |
as.numeric(dif) |
| 283 | ! |
), 2), domain = "R-stacomiR")) |
| 284 | ! |
funout(gettextf("minimum term = %sdays\n", round(min(
|
| 285 | ! |
as.numeric(dif) |
| 286 | ! |
), 2), domain = "R-stacomiR")) |
| 287 |
} |
|
| 288 |
|
|
| 289 | 2x |
df <- report_mig@dc@data$df[report_mig@dc@data$dc == dc] |
| 290 | 2x |
report_df <- get("report_df", envir = envir_stacomi)
|
| 291 | 2x |
report_dc <- get("report_dc", envir = envir_stacomi)
|
| 292 | 2x |
report_df@data <- |
| 293 | 2x |
report_df@data[report_df@data$per_dis_identifiant == df, ] |
| 294 | 2x |
report_dc@data <- |
| 295 | 2x |
report_dc@data[report_dc@data$per_dis_identifiant == dc, ] |
| 296 |
|
|
| 297 |
|
|
| 298 | 2x |
graphdate <- function(vectordate) {
|
| 299 | 40x |
attributes(vectordate) <- NULL |
| 300 | 40x |
unclass(vectordate) |
| 301 |
} |
|
| 302 |
|
|
| 303 |
|
|
| 304 |
################################### |
|
| 305 |
# creation d'un graphique vide (2) |
|
| 306 |
################################### |
|
| 307 |
|
|
| 308 | 2x |
graphics::par("mar" = c(0, 4, 0, 2) + 0.1)
|
| 309 | 2x |
plot( |
| 310 | 2x |
as.Date(time.sequence), |
| 311 | 2x |
seq(0, 3, length.out = length(eff)), |
| 312 | 2x |
xlim = xlim, |
| 313 | 2x |
type = "n", |
| 314 | 2x |
xlab = "", |
| 315 | 2x |
xaxt = "n", |
| 316 | 2x |
yaxt = "n", |
| 317 | 2x |
ylab = gettext("Fishway", domain = "R-stacomiR"),
|
| 318 | 2x |
bty = "n", |
| 319 | 2x |
cex = cex + 0.2 |
| 320 |
) |
|
| 321 |
|
|
| 322 |
################################### |
|
| 323 |
# Time for dc operation |
|
| 324 |
################################### |
|
| 325 |
|
|
| 326 | 2x |
if (dim(report_df@data)[1] == 0) {
|
| 327 | ! |
rect( |
| 328 | ! |
xleft = debut, |
| 329 | ! |
ybottom = 2.1, |
| 330 | ! |
xright = fin, |
| 331 | ! |
ytop = 3, |
| 332 | ! |
col = "grey", |
| 333 | ! |
border = NA, |
| 334 | ! |
lwd = 1 |
| 335 |
) |
|
| 336 | ! |
rect( |
| 337 | ! |
xleft = debut, |
| 338 | ! |
ybottom = 1.1, |
| 339 | ! |
xright = fin, |
| 340 | ! |
ytop = 2, |
| 341 | ! |
col = "grey40", |
| 342 | ! |
border = NA, |
| 343 | ! |
lwd = 1 |
| 344 |
) |
|
| 345 | ! |
legend( |
| 346 | ! |
x = "bottom", |
| 347 | ! |
legend = gettext("Unknown working", "Unknow operation type", domain =
|
| 348 | ! |
"R-stacomiR"), |
| 349 | ! |
pch = c(16, 16), |
| 350 | ! |
col = c("grey", "grey40"),
|
| 351 | ! |
horiz = TRUE, |
| 352 | ! |
bty = "n" |
| 353 |
) |
|
| 354 |
|
|
| 355 |
|
|
| 356 |
} else {
|
|
| 357 |
# si il sort quelque chose |
|
| 358 | 2x |
if (sum(report_df@data$per_etat_fonctionnement == 1) > 0) {
|
| 359 | 2x |
rect( |
| 360 | 2x |
xleft = graphdate(as.Date(report_df@data$per_date_debut[report_df@data$per_etat_fonctionnement == |
| 361 | 2x |
1])), |
| 362 | 2x |
ybottom = 2.1, |
| 363 | 2x |
xright = graphdate(as.Date(report_df@data$per_date_fin[report_df@data$per_etat_fonctionnement == |
| 364 | 2x |
1])), |
| 365 | 2x |
ytop = 3, |
| 366 | 2x |
col = mypalette["working"], |
| 367 | 2x |
border = NA, |
| 368 | 2x |
lwd = 1 |
| 369 |
) |
|
| 370 |
} |
|
| 371 | 2x |
if (sum(report_df@data$per_etat_fonctionnement == 0) > 0) {
|
| 372 | 2x |
rect( |
| 373 | 2x |
xleft = graphdate(as.Date(report_df@data$per_date_debut[report_df@data$per_etat_fonctionnement == |
| 374 | 2x |
0])), |
| 375 | 2x |
ybottom = 2.1, |
| 376 | 2x |
xright = graphdate(as.Date(report_df@data$per_date_fin[report_df@data$per_etat_fonctionnement == |
| 377 | 2x |
0])), |
| 378 | 2x |
ytop = 3, |
| 379 | 2x |
col = mypalette["stopped"], |
| 380 | 2x |
border = NA, |
| 381 | 2x |
lwd = 1 |
| 382 |
) |
|
| 383 |
} |
|
| 384 |
#creation d'une liste par categorie d'arret contenant vecteurs dates |
|
| 385 | 2x |
listeperiode <- |
| 386 | 2x |
fun_table_per_dis( |
| 387 | 2x |
typeperiode = report_df@data$per_tar_code, |
| 388 | 2x |
tempsdebut = report_df@data$per_date_debut, |
| 389 | 2x |
tempsfin = report_df@data$per_date_fin, |
| 390 | 2x |
libelle = report_df@data$libelle, |
| 391 | 2x |
color= mypalette[report_df@data$libelle] |
| 392 |
) |
|
| 393 | 2x |
nomperiode <- vector() |
| 394 | 2x |
color_periodes <- vector() |
| 395 | 2x |
for (j in 1:length(listeperiode)) {
|
| 396 | 5x |
nomperiode[j] <- substr(listeperiode[[j]]$nom, 1, 17) |
| 397 |
#ecriture pour chaque type de periode |
|
| 398 | 5x |
color_periode = listeperiode[[j]]$color |
| 399 | 5x |
rect( |
| 400 | 5x |
xleft = graphdate(listeperiode[[j]]$debut), |
| 401 | 5x |
ybottom = 1.1, |
| 402 | 5x |
xright = graphdate(listeperiode[[j]]$fin), |
| 403 | 5x |
ytop = 2, |
| 404 | 5x |
col = color_periode, |
| 405 | 5x |
border = NA, |
| 406 | 5x |
lwd = 1 |
| 407 |
) |
|
| 408 | 5x |
color_periodes <- c(color_periodes, color_periode) |
| 409 |
} |
|
| 410 |
# below the colors for operation are from 4 to 3+ntypeoperation |
|
| 411 | 2x |
legend ( |
| 412 | 2x |
x = debut, |
| 413 | 2x |
y = 1.2, |
| 414 | 2x |
legend = gettext("working", "stopped", nomperiode, domain = "R-stacomiR"),
|
| 415 | 2x |
pch = c(15, 15), |
| 416 | 2x |
col = c(mypalette["working"], mypalette["stopped"], color_periodes), |
| 417 | 2x |
bty = "n", |
| 418 | 2x |
ncol = length(listeperiode) + 2, |
| 419 | 2x |
text.width = (fin - debut) / 10 |
| 420 |
) |
|
| 421 |
} |
|
| 422 |
|
|
| 423 |
################################### |
|
| 424 |
# creation d'un graphique vide (3=DC) |
|
| 425 |
################################### |
|
| 426 |
|
|
| 427 |
|
|
| 428 | 2x |
graphics::par("mar" = c(0, 4, 0, 2) + 0.1)
|
| 429 | 2x |
plot( |
| 430 | 2x |
as.Date(time.sequence), |
| 431 | 2x |
seq(0, 3, length.out = length(eff)), |
| 432 | 2x |
xlim = xlim, |
| 433 | 2x |
type = "n", |
| 434 | 2x |
xlab = "", |
| 435 | 2x |
xaxt = "n", |
| 436 | 2x |
yaxt = "n", |
| 437 | 2x |
ylab = gettext("CD", domain = "R-stacomiR"),
|
| 438 | 2x |
bty = "n", |
| 439 | 2x |
cex = cex + 0.2 |
| 440 |
) |
|
| 441 |
################################### |
|
| 442 |
# time for DC (counting device) operation |
|
| 443 |
################################### |
|
| 444 |
|
|
| 445 |
|
|
| 446 | 2x |
if (dim(report_dc@data)[1] == 0) {
|
| 447 | ! |
rect( |
| 448 | ! |
xleft = debut, |
| 449 | ! |
ybottom = 2.1, |
| 450 | ! |
xright = fin, |
| 451 | ! |
ytop = 3, |
| 452 | ! |
col = "grey", |
| 453 | ! |
border = NA, |
| 454 | ! |
lwd = 1 |
| 455 |
) |
|
| 456 |
|
|
| 457 | ! |
rect( |
| 458 | ! |
xleft = debut, |
| 459 | ! |
ybottom = 1.1, |
| 460 | ! |
xright = fin, |
| 461 | ! |
ytop = 2, |
| 462 | ! |
col = "grey40", |
| 463 | ! |
border = NA, |
| 464 | ! |
lwd = 1 |
| 465 |
) |
|
| 466 | ! |
legend( |
| 467 | ! |
x = "bottom", |
| 468 | ! |
legend = gettext("Unknown working", "Unknow operation type", domain =
|
| 469 | ! |
"R-stacomiR"), |
| 470 | ! |
pch = c(16, 16), |
| 471 | ! |
col = c("grey", "grey40"),
|
| 472 | ! |
horiz = TRUE, |
| 473 |
#ncol=5, |
|
| 474 | ! |
bty = "n" |
| 475 |
) |
|
| 476 |
|
|
| 477 |
|
|
| 478 |
} else {
|
|
| 479 | 2x |
if (sum(report_dc@data$per_etat_fonctionnement == 1) > 0) {
|
| 480 | 2x |
rect( |
| 481 | 2x |
xleft = graphdate(as.Date(report_dc@data$per_date_debut[report_dc@data$per_etat_fonctionnement == |
| 482 | 2x |
1])), |
| 483 | 2x |
ybottom = 2.1, |
| 484 | 2x |
xright = graphdate(as.Date(report_dc@data$per_date_fin[report_dc@data$per_etat_fonctionnement == |
| 485 | 2x |
1])), |
| 486 | 2x |
ytop = 3, |
| 487 | 2x |
col = mypalette["working"], |
| 488 | 2x |
border = NA, |
| 489 | 2x |
lwd = 1 |
| 490 |
) |
|
| 491 |
} |
|
| 492 | 2x |
if (sum(report_dc@data$per_etat_fonctionnement == 0) > 0) |
| 493 |
{
|
|
| 494 | 2x |
rect( |
| 495 | 2x |
xleft = graphdate(as.Date(report_dc@data$per_date_debut[report_dc@data$per_etat_fonctionnement == |
| 496 | 2x |
0])), |
| 497 | 2x |
ybottom = 2.1, |
| 498 | 2x |
xright = graphdate(as.Date(report_dc@data$per_date_fin[report_dc@data$per_etat_fonctionnement == |
| 499 | 2x |
0])), |
| 500 | 2x |
ytop = 3, |
| 501 | 2x |
col = mypalette["stopped"], |
| 502 | 2x |
border = NA, |
| 503 | 2x |
lwd = 1 |
| 504 |
) |
|
| 505 |
} |
|
| 506 | 2x |
listeperiode <- |
| 507 | 2x |
fun_table_per_dis( |
| 508 | 2x |
typeperiode = report_dc@data$per_tar_code, |
| 509 | 2x |
tempsdebut = report_dc@data$per_date_debut, |
| 510 | 2x |
tempsfin = report_dc@data$per_date_fin, |
| 511 | 2x |
libelle = report_dc@data$libelle, |
| 512 | 2x |
color= mypalette[report_df@data$libelle] |
| 513 |
) |
|
| 514 | 2x |
nomperiode <- vector() |
| 515 | 2x |
color_periodes <- vector() |
| 516 | 2x |
for (j in 1:length(listeperiode)) {
|
| 517 | 5x |
nomperiode[j] <- substr(listeperiode[[j]]$nom, 1, 17) |
| 518 | 5x |
color_periode = listeperiode[[j]]$color |
| 519 | 5x |
rect( |
| 520 | 5x |
xleft = graphdate(listeperiode[[j]]$debut), |
| 521 | 5x |
ybottom = 1.1, |
| 522 | 5x |
xright = graphdate(listeperiode[[j]]$fin), |
| 523 | 5x |
ytop = 2, |
| 524 | 5x |
col = color_periode, |
| 525 | 5x |
border = NA, |
| 526 | 5x |
lwd = 1 |
| 527 |
) |
|
| 528 | 5x |
color_periodes <- c(color_periodes, color_periode) |
| 529 |
} |
|
| 530 |
|
|
| 531 | 2x |
legend ( |
| 532 | 2x |
x = debut, |
| 533 | 2x |
y = 1.2, |
| 534 | 2x |
legend = gettext("working", "stopped", nomperiode, domain = "R-stacomiR"),
|
| 535 | 2x |
pch = c(15, 15), |
| 536 | 2x |
col = c(mypalette["working"], mypalette["stopped"], color_periodes), |
| 537 | 2x |
bty = "n", |
| 538 | 2x |
ncol = length(listeperiode) + 2, |
| 539 | 2x |
text.width = (fin - debut) / 10 |
| 540 |
) |
|
| 541 |
} |
|
| 542 |
|
|
| 543 |
################################### |
|
| 544 |
# creation d'un graphique vide (4=OP) |
|
| 545 |
################################### |
|
| 546 |
|
|
| 547 |
|
|
| 548 | 2x |
graphics::par("mar" = c(0, 4, 0, 2) + 0.1)
|
| 549 | 2x |
plot( |
| 550 | 2x |
as.Date(time.sequence), |
| 551 | 2x |
seq(0, 1, length.out = length(eff)), |
| 552 | 2x |
xlim = xlim, |
| 553 | 2x |
type = "n", |
| 554 | 2x |
xlab = "", |
| 555 | 2x |
xaxt = "n", |
| 556 | 2x |
yaxt = "n", |
| 557 | 2x |
ylab = gettext("Op", domain = "R-stacomiR"),
|
| 558 | 2x |
bty = "n", |
| 559 | 2x |
cex = cex + 0.2 |
| 560 |
) |
|
| 561 |
################################### |
|
| 562 |
# operations |
|
| 563 |
################################### |
|
| 564 | 2x |
rect( |
| 565 | 2x |
xleft = graphdate(as.Date(t_operation_ope$ope_date_debut)), |
| 566 | 2x |
ybottom = 0, |
| 567 | 2x |
xright = graphdate(as.Date(t_operation_ope$ope_date_fin)), |
| 568 | 2x |
ytop = 1, |
| 569 | 2x |
col = color_ope, |
| 570 | 2x |
border = NA, |
| 571 | 2x |
lwd = 1 |
| 572 |
) |
|
| 573 |
|
|
| 574 |
|
|
| 575 |
################################### |
|
| 576 |
# Graph mensuel |
|
| 577 |
#################################### |
|
| 578 | 2x |
graphics::par("mar" = c(4, 4, 1, 2) + 0.1)
|
| 579 | 2x |
petitmois = substr(as.character(mois), 1, 3) |
| 580 | 2x |
effmois = tapply(eff, mois, sum, na.rm = TRUE)[c(5, 4, 9, 2, 8, 7, 6, 1, 12, 11, 10, 3)] |
| 581 | 2x |
effmois.p = tapply(eff.p, mois, sum, na.rm = TRUE)[c(5, 4, 9, 2, 8, 7, 6, 1, 12, 11, 10, 3)] |
| 582 | 2x |
effmois <- data.frame("eff" = effmois)
|
| 583 | 2x |
effmois.p <- data.frame("eff" = effmois.p)
|
| 584 | 2x |
tablemens <- |
| 585 | 2x |
rbind( |
| 586 | 2x |
cbind( |
| 587 | 2x |
"eff" = effmois - effmois.p, |
| 588 | 2x |
"type" = 2, |
| 589 | 2x |
"mois" = 1:12 |
| 590 |
), |
|
| 591 | 2x |
cbind(effmois.p, "type" = "1", "mois" = 1:12) |
| 592 |
) |
|
| 593 |
|
|
| 594 |
|
|
| 595 | 2x |
superpose.polygon <- lattice::trellis.par.get("superpose.polygon")
|
| 596 | 2x |
superpose.polygon$col = mypalette[c("weight", "eff")]
|
| 597 | 2x |
superpose.polygon$border = rep("transparent", 6)
|
| 598 | 2x |
lattice::trellis.par.set("superpose.polygon", superpose.polygon)
|
| 599 | 2x |
fontsize <- lattice::trellis.par.get("fontsize")
|
| 600 | 2x |
fontsize$text = 10 |
| 601 | 2x |
lattice::trellis.par.set("fontsize", fontsize)
|
| 602 | 2x |
par.main.text <- lattice::trellis.par.get("par.main.text")
|
| 603 | 2x |
par.main.text$cex = cex |
| 604 | 2x |
par.main.text$font = 1 |
| 605 | 2x |
lattice::trellis.par.set("par.main.text", par.main.text)
|
| 606 |
|
|
| 607 |
|
|
| 608 | 2x |
par.ylab.text <- lattice::trellis.par.get("par.ylab.text")
|
| 609 | 2x |
par.ylab.text$cex = cex - 0.2 |
| 610 | 2x |
lattice::trellis.par.set("par.ylab.text", par.ylab.text)
|
| 611 | 2x |
par.xlab.text <- lattice::trellis.par.get("par.xlab.text")
|
| 612 | 2x |
par.xlab.text$cex = cex - 0.2 |
| 613 | 2x |
lattice::trellis.par.set("par.xlab.text", par.xlab.text)
|
| 614 |
|
|
| 615 |
|
|
| 616 | 2x |
bar <- lattice::barchart( |
| 617 | 2x |
eff / 1000 ~ as.factor(mois), |
| 618 | 2x |
groups = as.factor(type), |
| 619 | 2x |
xlab = gettext("Month", domain = "R-stacomiR"),
|
| 620 | 2x |
ylab = gettext("Number (x1000)", domain = "R-stacomiR"),
|
| 621 |
# main=list(label=paste("Donnees mensuelles")),
|
|
| 622 | 2x |
data = tablemens, |
| 623 | 2x |
allow.multiple = FALSE, |
| 624 |
# key=lattice::simpleKey(text=c(gettext("weight of monthly number"),gettext("monthly number counted",domain="R-stacomiR")),
|
|
| 625 |
# rectangles = TRUE, |
|
| 626 |
# points=FALSE, |
|
| 627 |
# space="right", |
|
| 628 |
# cex=0.8), |
|
| 629 | 2x |
strip = FALSE, |
| 630 | 2x |
stack = TRUE |
| 631 |
) |
|
| 632 | 2x |
print(bar, position = c(0, 0, 1, .25), newpage = FALSE) |
| 633 | 2x |
return(invisible(NULL)) |
| 634 |
} |
| 1 |
#' Class "report_mig_interannual" |
|
| 2 |
#' |
|
| 3 |
#' When daily report are written in the t_reportjournalier_bjo table by the |
|
| 4 |
#' \link{report_mig-class} they can be used by this class to display
|
|
| 5 |
#' interannual comparisons of migration. |
|
| 6 |
#' When running its connect method, this class will run the \link{report_mig-class}
|
|
| 7 |
#' for each year where data are missing, or |
|
| 8 |
#' where the annual sum in the t_reportjournalier_bjo table differs from the counts |
|
| 9 |
#' generated by the \link{report_annual-class} : rows have been changed in the database.
|
|
| 10 |
#' Different charts are produced with different |
|
| 11 |
#' period grouping. See \link{write_database,report_mig-method} for details about how
|
|
| 12 |
#' this method inserts data in the t_reportjournalier_bjo table. |
|
| 13 |
#' |
|
| 14 |
#' @include ref_year.R |
|
| 15 |
#' @slot dc An object of class \link{ref_dc-class}, the counting device
|
|
| 16 |
#' @slot data A \code{data.frame} data loaded from the daily migration table t_bilanmigrationjournalier_bjo
|
|
| 17 |
#' @slot taxa An object of class \link{ref_taxa-class}
|
|
| 18 |
#' @slot stage An object of class \link{ref_stage-class}
|
|
| 19 |
#' @slot start_year An object of class \link{ref_year-class}. ref_year allows to choose year of beginning
|
|
| 20 |
#' @slot end_year An object of class \link{ref_year-class}
|
|
| 21 |
#' ref_year allows to choose last year of the report |
|
| 22 |
#' @slot calcdata A \code{list} of calculated data, filled in by the calcule method
|
|
| 23 |
#' |
|
| 24 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 25 |
#' @family report Objects |
|
| 26 |
#' @keywords classes |
|
| 27 |
#' @example inst/examples/report_mig_interannual-example.R |
|
| 28 |
#' @aliases report_mig_interannual |
|
| 29 |
#' @export |
|
| 30 |
setClass( |
|
| 31 |
Class = "report_mig_interannual", |
|
| 32 |
representation = |
|
| 33 |
representation( |
|
| 34 |
dc = "ref_dc", |
|
| 35 |
taxa = "ref_taxa", |
|
| 36 |
stage = "ref_stage", |
|
| 37 |
data = "data.frame", |
|
| 38 |
start_year = "ref_year", |
|
| 39 |
end_year = "ref_year", |
|
| 40 |
calcdata = "list" |
|
| 41 |
), |
|
| 42 |
prototype = prototype( |
|
| 43 |
dc = new("ref_dc"),
|
|
| 44 |
taxa = new("ref_taxa"),
|
|
| 45 |
stage = new("ref_stage"),
|
|
| 46 |
data = data.frame(), |
|
| 47 |
start_year = new("ref_year"),
|
|
| 48 |
end_year = new("ref_year"),
|
|
| 49 |
calcdata = list() |
|
| 50 |
) |
|
| 51 |
) |
|
| 52 |
setValidity("report_mig_interannual", function(object)
|
|
| 53 |
{
|
|
| 54 |
# if more than one taxa, the connect method will fail when trying to run the write_database for missing data |
|
| 55 |
# also plots have not been developed accordingly |
|
| 56 |
rep1 = ifelse( |
|
| 57 |
length(object@taxa@taxa_selected) == 1, |
|
| 58 |
TRUE, |
|
| 59 |
gettext("report_mig_interannual can only take one taxa", domain = "R-stacomiR")
|
|
| 60 |
) |
|
| 61 |
# same for stage |
|
| 62 |
rep2 = ifelse( |
|
| 63 |
length(object@stage@stage_selected) == 1, |
|
| 64 |
TRUE, |
|
| 65 |
gettext("report_mig_interannual can only take one stage", domain = "R-stacomiR")
|
|
| 66 |
) |
|
| 67 |
# multiple DC are allowed |
|
| 68 |
return(ifelse(rep1 & rep2 , TRUE , c(1:2)[!c(rep1, rep2)])) |
|
| 69 |
}) |
|
| 70 | ||
| 71 | ||
| 72 |
#' connect method for report_mig_interannual |
|
| 73 |
#' |
|
| 74 |
#' This method will check if the data in the t_reportjournalier_bjo table has no missing data, |
|
| 75 |
#' if missing the program will load missing data. As a second step, |
|
| 76 |
#' the program will check if the numbers in the table t_reportjournalier_bjo differ from those in the database, |
|
| 77 |
#' and propose to re-run the report_mig (which has a write_database methode to write daily reports) for those years. |
|
| 78 |
#' @note We expect different results between daily reports from the t_reportjournalier_bjo table and the annual sums |
|
| 79 |
#' from report_annual for glass eels as those may have been weighted and not only counted. The t_reportjournalier_bjo table used by report_mig_interannual |
|
| 80 |
#' contains the sum of glass eel numbers converted from weights and those directly counted. The report_annual does not. |
|
| 81 |
#' @param object An object of class \link{report_mig_interannual-class}
|
|
| 82 |
#' @param silent Stops messages from being displayed if silent=TRUE, default FALSE |
|
| 83 |
#' @param check Checks that data are corresponding between report_annual and report_mig |
|
| 84 |
#' @return report_mig_interannual an instantiated object with values filled with user choice |
|
| 85 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 86 |
#' @aliases connect.report_mig_interannual |
|
| 87 |
#' @importFrom utils menu |
|
| 88 |
setMethod( |
|
| 89 |
"connect", |
|
| 90 |
signature = signature("report_mig_interannual"),
|
|
| 91 |
definition = function(object, |
|
| 92 |
silent = FALSE, |
|
| 93 |
check = TRUE) |
|
| 94 |
{
|
|
| 95 |
# object<-r_mig_interannual |
|
| 96 |
# object<-bmi_cha |
|
| 97 |
# object<-bmi_des |
|
| 98 |
# object<-r_mig_interannual_vichy |
|
| 99 |
# require(dplyr); require(ggplot2) |
|
| 100 |
#--------------------------------------------------------------------------------------- |
|
| 101 |
# this function will be run several times if missing data or mismatching data are found |
|
| 102 |
# later in the script (hence the encapsulation) |
|
| 103 |
|
|
| 104 |
# if not silent display information about the connection |
|
| 105 | 10x |
if (!silent) {
|
| 106 | ! |
host <- options("stacomiR.host")
|
| 107 | ! |
funout(gettextf("host:%s", host, domain = "R-StacomiR"))
|
| 108 | ! |
port <- options("stacomiR.port")
|
| 109 | ! |
funout(gettextf("port:%s", port, domain = "R-StacomiR"))
|
| 110 |
# getting the database name |
|
| 111 | ! |
dbname <- options("stacomiR.dbname")
|
| 112 | ! |
funout(gettextf("dbname:%s", dbname, domain = "R-StacomiR"))
|
| 113 |
} |
|
| 114 |
|
|
| 115 |
#--------------------------------------------------------------------------------------- |
|
| 116 |
|
|
| 117 |
|
|
| 118 | 10x |
fn_connect <- function() {
|
| 119 | 13x |
les_annees = (object@start_year@year_selected):(object@end_year@year_selected) |
| 120 | 13x |
tax = object@taxa@taxa_selected |
| 121 | 13x |
std = object@stage@stage_selected |
| 122 | 13x |
dic = object@dc@dc_selected |
| 123 | 13x |
requete = new("RequeteDBwhere")
|
| 124 | 13x |
requete@where = paste( |
| 125 | 13x |
"WHERE bjo_annee IN ", |
| 126 | 13x |
vector_to_listsql(les_annees), |
| 127 | 13x |
" AND bjo_tax_code='", |
| 128 | 13x |
tax, |
| 129 | 13x |
"' AND bjo_std_code='", |
| 130 | 13x |
std, |
| 131 | 13x |
"' AND bjo_dis_identifiant in", |
| 132 | 13x |
vector_to_listsql(dic), |
| 133 | 13x |
sep = "" |
| 134 |
) |
|
| 135 | 13x |
requete@select = paste( |
| 136 | 13x |
"SELECT * FROM ", |
| 137 | 13x |
get_schema(), |
| 138 | 13x |
"t_bilanmigrationjournalier_bjo", |
| 139 | 13x |
sep = "" |
| 140 |
) |
|
| 141 | 13x |
requete@order_by = " ORDER BY bjo_jour " |
| 142 | 13x |
requete <- stacomirtools::query(requete) |
| 143 | 13x |
t_bilanmigrationjournalier_bjo <- requete@query |
| 144 | 13x |
if (nrow(t_bilanmigrationjournalier_bjo)>0) {
|
| 145 | 12x |
t_bilanmigrationjournalier_bjo <- stacomirtools::killfactor(t_bilanmigrationjournalier_bjo) |
| 146 |
} |
|
| 147 | 13x |
return(t_bilanmigrationjournalier_bjo) |
| 148 |
} |
|
| 149 |
|
|
| 150 |
#--------------------------------------------------------------------------------------- |
|
| 151 |
|
|
| 152 | 10x |
object@data <- fn_connect() |
| 153 | 10x |
if (nrow(object@data) == 0) {
|
| 154 | 1x |
funout( |
| 155 | 1x |
gettextf("No data in table t_bilanmigrationjournalier_bjo", domain = "R-StacomiR")
|
| 156 |
) |
|
| 157 | 1x |
check = TRUE |
| 158 |
} |
|
| 159 |
#browser() |
|
| 160 | 10x |
if (check) {
|
| 161 |
#---------------------------------------------------------------------- |
|
| 162 |
# Loading a report Annuel to compare numbers |
|
| 163 |
#---------------------------------------------------------------------- |
|
| 164 | 9x |
report_annual <- as(object, "report_annual") |
| 165 | 9x |
report_annual <- connect(report_annual) |
| 166 |
#---------------------------------------------------------------------- |
|
| 167 |
# MAIN LOOP, there can be several dic |
|
| 168 |
#---------------------------------------------------------------------- |
|
| 169 | 9x |
dic <- object@dc@dc_selected |
| 170 | 9x |
for (i in 1:length(dic)) {
|
| 171 |
#i=1 |
|
| 172 |
############################################ |
|
| 173 |
# function creating a table to compare actual counts with those stored in |
|
| 174 |
# in the t_reportjournalier_bjo table |
|
| 175 |
########################################### |
|
| 176 |
#========================================== |
|
| 177 |
|
|
| 178 | 10x |
fn_check <- function() {
|
| 179 | 12x |
data1 <- |
| 180 | 12x |
report_annual@data[report_annual@data$ope_dic_identifiant == dic[i], c("effectif", "annee")]
|
| 181 |
# data from report_migInterannuel |
|
| 182 | 12x |
data2 <- object@data[object@data$bjo_dis_identifiant == dic[i], ] |
| 183 | 12x |
data21 <- |
| 184 | 12x |
dplyr::select(data2, bjo_annee, bjo_valeur, bjo_labelquantite) |
| 185 |
|
|
| 186 | 12x |
data22 <- dplyr::group_by(data21, bjo_annee, bjo_labelquantite) |
| 187 | 12x |
if (nrow(data22) == 0) |
| 188 | 12x |
data22$bjo_valeur <- as.numeric(data22$bjo_valeur) |
| 189 | 12x |
data23 <- dplyr::summarize(data22, total = sum(bjo_valeur)) |
| 190 | 12x |
data24 <- |
| 191 | 12x |
dplyr::filter(dplyr::ungroup(data23), |
| 192 | 12x |
bjo_labelquantite == "Effectif_total") |
| 193 | 12x |
data24 <- dplyr::select(data24, bjo_annee, total) |
| 194 | 12x |
data24 <- |
| 195 | 12x |
dplyr::rename(data24, annee = bjo_annee, effectif_bjo = total) |
| 196 | 12x |
data124 <- merge(data1, |
| 197 | 12x |
data24, |
| 198 | 12x |
all.x = TRUE, |
| 199 | 12x |
all.y = TRUE, |
| 200 | 12x |
by = "annee") |
| 201 | 12x |
return(data124) |
| 202 |
} |
|
| 203 |
#========================================== |
|
| 204 |
# table with 3 columns : annee; effectif; effectif_bjo |
|
| 205 | 10x |
compared_numbers <- fn_check() |
| 206 |
# as we have changed the report_annual to split data between years |
|
| 207 |
# some unwanted data might step in outside the year range |
|
| 208 |
# we correct for that |
|
| 209 | 10x |
compared_numbers <- compared_numbers[compared_numbers$annee >= object@start_year@year_selected & |
| 210 | 10x |
compared_numbers$annee <= object@end_year@year_selected, ] |
| 211 |
|
|
| 212 |
#------------------------------------------------------------------------------------- |
|
| 213 |
# First test, if missing data, the program will propose to load the data by running report_mig |
|
| 214 |
#------------------------------------------------------------------------------------- |
|
| 215 |
# when data are missing, NA appear in the effectif_bjo column |
|
| 216 | 10x |
if (any(is.na(compared_numbers$effectif_bjo))) {
|
| 217 | 2x |
index_missing_years <- which(is.na(compared_numbers$effectif_bjo)) |
| 218 | 2x |
missing_years <- compared_numbers$annee[index_missing_years] |
| 219 | 2x |
if (!silent & |
| 220 | 2x |
length(dic) > 1) |
| 221 | 2x |
funout(gettextf("DC with missing values : %s ", dic[i], domain = "R-StacomiR"))
|
| 222 | 2x |
if (!silent) |
| 223 | 2x |
funout(gettextf( |
| 224 | 2x |
"Years with no value : %s ", |
| 225 | 2x |
stringr::str_c(missing_years, collapse = "; "), |
| 226 | 2x |
domain = "R-StacomiR" |
| 227 |
)) |
|
| 228 | 2x |
if (!silent) |
| 229 | 2x |
funout( |
| 230 | 2x |
gettextf( |
| 231 | 2x |
"Some years are missing in the t_reportjournalier_bjo table, loading them now !", |
| 232 | 2x |
domain = "R-StacomiR" |
| 233 |
) |
|
| 234 |
) |
|
| 235 |
|
|
| 236 |
|
|
| 237 | 2x |
for (y in 1:length(missing_years)) {
|
| 238 | 5x |
Y <- missing_years[y] |
| 239 | 5x |
bM = new("report_mig")
|
| 240 | 5x |
if (!silent) |
| 241 | 5x |
funout(gettextf("Running report_mig for year %s", Y, domain = "R-StacomiR"))
|
| 242 | 5x |
bM = choice_c( |
| 243 | 5x |
bM, |
| 244 | 5x |
dc = dic[i], |
| 245 | 5x |
taxa = object@taxa@taxa_selected, |
| 246 | 5x |
stage = object@stage@stage_selected, |
| 247 | 5x |
datedebut = stringr::str_c(Y, "-01-01"), |
| 248 | 5x |
datefin = stringr::str_c(Y, "-12-31") |
| 249 |
) |
|
| 250 | 5x |
bM <- charge(bM, silent = silent) |
| 251 | 5x |
bM <- connect(bM, silent = silent) |
| 252 | 5x |
bM <- calcule(bM, silent = silent) |
| 253 | 5x |
if (nrow(bM@data) > 0) {
|
| 254 |
# below the argument check_for_bjo is necessary |
|
| 255 |
# as the write database method from report_mig |
|
| 256 |
# uses the connect method from report_mig_interannual and the |
|
| 257 |
# program runs in endless loops... |
|
| 258 | 3x |
write_database(bM, silent = silent, check_for_bjo = FALSE) |
| 259 |
} |
|
| 260 | 2x |
} # end for loop to write new reports |
| 261 |
# reloading everything |
|
| 262 | 2x |
object@data <- fn_connect() |
| 263 | 2x |
compared_numbers <- fn_check() |
| 264 |
|
|
| 265 | 10x |
} # end if any... |
| 266 |
|
|
| 267 |
# The method for report annual has been changed and now reports NA when taxa are missing |
|
| 268 |
# we have to remove them otherwise the comparison does not work : |
|
| 269 |
# (!all(round(compared_numbers$effectif) == round(compared_numbers$effectif_bjo))) |
|
| 270 | 10x |
compared_numbers$effectif_bjo[is.na(compared_numbers$effectif_bjo)] <- 0 |
| 271 |
|
|
| 272 |
#------------------------------------------------------------------------------------- |
|
| 273 |
# Second test, for existing report with different numbers, again the data will be witten again |
|
| 274 |
# if the previous test failed, and user confirmed that there was a problem |
|
| 275 |
# the object@data and compared_numbers are reloaded (see above) |
|
| 276 |
# this test will only be run if the stage is not glass eel, for glass eels it does not make sense |
|
| 277 |
# as some of the "effectif_total" in the bjo table correspond to weights not counts. |
|
| 278 |
#------------------------------------------------------------------------------------- |
|
| 279 |
|
|
| 280 | 10x |
if (object@taxa@taxa_selected == 2038 & |
| 281 | 10x |
object@stage@stage_selected == "CIV") {
|
| 282 | ! |
if (!silent) |
| 283 | ! |
funout( |
| 284 | ! |
gettext( |
| 285 | ! |
"For glass eel it is not possible to check that data are up to date", |
| 286 | ! |
domain = "R-StacomiR" |
| 287 |
) |
|
| 288 |
) |
|
| 289 |
|
|
| 290 | 10x |
} else if (!all(round(compared_numbers$effectif) == round(compared_numbers$effectif_bjo))) {
|
| 291 | 1x |
index_different_years <- |
| 292 | 1x |
which(round(compared_numbers$effectif) != round(compared_numbers$effectif_bjo)) |
| 293 | 1x |
differing_years <- compared_numbers$annee[index_different_years] |
| 294 | 1x |
if (!silent) |
| 295 | 1x |
funout( |
| 296 | 1x |
gettextf( |
| 297 | 1x |
"Years with values differing between t_reportjournalier_bjo and report_annual : %s ", |
| 298 | 1x |
stringr::str_c(differing_years, collapse = "; "), |
| 299 | 1x |
domain = "R-StacomiR" |
| 300 |
) |
|
| 301 |
) |
|
| 302 |
#================================== |
|
| 303 | 1x |
reload_years_with_error = function() {
|
| 304 | 1x |
bM = new("report_mig")
|
| 305 | 1x |
for (Y in differing_years) {
|
| 306 |
# Y=differing_years[1] |
|
| 307 | 1x |
funout(gettextf("Running report_mig to correct data for year %s", Y, domain="R-stacomiR"))
|
| 308 | 1x |
bM = choice_c( |
| 309 | 1x |
bM, |
| 310 | 1x |
dc = dic[i], |
| 311 | 1x |
taxa = object@taxa@taxa_selected, |
| 312 | 1x |
stage = object@stage@stage_selected, |
| 313 | 1x |
datedebut = stringr::str_c(Y, "-01-01"), |
| 314 | 1x |
datefin = stringr::str_c(Y, "-12-31") |
| 315 |
) |
|
| 316 | 1x |
bM <- charge(bM, silent = silent) |
| 317 | 1x |
bM <- connect(bM, silent = silent) |
| 318 | 1x |
bM <- calcule(bM, silent = silent) |
| 319 |
# report annual may have different numbers from report mig |
|
| 320 |
# so I'm adding an additional check there |
|
| 321 | 1x |
bma_num <- compared_numbers[compared_numbers$annee==Y,"effectif"] |
| 322 | 1x |
bjo_num <- compared_numbers[compared_numbers$annee==Y,"effectif_bjo"] |
| 323 | 1x |
bjo_num_new <- sum(bM@calcdata[[stringr::str_c("dc_", dic[i])]][["data"]][,"Effectif_total"])
|
| 324 | 1x |
if (nrow(bM@data) > 0) {
|
| 325 | 1x |
if (!round(bjo_num_new) == round(bjo_num)){
|
| 326 |
# check for bjo will ensure that previous report are deleted |
|
| 327 | ! |
write_database(bM, |
| 328 | ! |
silent = silent, |
| 329 | ! |
check_for_bjo = TRUE) |
| 330 |
} else {
|
|
| 331 | 1x |
funout( |
| 332 | 1x |
gettextf( |
| 333 | 1x |
paste("There is a difference between report_annual Na= %s and report_mig ",
|
| 334 | 1x |
"Nj= %s but the sums are the same between report_mig and the database (t_bilanmigrationjournalier_bjo).", |
| 335 | 1x |
"This difference is due to migration report overlapping between two years and the program. No writing in the db."), |
| 336 | 1x |
round(bma_num), round(bjo_num), |
| 337 | 1x |
domain = "R-StacomiR" |
| 338 |
) |
|
| 339 |
) |
|
| 340 | 1x |
} # end else numbers are equal => do nothing |
| 341 | 1x |
} # end test nrow |
| 342 | 1x |
} # end for loop to write new reports |
| 343 |
# the data are loaded again |
|
| 344 | 1x |
object@data <- fn_connect() |
| 345 |
# I need to assign the result one step up (in the environment of the connect function) |
|
| 346 | 1x |
assign("object", object, envir = parent.frame(n = 1))
|
| 347 |
|
|
| 348 | 1x |
} # end reload year with errors |
| 349 |
#================================== |
|
| 350 |
|
|
| 351 | 1x |
if (!silent) {
|
| 352 | ! |
choice2 <- |
| 353 | ! |
menu( |
| 354 | ! |
c("yes", "no"),
|
| 355 | ! |
graphics = TRUE, |
| 356 | ! |
title = gettextf("Data changed, rerun ?", domain = "R-StacomiR")
|
| 357 |
) |
|
| 358 | ! |
if (choice2 == 1) |
| 359 | ! |
reload_years_with_error() |
| 360 |
|
|
| 361 |
} else {
|
|
| 362 | 1x |
reload_years_with_error() |
| 363 |
} |
|
| 364 | 10x |
} # secondary check |
| 365 | 9x |
} # end for |
| 366 | 10x |
} # end check |
| 367 |
#------------------------------------------------------------------------------------- |
|
| 368 |
# Final check for data |
|
| 369 |
# index of data already present in the database |
|
| 370 |
#------------------------------------------------------------------------------------- |
|
| 371 | 10x |
les_annees = object@start_year@year_selected:object@end_year@year_selected |
| 372 | 10x |
index = unique(object@data$bjo_annee) %in% les_annees |
| 373 |
# s'il manque des donnees pour certaines annees selectionnnees" |
|
| 374 | 10x |
if (!silent) {
|
| 375 | ! |
if (length(les_annees[!index]) > 0) |
| 376 |
{
|
|
| 377 | ! |
funout(paste( |
| 378 | ! |
gettext( |
| 379 | ! |
"Attention, there is no migration summary for these year\n", |
| 380 | ! |
domain = "R-stacomiR" |
| 381 |
), |
|
| 382 | ! |
paste(les_annees[!index], collapse = ","), |
| 383 | ! |
gettext( |
| 384 | ! |
", this taxa and this stage (report_mig_interannual.r)\n", |
| 385 | ! |
domain = "R-stacomiR" |
| 386 |
) |
|
| 387 |
)) |
|
| 388 | ! |
} # end if |
| 389 |
|
|
| 390 |
# si toutes les annees sont presentes |
|
| 391 | ! |
if (length(les_annees[index]) > 0) {
|
| 392 | ! |
funout(paste( |
| 393 | ! |
gettext("Interannual migrations query completed", domain = "R-stacomiR"),
|
| 394 | ! |
paste(les_annees[index], collapse = ","), |
| 395 | ! |
"\n" |
| 396 |
)) |
|
| 397 |
} |
|
| 398 |
} |
|
| 399 | 10x |
return(object) |
| 400 |
} |
|
| 401 |
) |
|
| 402 | ||
| 403 |
#' supprime method for report_mig_interannual class, deletes values in table t_bilanmigrationjournalier_bjo |
|
| 404 |
#' @param object An object of class \link{report_mig_interannual-class}
|
|
| 405 |
#' @return nothing, called for its side effect, removing lines from the database |
|
| 406 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 407 |
#' @aliases supprime.report_mig_interannual |
|
| 408 |
setMethod( |
|
| 409 |
"supprime", |
|
| 410 |
signature = signature("report_mig_interannual"),
|
|
| 411 |
definition = function(object) |
|
| 412 |
{
|
|
| 413 |
# recuperation des annees taxa et stage concernes |
|
| 414 | 6x |
les_annees = (object@start_year@year_selected):(object@end_year@year_selected) |
| 415 | 6x |
tax = object@taxa@taxa_selected |
| 416 | 6x |
std = object@stage@stage_selected |
| 417 | 6x |
dic = object@dc@dc_selected |
| 418 | 6x |
con = new("ConnectionDB")
|
| 419 | 6x |
con <- connect(con) |
| 420 | 6x |
on.exit(pool::poolClose(con@connection)) |
| 421 | 6x |
sql = stringr::str_c( |
| 422 | 6x |
"DELETE from ", |
| 423 | 6x |
get_schema(), |
| 424 | 6x |
"t_bilanmigrationjournalier_bjo ", |
| 425 | 6x |
" WHERE bjo_annee IN (",
|
| 426 | 6x |
paste(les_annees, collapse = ","), |
| 427 | 6x |
") AND bjo_tax_code='", |
| 428 | 6x |
tax, |
| 429 | 6x |
"' AND bjo_std_code='", |
| 430 | 6x |
std, |
| 431 | 6x |
"' AND bjo_dis_identifiant=", |
| 432 | 6x |
dic |
| 433 |
) |
|
| 434 | 6x |
pool::dbExecute(con@connection, statement = sql) |
| 435 |
|
|
| 436 | 6x |
sql = stringr::str_c( |
| 437 | 6x |
"DELETE from ", |
| 438 | 6x |
get_schema(), |
| 439 | 6x |
"t_bilanmigrationmensuel_bme ", |
| 440 | 6x |
" WHERE bme_annee IN (",
|
| 441 | 6x |
paste(les_annees, collapse = ","), |
| 442 | 6x |
") AND bme_tax_code='", |
| 443 | 6x |
tax, |
| 444 | 6x |
"' AND bme_std_code='", |
| 445 | 6x |
std, |
| 446 | 6x |
"' AND bme_dis_identifiant=", |
| 447 | 6x |
dic |
| 448 |
) |
|
| 449 | 6x |
pool::dbExecute(con@connection, statement = sql) |
| 450 | 6x |
return(invisible(NULL)) |
| 451 |
} |
|
| 452 | ||
| 453 |
) |
|
| 454 | ||
| 455 |
#' loading method for report_mig_interannual class |
|
| 456 |
#' @param object An object of class \link{report_mig_interannual-class}
|
|
| 457 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 458 |
#' @return An object of class \link{report_mig_interannual-class} with slots set from values assigned in \code{envir_stacomi} environment
|
|
| 459 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 460 |
#' @aliases charge.report_mig_interannual |
|
| 461 |
#' @keywords internal |
|
| 462 |
setMethod( |
|
| 463 |
"charge", |
|
| 464 |
signature = signature("report_mig_interannual"),
|
|
| 465 |
definition = function(object, silent = FALSE) |
|
| 466 |
{
|
|
| 467 | 2x |
report_mig_interannual <- object |
| 468 | 2x |
if (exists("ref_dc", envir_stacomi)) {
|
| 469 | 2x |
report_mig_interannual@dc <- get("ref_dc", envir_stacomi)
|
| 470 |
} else {
|
|
| 471 | ! |
funout( |
| 472 | ! |
gettext( |
| 473 | ! |
"You need to choose a counting device, clic on validate\n", |
| 474 | ! |
domain = "R-stacomiR" |
| 475 |
), |
|
| 476 | ! |
arret = TRUE |
| 477 |
) |
|
| 478 |
} |
|
| 479 | 2x |
if (exists("ref_taxa", envir_stacomi)) {
|
| 480 | 2x |
report_mig_interannual@taxa <- get("ref_taxa", envir_stacomi)
|
| 481 |
} else {
|
|
| 482 | ! |
funout( |
| 483 | ! |
gettext("You need to choose a taxa, clic on validate\n", domain = "R-stacomiR"),
|
| 484 | ! |
arret = TRUE |
| 485 |
) |
|
| 486 |
} |
|
| 487 | 2x |
if (exists("ref_stage", envir_stacomi)) {
|
| 488 | 2x |
report_mig_interannual@stage <- get("ref_stage", envir_stacomi)
|
| 489 |
} else |
|
| 490 |
{
|
|
| 491 | ! |
funout( |
| 492 | ! |
gettext("You need to choose a stage, clic on validate\n", domain = "R-stacomiR"),
|
| 493 | ! |
arret = TRUE |
| 494 |
) |
|
| 495 |
} |
|
| 496 | 2x |
if (exists("start_year", envir_stacomi)) {
|
| 497 | 2x |
report_mig_interannual@start_year <- get("start_year", envir_stacomi)
|
| 498 |
} else {
|
|
| 499 | ! |
funout(gettext("You need to choose the starting year\n", domain = "R-stacomiR"),
|
| 500 | ! |
arret = TRUE) |
| 501 |
} |
|
| 502 | 2x |
if (exists("end_year", envir_stacomi)) {
|
| 503 | 2x |
report_mig_interannual@end_year <- get("end_year", envir_stacomi)
|
| 504 |
} else {
|
|
| 505 | ! |
funout(gettext("You need to choose the ending year\n", domain = "R-stacomiR"),
|
| 506 | ! |
arret = TRUE) |
| 507 |
} |
|
| 508 |
# this will test that only one taxa and one stage have been loaded (multiple dc are allowed) |
|
| 509 | 2x |
validObject(report_mig_interannual) |
| 510 | 2x |
assign("report_mig_interannual",
|
| 511 | 2x |
report_mig_interannual, |
| 512 | 2x |
envir_stacomi) |
| 513 | 2x |
if (!silent) |
| 514 | 2x |
funout( |
| 515 | 2x |
gettext( |
| 516 | 2x |
"Writing report_mig_interannual in the environment envir_stacomi : write r_mig_interannual=get('report_mig_interannual',envir_stacomi) ",
|
| 517 | 2x |
domain = "R-stacomiR" |
| 518 |
) |
|
| 519 |
) |
|
| 520 |
|
|
| 521 | 2x |
return(report_mig_interannual) |
| 522 |
} |
|
| 523 |
) |
|
| 524 | ||
| 525 |
#' command line interface for report_mig_interannual class |
|
| 526 |
#' @param object An object of class \link{report_mig_interannual-class}
|
|
| 527 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 528 |
#' @param taxa Either a species name in latin or the SANDRE code for species (ie 2038=Anguilla anguilla), |
|
| 529 |
#' it should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 530 |
#' @param stage A stage code matching the ref.tr_stadedeveloppement_std table in the stacomi database, see \link{choice_c,ref_stage-method}
|
|
| 531 |
#' @param start_year The starting the first year, passed as character or integer |
|
| 532 |
#' @param end_year the finishing year |
|
| 533 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 534 |
#' @return An object of class \link{report_mig_interannual-class} with data selected
|
|
| 535 |
#' The choice_c method fills in the data slot for classes \link{ref_dc-class}, \link{ref_taxa-class}, \link{ref_stage-class} and two slots of \link{ref_year-class}
|
|
| 536 |
#' @aliases choice_c.report_mig_interannual |
|
| 537 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 538 |
setMethod( |
|
| 539 |
"choice_c", |
|
| 540 |
signature = signature("report_mig_interannual"),
|
|
| 541 |
definition = function(object, |
|
| 542 |
dc, |
|
| 543 |
taxa, |
|
| 544 |
stage, |
|
| 545 |
start_year, |
|
| 546 |
end_year, |
|
| 547 |
silent = FALSE) {
|
|
| 548 |
# code for debug using example |
|
| 549 |
#report_mig_interannual<-r_mig_interannual;dc=c(16);taxa="Anguilla anguilla";stage=c("AGJ");start_year="1984";end_year="2016"
|
|
| 550 | 10x |
report_mig_interannual <- object |
| 551 | 10x |
report_mig_interannual@dc = charge(report_mig_interannual@dc) |
| 552 |
# loads and verifies the dc |
|
| 553 |
# this will set dc_selected slot |
|
| 554 | 10x |
report_mig_interannual@dc <- |
| 555 | 10x |
choice_c(object = report_mig_interannual@dc, dc) |
| 556 |
# only taxa present in the report_mig are used |
|
| 557 | 10x |
report_mig_interannual@taxa <- |
| 558 | 10x |
charge_with_filter(object = report_mig_interannual@taxa, report_mig_interannual@dc@dc_selected) |
| 559 | 10x |
report_mig_interannual@taxa <- |
| 560 | 10x |
choice_c(report_mig_interannual@taxa, taxa) |
| 561 | 10x |
report_mig_interannual@stage <- |
| 562 | 10x |
charge_with_filter( |
| 563 | 10x |
object = report_mig_interannual@stage, |
| 564 | 10x |
report_mig_interannual@dc@dc_selected, |
| 565 | 10x |
report_mig_interannual@taxa@taxa_selected |
| 566 |
) |
|
| 567 | 10x |
report_mig_interannual@stage <- |
| 568 | 10x |
choice_c(report_mig_interannual@stage, stage) |
| 569 |
# depending on report_object the method will load data and issue a warning if data are not present |
|
| 570 |
# this is the first step, the second verification will be done in method connect |
|
| 571 |
|
|
| 572 | 10x |
report_mig_interannual@start_year <- |
| 573 | 10x |
charge(object = report_mig_interannual@start_year, |
| 574 | 10x |
objectreport = "report_mig_interannual") |
| 575 | 10x |
report_mig_interannual@start_year <- |
| 576 | 10x |
choice_c( |
| 577 | 10x |
object = report_mig_interannual@start_year, |
| 578 | 10x |
nomassign = "start_year", |
| 579 | 10x |
annee = start_year, |
| 580 | 10x |
silent = silent |
| 581 |
) |
|
| 582 | 9x |
report_mig_interannual@end_year@data <- |
| 583 | 9x |
report_mig_interannual@start_year@data |
| 584 | 9x |
report_mig_interannual@end_year <- |
| 585 | 9x |
choice_c( |
| 586 | 9x |
object = report_mig_interannual@end_year, |
| 587 | 9x |
nomassign = "end_year", |
| 588 | 9x |
annee = end_year, |
| 589 | 9x |
silent = silent |
| 590 |
) |
|
| 591 | 9x |
assign("report_mig_interannual", report_mig_interannual, envir = envir_stacomi)
|
| 592 | 9x |
return(report_mig_interannual) |
| 593 |
} |
|
| 594 |
) |
|
| 595 | ||
| 596 | ||
| 597 |
#' calcule method for report_mig_interannual |
|
| 598 |
#' |
|
| 599 |
#' Performs the calculation of seasonal coefficients for the plot(plot.type="seasonal") method. The numbers |
|
| 600 |
#' are split according to the period chosen, one of "day","week","month","2 weeks", French labels are also |
|
| 601 |
#' accepted as arguments. Once this is done, the seasonality of the migration is displayed using the day when the |
|
| 602 |
#' first fish was seen, then the days (or period) corresponding to 5, 50 , 95, and 100 percent of the migration. |
|
| 603 |
#' The duration of 90% of the migraton between Q5 and Q95 is also of interest. |
|
| 604 |
#' |
|
| 605 |
#' @param object An object of class \link{report_mig_interannual-class}
|
|
| 606 |
#' @param silent Boolean, if TRUE, information messages are not displayed, only warnings and errors |
|
| 607 |
#' @param timesplit One of "day","week","month","2 weeks", "jour","semaine","quinzaine","mois" |
|
| 608 |
#' @note The class report_mig_interannual does not handle escapement rates nor |
|
| 609 |
#' 'devenir' i.e. the destination of the fishes. |
|
| 610 |
#' @return An object of class \link{report_mig_interannual-class} with calcdata slot filled.
|
|
| 611 |
#' @aliases calcule.report_mig_interannual |
|
| 612 |
#' @author Marion Legrand |
|
| 613 |
setMethod( |
|
| 614 |
"calcule", |
|
| 615 |
signature = signature("report_mig_interannual"),
|
|
| 616 |
definition = function(object, |
|
| 617 |
silent = FALSE, |
|
| 618 |
timesplit = "mois") {
|
|
| 619 | 8x |
report_mig_interannual <- object |
| 620 |
#report_mig_interannual<-r_mig_interannual |
|
| 621 |
#report_mig_interannual<-r_mig_interannual_vichy;silent=FALSE;timesplit="mois" |
|
| 622 |
#require(dplyr) |
|
| 623 | 8x |
if (!timesplit %in% c("jour",
|
| 624 | 8x |
"day", |
| 625 | 8x |
"month", |
| 626 | 8x |
"mois", |
| 627 | 8x |
"week", |
| 628 | 8x |
"semaine", |
| 629 | 8x |
"quinzaine", |
| 630 | 8x |
"2 weeks")) |
| 631 | 8x |
stop ( |
| 632 | 8x |
stringr::str_c( |
| 633 | 8x |
"timesplit should be one of :", |
| 634 | 8x |
"jour ", |
| 635 | 8x |
"day ", |
| 636 | 8x |
"month ", |
| 637 | 8x |
"mois ", |
| 638 | 8x |
"week ", |
| 639 | 8x |
"semaine ", |
| 640 | 8x |
"month ", |
| 641 | 8x |
"mois ", |
| 642 | 8x |
"quinzaine ", |
| 643 | 8x |
"2 weeks " |
| 644 |
) |
|
| 645 |
) |
|
| 646 |
# back to French labels for consistency with fun_report_mig_interannual code |
|
| 647 | 8x |
timesplit <- |
| 648 | 8x |
switch( |
| 649 | 8x |
timesplit, |
| 650 | 8x |
"day" = "jour_365", |
| 651 | 8x |
"jour" = "jour_365", |
| 652 | 8x |
"week" = "semaine", |
| 653 | 8x |
"month" = "mois", |
| 654 | 8x |
"2 weeks" = "quinzaine", |
| 655 | 8x |
timesplit |
| 656 |
) |
|
| 657 |
# there should be just one station, this will be tested |
|
| 658 | 8x |
station <- report_mig_interannual@dc@station |
| 659 | 8x |
taxa <- report_mig_interannual@taxa@taxa_selected |
| 660 | 8x |
stage <- report_mig_interannual@stage@stage_selected |
| 661 | 8x |
if (length(unique(report_mig_interannual@dc@station)) != 1) |
| 662 | 8x |
stop( |
| 663 | 8x |
"You have more than one station in the report, the dc from the report should belong to the same station" |
| 664 |
) |
|
| 665 | 8x |
if (nrow(report_mig_interannual@data) == 0) |
| 666 | 8x |
stop( |
| 667 | 8x |
"No rows in report_mig_interannual@data, nothing to run calculations on, you should run a report_mig_mult on this dc first" |
| 668 |
) |
|
| 669 |
|
|
| 670 | 8x |
datadic <- report_mig_interannual@data[report_mig_interannual@data$bjo_labelquantite == |
| 671 | 8x |
"Effectif_total", ] |
| 672 | 8x |
datadic <- |
| 673 | 8x |
fun_date_extraction( |
| 674 | 8x |
datadic, |
| 675 | 8x |
nom_coldt = "bjo_jour", |
| 676 | 8x |
jour_an = TRUE, |
| 677 | 8x |
quinzaine = TRUE |
| 678 |
) |
|
| 679 | 8x |
datadic <- killfactor(datadic) |
| 680 |
# here this code avoids the following problem :Error: (list) object cannot be coerced to type 'double' |
|
| 681 |
# data is subsetted for columns not containing bjo, and apply is run on each of the column |
|
| 682 | 8x |
datadic[, colnames(datadic)[!grepl("bjo_", colnames(datadic))]] <-
|
| 683 | 8x |
apply( |
| 684 | 8x |
X = datadic[, colnames(datadic)[!grepl("bjo_", colnames(datadic))]],
|
| 685 | 8x |
MARGIN = 2, |
| 686 | 8x |
FUN = function(X) |
| 687 | 8x |
as.numeric(X) |
| 688 |
) |
|
| 689 | 8x |
fnquant <- |
| 690 | 8x |
function(data, |
| 691 | 8x |
timesplit = "jour_365", |
| 692 | 8x |
probs = c(0, .05, .5, .95, 1)) {
|
| 693 |
# if there is just a single line, crashes, so reports exactly the same for all values |
|
| 694 | 163x |
if (nrow(data) == 1) {
|
| 695 | 1x |
res <- c( |
| 696 | 1x |
"0%" = data[, timesplit], |
| 697 | 1x |
"5%" = data[, timesplit], |
| 698 | 1x |
"50%" = data[, timesplit], |
| 699 | 1x |
"95%" = data[, timesplit], |
| 700 | 1x |
"100%" = data[, timesplit] |
| 701 |
) |
|
| 702 |
} else {
|
|
| 703 | 162x |
res <- Hmisc::wtd.quantile( |
| 704 | 162x |
x = data[, timesplit], |
| 705 | 162x |
weights = abs(data$bjo_valeur), |
| 706 | 162x |
probs = probs |
| 707 |
) |
|
| 708 | 162x |
return(res) |
| 709 |
} |
|
| 710 |
} |
|
| 711 |
#fnquant(datadic[datadic$bjo_annee==2012,],"mois") |
|
| 712 |
# for some reasons this code does not work : Error in x + weights : non-numeric argument to binary operator |
|
| 713 |
# dat<-dplyr::select_(datadic,"bjo_annee","bjo_dis_identifiant","bjo_tax_code","bjo_std_code","bjo_valeur",timesplit)%>% |
|
| 714 |
# dplyr::group_by_("bjo_annee","bjo_tax_code","bjo_std_code")
|
|
| 715 |
# dat2<-dat%>% do(res=fnquant(data=.,timesplit=timesplit,probs=c(0, .05, .5, .95, 1))) |
|
| 716 |
# dat3<-dat2%>%summarize(bjo_annee,bjo_tax_code,bjo_std_code,Q0=res[[1]],Q5=res[[2]], |
|
| 717 |
# Q50=res[[3]],Q95=res[[4]],Q100=res[[5]]) |
|
| 718 |
# this simple code will do : |
|
| 719 | 8x |
dat <- list() |
| 720 | 8x |
for (i in unique(datadic$bjo_annee)) {
|
| 721 | 163x |
dat[[i]] <- |
| 722 | 163x |
fnquant(data = datadic[datadic$bjo_annee == i, ], timesplit = timesplit) |
| 723 |
} |
|
| 724 | 8x |
dat <- as.data.frame(matrix(unlist(dat), ncol = 5, byrow = TRUE)) |
| 725 | 8x |
colnames(dat) <- c("Q0", "Q5", "Q50", "Q95", "Q100")
|
| 726 | 8x |
dat$d90 <- dat$Q95 - dat$Q5 |
| 727 | 8x |
dat$year = unique(datadic$bjo_annee) |
| 728 | 8x |
dat$taxa = taxa |
| 729 | 8x |
dat$stage = stage |
| 730 | 8x |
dat$station = unique(station) |
| 731 | 8x |
dat$timesplit = timesplit |
| 732 | 8x |
dat <- |
| 733 | 8x |
dat[, c( |
| 734 | 8x |
"year", |
| 735 | 8x |
"station", |
| 736 | 8x |
"taxa", |
| 737 | 8x |
"stage", |
| 738 | 8x |
"Q0", |
| 739 | 8x |
"Q5", |
| 740 | 8x |
"Q50", |
| 741 | 8x |
"Q95", |
| 742 | 8x |
"Q100", |
| 743 | 8x |
"d90", |
| 744 | 8x |
"timesplit" |
| 745 |
)] |
|
| 746 | 8x |
report_mig_interannual@calcdata <- dat |
| 747 | 8x |
return(report_mig_interannual) |
| 748 |
} |
|
| 749 |
) |
|
| 750 | ||
| 751 |
#' statistics per time period |
|
| 752 |
#' |
|
| 753 |
#' function called for report_mig_mult objects renames columns |
|
| 754 |
#' replaces nulls, and calculates reports with time period larger than day |
|
| 755 |
#' |
|
| 756 |
#' @param dat a data frame with columns ("bjo_annee","bjo_jour","bjo_labelquantite","bjo_valeur")
|
|
| 757 |
#' @param year The year to exclude from the historical series (it will be plotted against the historical series) |
|
| 758 |
#' @param timesplit "week" "2 weeks" "month" as provided to seq.POSIXt, default NULL |
|
| 759 |
#' @return a data frame with mean, max, and min calculated for each timesplit |
|
| 760 |
#' @export |
|
| 761 |
fun_report_mig_interannual = function(dat, |
|
| 762 |
year = NULL, |
|
| 763 |
timesplit = NULL) |
|
| 764 |
{
|
|
| 765 | 19x |
if (nrow(dat) > 0) |
| 766 |
{
|
|
| 767 | 19x |
dat <- dat[dat$bjo_labelquantite == "Effectif_total", ] |
| 768 | 19x |
dat <- |
| 769 | 19x |
stacomirtools::chnames( |
| 770 | 19x |
dat, |
| 771 | 19x |
c( |
| 772 | 19x |
"bjo_annee", |
| 773 | 19x |
"bjo_jour", |
| 774 | 19x |
"bjo_labelquantite", |
| 775 | 19x |
"bjo_valeur" |
| 776 |
), |
|
| 777 | 19x |
c("year", "day", "labelquantity", "value")
|
| 778 |
) |
|
| 779 | 19x |
dat <- dat[, c("year", "day", "value")]
|
| 780 | 19x |
if (!is.null(year)) {
|
| 781 | 6x |
dat <- dat[dat$year != year, ] |
| 782 |
} |
|
| 783 | 19x |
dat$day <- trunc.POSIXt(dat$day, digits = 'days') |
| 784 | 19x |
dat$day <- as.Date(strptime(strftime(dat$day, '2000-%m-%d'), '%Y-%m-%d')) |
| 785 |
|
|
| 786 |
|
|
| 787 | 19x |
if (!is.null(timesplit)) {
|
| 788 | 13x |
seq_timesplit <- seq.POSIXt( |
| 789 | 13x |
from = strptime("2000-01-01", format = '%Y-%m-%d'),
|
| 790 | 13x |
to = strptime("2000-12-31", format = '%Y-%m-%d'),
|
| 791 | 13x |
by = timesplit |
| 792 |
) |
|
| 793 | 13x |
seq_timesplit <- as.Date(trunc(seq_timesplit, digits = 'days')) |
| 794 | 13x |
dat[, timesplit] <- dat$day |
| 795 | 13x |
for (j in 1:(length(seq_timesplit) - 1)) {
|
| 796 | 896x |
dat[dat$day >= seq_timesplit[j] & |
| 797 | 896x |
dat$day < seq_timesplit[j + 1], timesplit] <- |
| 798 | 896x |
seq_timesplit[j] |
| 799 |
} |
|
| 800 | 13x |
dat[dat$day >= seq_timesplit[length(seq_timesplit)], timesplit] <- |
| 801 | 13x |
seq_timesplit[length(seq_timesplit)] |
| 802 | 13x |
dat[, "interv"] <- paste(dat[, "year"], dat[, timesplit]) |
| 803 | 13x |
res <- tapply(dat$value, dat[, "interv"], sum, na.rm = TRUE) |
| 804 | 13x |
datc <- |
| 805 | 13x |
data.frame( |
| 806 | 13x |
"year" = substr(names(res), 1, 4), |
| 807 | 13x |
timesplit = substr(names(res), 5, 15), |
| 808 | 13x |
"value" = as.numeric(res) |
| 809 |
) |
|
| 810 | 13x |
colnames(datc)[2] <- timesplit |
| 811 | 13x |
dat <- datc |
| 812 | 13x |
rm(datc) |
| 813 |
} else {
|
|
| 814 |
# if null default value is day |
|
| 815 | 6x |
timesplit <- "day" |
| 816 | 6x |
day2000 <- as.Date(seq.POSIXt( |
| 817 | 6x |
from = strptime("2000-01-01", format = '%Y-%m-%d'),
|
| 818 | 6x |
to = strptime("2000-12-31", format = '%Y-%m-%d'),
|
| 819 | 6x |
by = "day" |
| 820 |
)) |
|
| 821 | 6x |
for (j in unique(dat$year)) {
|
| 822 |
# days without report are added with a zero |
|
| 823 | 118x |
day2000remaining <- |
| 824 | 118x |
day2000[!day2000 %in% dat[dat$year == j, "day"]] |
| 825 | 118x |
dat0 <- data.frame("day" = day2000remaining,
|
| 826 | 118x |
"year" = j, |
| 827 | 118x |
"value" = NA) |
| 828 | 118x |
dat <- rbind(dat, dat0) |
| 829 | 6x |
} # end for |
| 830 |
} |
|
| 831 |
|
|
| 832 | 19x |
maxdat <- |
| 833 | 19x |
suppressWarnings(tapply(dat$value, as.character(dat[, timesplit]), max, na.rm = |
| 834 | 19x |
TRUE)) |
| 835 | 19x |
mindat <- |
| 836 | 19x |
suppressWarnings(tapply(dat$value, as.character(dat[, timesplit]), min, na.rm = |
| 837 | 19x |
TRUE)) |
| 838 | 19x |
meandat <- |
| 839 | 19x |
suppressWarnings(tapply(dat$value, as.character(dat[, timesplit]), mean, na.rm = |
| 840 | 19x |
TRUE)) |
| 841 | 19x |
datsummary <- |
| 842 | 19x |
data.frame("maxtab" = maxdat,
|
| 843 | 19x |
"mintab" = mindat, |
| 844 | 19x |
"mean" = meandat) |
| 845 | 19x |
datsummary <- |
| 846 | 19x |
datsummary[!is.infinite(datsummary$maxtab), ]# the minimum and max of empty set are -Inf and Inf respectively |
| 847 | 19x |
datsummary[, timesplit] <- names(maxdat)[!is.infinite(maxdat)] |
| 848 | 19x |
dat[, timesplit] <- as.character(dat[, timesplit]) |
| 849 | 19x |
dat <- merge(dat, datsummary, by = timesplit) |
| 850 | 19x |
dat[, timesplit] <- |
| 851 | 19x |
as.POSIXct(strptime(dat[, timesplit], format = '%Y-%m-%d')) # le format Posixct est necessaire pour les ggplot |
| 852 | 19x |
rm(maxdat, mindat, meandat) |
| 853 | 19x |
dat <- dat[order(dat$year, dat[, timesplit]), ] |
| 854 |
# this return the first occurence for each day, |
|
| 855 |
# for any day , min, max and mean are OK |
|
| 856 | 19x |
return(dat) |
| 857 |
|
|
| 858 |
} else {
|
|
| 859 | ! |
funout( |
| 860 | ! |
gettext( |
| 861 | ! |
"Attention : you have to complete a migration summary for at least one of the selected year before launching a inter-annual summary", |
| 862 | ! |
domain = "R-stacomiR" |
| 863 |
), |
|
| 864 | ! |
arret = TRUE |
| 865 |
) |
|
| 866 | 19x |
}# end else |
| 867 |
} |
|
| 868 | ||
| 869 |
#' Plot method for report_mig_interannual |
|
| 870 |
#' |
|
| 871 |
#' Several of these plots are scaled against the same year,i.e.the comparison is based on |
|
| 872 |
#' year 2000, meaning that day 1 would correspond to the first date of 2000, which is also a |
|
| 873 |
#' saturday, the last day of the week. |
|
| 874 |
#' @param x An object of class \link{report_mig_interannual-class}
|
|
| 875 |
#' @param plot.type Default standard |
|
| 876 |
#' @param timesplit Used for plot.type barchart or dotplot, Default month other possible values are day, week, 2 weeks, month |
|
| 877 |
#' French values "jour" "semaine" "quinzaine" "mois" are also accepted. |
|
| 878 |
#' @param silent Stops displaying the messages. |
|
| 879 |
#' \itemize{
|
|
| 880 |
#' \item{plot.type="line": one line per daily report_mig}
|
|
| 881 |
#' \item{plot.type="standard": the current year is displayed against a ribbon of historical values"}
|
|
| 882 |
#' \item{plot.type="density": creates density plot to compare seasonality, data computed by 15 days period}
|
|
| 883 |
#' \item{plot.type="step" : creates step plots to compare seasonality, the year chosen in the interface is the
|
|
| 884 |
#' latest if silent=TRUE, or it can be selected in the droplist. It is highlighted against the other with a dotted line} |
|
| 885 |
#' \item{plot.type="barchart": comparison of daily migration of one year against periodic migration for the other years available in the chronicle,
|
|
| 886 |
#' different periods can be chosen with argument timesplit} |
|
| 887 |
#' \item{plot.type="pointrange": Pointrange graphs, different periods can be chosen with argument timesplit}
|
|
| 888 |
#' \item{plot.type="seasonal": plot to display summary statistics about the migration period}
|
|
| 889 |
#' } |
|
| 890 |
#' @return Nothing, called for its side effect of plotting |
|
| 891 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 892 |
#' @aliases plot.report_mig_interannual |
|
| 893 |
#' @export |
|
| 894 |
setMethod( |
|
| 895 |
"plot", |
|
| 896 |
signature(x = "report_mig_interannual", y = "missing"), |
|
| 897 |
definition = function(x, |
|
| 898 |
plot.type = "standard", |
|
| 899 |
timesplit = "month", |
|
| 900 |
silent = FALSE) {
|
|
| 901 |
#report_mig_interannual<-r_mig_interannual |
|
| 902 | 20x |
report_mig_interannual <- x |
| 903 | 20x |
if (!timesplit %in% c( |
| 904 | 20x |
"jour", |
| 905 | 20x |
"day", |
| 906 | 20x |
"month", |
| 907 | 20x |
"mois", |
| 908 | 20x |
"week", |
| 909 | 20x |
"semaine", |
| 910 | 20x |
"month", |
| 911 | 20x |
"mois", |
| 912 | 20x |
"quinzaine", |
| 913 | 20x |
"2 weeks" |
| 914 |
)) |
|
| 915 | 20x |
stop ( |
| 916 | 20x |
stringr::str_c( |
| 917 | 20x |
"timesplit should be one of :", |
| 918 | 20x |
"jour ", |
| 919 | 20x |
"day ", |
| 920 | 20x |
"month ", |
| 921 | 20x |
"mois ", |
| 922 | 20x |
"week ", |
| 923 | 20x |
"semaine ", |
| 924 | 20x |
"month ", |
| 925 | 20x |
"mois ", |
| 926 | 20x |
"quinzaine ", |
| 927 | 20x |
"2 weeks " |
| 928 |
) |
|
| 929 |
) |
|
| 930 |
# back to French labels for consistency with fun_report_mig_interannual code |
|
| 931 | 20x |
timesplit <- |
| 932 | 20x |
switch( |
| 933 | 20x |
timesplit, |
| 934 | 20x |
"jour" = "day", |
| 935 | 20x |
"semaine" = "week", |
| 936 | 20x |
"mois" = "month", |
| 937 | 20x |
"quinzaine"= "2 weeks", |
| 938 | 20x |
timesplit |
| 939 |
) |
|
| 940 |
|
|
| 941 |
# plot.type="line";require(ggplot2) |
|
| 942 |
|
|
| 943 | 20x |
if (nrow(report_mig_interannual@data) > 0) {
|
| 944 |
|
|
| 945 | 19x |
if (plot.type == "line") {
|
| 946 | 3x |
dat <- report_mig_interannual@data |
| 947 | 3x |
dat <- dat[dat$bjo_labelquantite == "Effectif_total", ] |
| 948 | 3x |
dat <- stacomirtools::chnames( |
| 949 | 3x |
dat, |
| 950 | 3x |
c( |
| 951 | 3x |
"bjo_annee", |
| 952 | 3x |
"bjo_jour", |
| 953 | 3x |
"bjo_labelquantite", |
| 954 | 3x |
"bjo_valeur" |
| 955 |
), |
|
| 956 | 3x |
c("year", "day", "labelquantity", "value")
|
| 957 |
) |
|
| 958 |
# we need to choose a date, every year brought back to 2000 |
|
| 959 | 3x |
dat$day <- as.POSIXct(strptime(strftime(dat$day, |
| 960 | 3x |
'2000-%m-%d %H:%M:%S'), |
| 961 | 3x |
format = '%Y-%m-%d %H:%M:%S'), tz = "GMT") |
| 962 | 3x |
dat$year <- as.factor(dat$year) |
| 963 | 3x |
dat <- stacomirtools::killfactor(dat) |
| 964 | 3x |
titre = paste( |
| 965 | 3x |
gettext("Migration ", domain="R-stacomiR"),
|
| 966 | 3x |
paste(min(dat$year), max(dat$year), collapse = "-"), |
| 967 |
", ", |
|
| 968 | 3x |
paste(report_mig_interannual@dc@data$dis_commentaires[report_mig_interannual@dc@data$dc %in% |
| 969 | 3x |
report_mig_interannual@dc@dc_selected], collapse="+"), |
| 970 | 3x |
sep="" |
| 971 |
) |
|
| 972 | 3x |
soustitre = paste( |
| 973 | 3x |
report_mig_interannual@taxa@data[ |
| 974 | 3x |
report_mig_interannual@taxa@data$tax_code %in% |
| 975 | 3x |
report_mig_interannual@taxa@taxa_selected, |
| 976 | 3x |
"tax_nom_latin"], |
| 977 |
", ", |
|
| 978 | 3x |
report_mig_interannual@stage@data[ |
| 979 | 3x |
report_mig_interannual@stage@data$std_code %in% |
| 980 | 3x |
report_mig_interannual@stage@stage_selected, |
| 981 | 3x |
"std_libelle"], |
| 982 |
", ", |
|
| 983 | 3x |
sep = "" |
| 984 |
) |
|
| 985 | 3x |
g <- ggplot(dat, aes(x = day, y = value)) |
| 986 | 3x |
g <- |
| 987 | 3x |
g + geom_line(aes(color = year)) + labs(title = paste(titre, "\n", soustitre)) + |
| 988 | 3x |
scale_x_datetime(name = "date", date_breaks = "1 month", |
| 989 | 3x |
date_labels = "%b") + |
| 990 | 3x |
theme_bw() |
| 991 | 3x |
print(g) |
| 992 | 3x |
assign("g", g, envir = envir_stacomi)
|
| 993 | 3x |
if (!silent) |
| 994 | 3x |
funout( |
| 995 | 3x |
gettext( |
| 996 | 3x |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 997 | 3x |
domain = "R-stacomiR" |
| 998 |
) |
|
| 999 |
) |
|
| 1000 |
#---------------------------------------------- |
|
| 1001 | 19x |
} else if (plot.type == "standard") {
|
| 1002 | 2x |
dat <- report_mig_interannual@data |
| 1003 | 2x |
if (silent == FALSE) {
|
| 1004 | ! |
the_choice <- |
| 1005 | ! |
as.numeric( |
| 1006 | ! |
select.list( |
| 1007 | ! |
choices = as.character(unique(dat$bjo_annee)[order(unique(dat$bjo_annee))]), |
| 1008 | ! |
preselect = as.character(max(dat$bjo_annee)), |
| 1009 | ! |
gettext("Year choice", domain="R-stacomiR"),
|
| 1010 | ! |
multiple = FALSE |
| 1011 |
) |
|
| 1012 |
) |
|
| 1013 |
} else {
|
|
| 1014 | 2x |
the_choice <- max(dat$bjo_annee) |
| 1015 |
} |
|
| 1016 |
# dataset for current year |
|
| 1017 | 2x |
dat0 <- |
| 1018 | 2x |
fun_report_mig_interannual(dat, year = NULL, timesplit = NULL) |
| 1019 | 2x |
dat <- |
| 1020 | 2x |
fun_report_mig_interannual(dat, year = the_choice, timesplit = NULL) |
| 1021 | 2x |
dat <- |
| 1022 | 2x |
dat[dat$mean != 0, ] # pour des raisons graphiques on ne garde pas les effectifs nuls generes par fun_report_mig_interannual |
| 1023 | 2x |
newdat <- |
| 1024 | 2x |
dat[match(unique(as.character(dat$day)), as.character(dat$day)), ] |
| 1025 | 2x |
newdat <- |
| 1026 | 2x |
newdat[order(newdat$day), ] # pour avoir les range sur l'ensemble des valeurs dispo et pas seult l'annee en cours |
| 1027 | 2x |
amplitude = paste(min(as.numeric(as.character(dat$year))), "-", max(as.numeric(as.character(dat$year))), sep = |
| 1028 |
"") |
|
| 1029 | 2x |
if (length(the_choice) > 0) {
|
| 1030 | 2x |
vplayout <- |
| 1031 | 2x |
function(x, y) {
|
| 1032 | 2x |
grid::viewport(layout.pos.row = x, |
| 1033 | 2x |
layout.pos.col = y) |
| 1034 |
} |
|
| 1035 | 2x |
grid::grid.newpage() |
| 1036 | 2x |
grid::pushViewport(grid::viewport(layout = grid::grid.layout(length(the_choice), 1, just = |
| 1037 | 2x |
"center"))) |
| 1038 | 2x |
amplitudechoice <- paste(the_choice, '/', amplitude) |
| 1039 | 2x |
tmp <- dat0[as.numeric(as.character(dat0$year)) == the_choice, ] |
| 1040 | 2x |
tmp$year <- as.character(tmp$year) |
| 1041 | 2x |
g <- ggplot(newdat, aes(x = day)) |
| 1042 | 2x |
g <- |
| 1043 | 2x |
g + geom_ribbon( |
| 1044 | 2x |
aes( |
| 1045 | 2x |
ymin = mintab, |
| 1046 | 2x |
ymax = maxtab, |
| 1047 | 2x |
fill = "amplitude" |
| 1048 |
), |
|
| 1049 | 2x |
color = "grey20", |
| 1050 | 2x |
alpha = 0.5 |
| 1051 |
) |
|
| 1052 | 2x |
g <- |
| 1053 | 2x |
g + geom_bar( |
| 1054 | 2x |
aes(y = value, fill = I("orange")),
|
| 1055 | 2x |
position = "dodge", |
| 1056 | 2x |
stat = "identity", |
| 1057 | 2x |
color = "grey20", |
| 1058 | 2x |
alpha = 0.8, |
| 1059 | 2x |
data = tmp |
| 1060 |
) |
|
| 1061 | 2x |
g <- |
| 1062 | 2x |
g + scale_fill_manual( |
| 1063 | 2x |
name = eval(amplitudechoice), |
| 1064 | 2x |
values = c("#35789C", "orange"),
|
| 1065 | 2x |
labels = c( |
| 1066 | 2x |
gettext("Historical amplitude", domain = "R-StacomiR"),
|
| 1067 | 2x |
the_choice |
| 1068 |
) |
|
| 1069 |
) |
|
| 1070 |
#g <- g+geom_point(aes(y=value,col=year),data=tmp,pch=16,size=1) |
|
| 1071 |
# moyenne interannuelle |
|
| 1072 |
|
|
| 1073 | 2x |
g <- g + geom_line(aes(y = mean, col = I("#002743")), data = newdat)
|
| 1074 | 2x |
g <- |
| 1075 | 2x |
g + geom_point(aes(y = mean, col = I("#002743")),
|
| 1076 | 2x |
size = 1.2, |
| 1077 | 2x |
data = newdat) |
| 1078 | 2x |
g <- |
| 1079 | 2x |
g + scale_colour_manual( |
| 1080 | 2x |
name = eval(amplitudechoice), |
| 1081 | 2x |
values = c("#002743"),
|
| 1082 | 2x |
labels = c(stringr::str_c( |
| 1083 | 2x |
gettext("Interannual mean\n", domain = "R-stacomiR"),
|
| 1084 | 2x |
amplitude |
| 1085 |
)) |
|
| 1086 |
) + |
|
| 1087 | 2x |
guides(fill = guide_legend(reverse = TRUE)) |
| 1088 | 2x |
g <- |
| 1089 | 2x |
g + labs( |
| 1090 | 2x |
title = paste( |
| 1091 | 2x |
paste(report_mig_interannual@dc@dc_selected,collapse="+"), |
| 1092 | 2x |
report_mig_interannual@taxa@data[ |
| 1093 | 2x |
report_mig_interannual@taxa@data$tax_code %in% |
| 1094 | 2x |
report_mig_interannual@taxa@taxa_selected, |
| 1095 | 2x |
"tax_nom_latin"], |
| 1096 |
",", |
|
| 1097 | 2x |
report_mig_interannual@stage@data[ |
| 1098 | 2x |
report_mig_interannual@stage@data$std_code %in% |
| 1099 | 2x |
report_mig_interannual@stage@stage_selected, |
| 1100 | 2x |
"std_libelle"], |
| 1101 |
",", |
|
| 1102 | 2x |
paste(newdat$year), |
| 1103 |
"/", |
|
| 1104 | 2x |
amplitude |
| 1105 |
) |
|
| 1106 |
) |
|
| 1107 | 2x |
g <- |
| 1108 | 2x |
g + scale_x_datetime( |
| 1109 | 2x |
name = "date", |
| 1110 | 2x |
date_breaks = "months", |
| 1111 | 2x |
date_minor_breaks = "weeks", |
| 1112 | 2x |
date_labels = "%d-%m" |
| 1113 |
) |
|
| 1114 | 2x |
g <- g + theme_bw() + theme(legend.key = element_blank()) |
| 1115 | 2x |
print(g, vp = vplayout(1, 1)) |
| 1116 | 2x |
assign(paste("g", 1, sep = ""), g, envir_stacomi)
|
| 1117 | 2x |
if (!silent) |
| 1118 | 2x |
funout( |
| 1119 | 2x |
gettextf( |
| 1120 | 2x |
"Writing the graphical object into envir_stacomi environment : write g=get(\"gi\",envir_stacomi) with i=%s", |
| 1121 | 2x |
paste(1:length(the_choice), collapse = ",") |
| 1122 |
) |
|
| 1123 |
) |
|
| 1124 |
|
|
| 1125 |
|
|
| 1126 | 2x |
} # end if plot==standard |
| 1127 |
#---------------------------------------------- |
|
| 1128 | 19x |
} else if (plot.type == "step") {
|
| 1129 | 2x |
dat <- report_mig_interannual@data |
| 1130 | 2x |
dat <- fun_report_mig_interannual(dat) |
| 1131 |
# runs the default with daily migration |
|
| 1132 |
#dat=dat[order(dat$year,dat$day),] |
|
| 1133 | 2x |
dat$value[is.na(dat$value)] <-0 |
| 1134 |
# otherwise if only one line it may crash |
|
| 1135 | 2x |
if (silent == FALSE) {
|
| 1136 | ! |
the_choice <- select.list( |
| 1137 | ! |
choices = as.character(unique(dat$year)), |
| 1138 | ! |
preselect = as.character(max(dat$year)), |
| 1139 | ! |
multiple = FALSE, |
| 1140 | ! |
title = gettext("Choose year", domain = "R-StacomirR")
|
| 1141 |
) |
|
| 1142 |
} else {
|
|
| 1143 | 2x |
the_choice <- max(as.numeric(as.character(dat$year))) |
| 1144 |
} |
|
| 1145 | 2x |
amplitude <- paste(min(as.numeric(as.character(dat$year))), |
| 1146 | 2x |
"-", max(as.numeric(as.character(dat$year))), sep = "") |
| 1147 |
################# |
|
| 1148 |
# calculation of cumsums |
|
| 1149 |
################### |
|
| 1150 |
|
|
| 1151 | 2x |
for (an in unique(dat$year)) {
|
| 1152 |
# an=as.character(unique(dat$year)) ;an<-an[1] |
|
| 1153 | 40x |
dat[dat$year == an, "cumsum"] <- |
| 1154 | 40x |
cumsum(dat[dat$year == an, "value"]) |
| 1155 | 40x |
dat[dat$year == an, "total_annuel"] <- |
| 1156 | 40x |
max(dat[dat$year == an, "cumsum"]) |
| 1157 |
} |
|
| 1158 | 2x |
dat$cumsum <- dat$cumsum / dat$total_annuel |
| 1159 | 2x |
dat$day <- as.Date(dat$day) |
| 1160 | 2x |
dat$year <- as.factor(dat$year) |
| 1161 |
|
|
| 1162 |
################# |
|
| 1163 |
# plot |
|
| 1164 |
################### |
|
| 1165 |
|
|
| 1166 | 2x |
g <- ggplot(dat, aes(x = day, y = cumsum)) |
| 1167 | 2x |
tmp <- |
| 1168 | 2x |
dat[as.numeric(as.character(dat$year)) == as.numeric(the_choice), ] |
| 1169 | 2x |
g <- g + geom_step(aes(col = year, size = total_annuel)) |
| 1170 | 2x |
g <- g + geom_step(data = tmp, |
| 1171 | 2x |
col = "black", |
| 1172 | 2x |
lty = 2) |
| 1173 | 2x |
g <- |
| 1174 | 2x |
g + labs( |
| 1175 | 2x |
title = gettextf( |
| 1176 | 2x |
"%s, %s, %s cum %s", |
| 1177 | 2x |
paste(report_mig_interannual@dc@dc_selected, collapse="+"), |
| 1178 | 2x |
report_mig_interannual@taxa@data[ |
| 1179 | 2x |
report_mig_interannual@taxa@data$tax_code %in% |
| 1180 | 2x |
report_mig_interannual@taxa@taxa_selected, |
| 1181 | 2x |
"tax_nom_latin"], |
| 1182 | 2x |
report_mig_interannual@stage@data[ |
| 1183 | 2x |
report_mig_interannual@stage@data$std_code %in% |
| 1184 | 2x |
report_mig_interannual@stage@stage_selected, |
| 1185 | 2x |
"std_libelle"], |
| 1186 | 2x |
amplitude |
| 1187 |
) |
|
| 1188 |
) |
|
| 1189 | 2x |
g <- |
| 1190 | 2x |
g + scale_y_continuous(name = gettext("Annual migration percentage", domain =
|
| 1191 | 2x |
"R-stacomiR")) |
| 1192 | 2x |
g <- |
| 1193 | 2x |
g + scale_x_date( |
| 1194 | 2x |
name = gettext("date", domain = "R-stacomiR"),
|
| 1195 | 2x |
date_breaks = "months", |
| 1196 | 2x |
date_minor_breaks = "weeks", |
| 1197 | 2x |
date_labels = "%b", |
| 1198 | 2x |
limits = range(dat[dat$value > 0 & |
| 1199 | 2x |
dat$cumsum != 1, "day"]) |
| 1200 | 2x |
)# date |
| 1201 | 2x |
g <- |
| 1202 | 2x |
g + scale_colour_hue( |
| 1203 | 2x |
name = gettext("year", domain = "R-stacomiR"),
|
| 1204 | 2x |
l = 70, |
| 1205 | 2x |
c = 150 |
| 1206 | 2x |
)# year |
| 1207 | 2x |
print(g) |
| 1208 | 2x |
assign("g", g, envir_stacomi)
|
| 1209 | 2x |
if (!silent) |
| 1210 | 2x |
funout( |
| 1211 | 2x |
gettext( |
| 1212 | 2x |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 1213 | 2x |
domain = "R-stacomiR" |
| 1214 |
) |
|
| 1215 |
) |
|
| 1216 |
#---------------------------------------------- |
|
| 1217 | 19x |
} else if (plot.type == "barchart") {
|
| 1218 | 2x |
dat = report_mig_interannual@data |
| 1219 | 2x |
if (silent == FALSE) {
|
| 1220 | ! |
the_choice = select.list( |
| 1221 | ! |
choices = as.character(unique(dat$bjo_annee)), |
| 1222 | ! |
preselect = as.character(max(dat$bjo_annee)), |
| 1223 | ! |
multiple = FALSE, |
| 1224 | ! |
title = gettext("Choose year", domain = "R-StacomiR")
|
| 1225 |
) |
|
| 1226 |
} else {
|
|
| 1227 | 2x |
the_choice = max(as.numeric(as.character(dat$bjo_annee))) |
| 1228 |
} |
|
| 1229 | 2x |
dat0 <- fun_report_mig_interannual(dat, timesplit = timesplit) |
| 1230 | 2x |
dat <- |
| 1231 | 2x |
fun_report_mig_interannual(dat, year = the_choice, timesplit = timesplit) |
| 1232 | 2x |
prepare_dat <- function(dat) {
|
| 1233 | 4x |
dat <- dat[order(dat$year, dat[, timesplit]), ] |
| 1234 | 4x |
dat$year <- as.factor(dat$year) |
| 1235 | 4x |
dat$keeptimesplit <- dat[, timesplit] |
| 1236 | 4x |
if (timesplit == "mois") {
|
| 1237 | ! |
dat[, timesplit] <- strftime(dat[, timesplit], format = "%m") |
| 1238 | 4x |
} else if (timesplit == "quinzaine") {
|
| 1239 | ! |
dat[, timesplit] <- strftime(dat[, timesplit], format = "%m/%d") |
| 1240 |
} else {
|
|
| 1241 | 4x |
dat[, timesplit] <- strftime(dat[, timesplit], format = "%W") |
| 1242 |
} |
|
| 1243 | 4x |
dat[, timesplit] <- as.factor(dat[, timesplit]) |
| 1244 |
# we only keep one per week |
|
| 1245 | 4x |
newdat <- dat[match(unique(dat[, timesplit]), dat[, timesplit]), ] |
| 1246 | 4x |
newdat <- |
| 1247 | 4x |
newdat[order(newdat[, "keeptimesplit"]), ] # pour avoir les range sur l'ensemble des valeurs dispo et pas seult l'annee en cours |
| 1248 |
# here change 12/2012 the geom_crossbar now needs a factor, label change according to timesplit |
|
| 1249 | 4x |
newdat[, timesplit] <- as.factor(newdat[, timesplit]) |
| 1250 | 4x |
levels(newdat[, timesplit]) <- |
| 1251 | 4x |
newdat[, timesplit] # to have the factor in the right order from january to dec |
| 1252 | 4x |
return(newdat) |
| 1253 |
} |
|
| 1254 | 2x |
amplitude <- paste(min(as.numeric(as.character(dat$year))), |
| 1255 |
"-", |
|
| 1256 | 2x |
max(as.numeric(as.character(dat$year))), |
| 1257 | 2x |
sep = "") |
| 1258 |
|
|
| 1259 | 2x |
newdat <- prepare_dat(dat) |
| 1260 | 2x |
newdat0 <- prepare_dat(dat0) |
| 1261 | 2x |
if (length(the_choice) > 0) {
|
| 1262 |
# le layout pour l'affichage des graphiques |
|
| 1263 | 2x |
vplayout <- |
| 1264 | 2x |
function(x, y) {
|
| 1265 | 2x |
grid::viewport(layout.pos.row = x, |
| 1266 | 2x |
layout.pos.col = y) |
| 1267 |
} |
|
| 1268 | 2x |
grid::grid.newpage() |
| 1269 | 2x |
grid::pushViewport(grid::viewport(layout = |
| 1270 | 2x |
grid::grid.layout(length(the_choice), 1, just = "center"))) |
| 1271 | 2x |
selection <- |
| 1272 | 2x |
as.numeric(as.character(dat0$year)) == as.numeric(the_choice) |
| 1273 | 2x |
tmp <- dat0[selection, ] |
| 1274 | 2x |
tmp[tmp$value >= tmp$mean, "comp"] <- ">=moy" |
| 1275 | 2x |
tmp[tmp$value < tmp$mean, "comp"] <- "<moy" |
| 1276 | 2x |
suppressWarnings({
|
| 1277 | 2x |
tmp[tmp$value == tmp$maxtab, "comp"] <- "max" |
| 1278 | 2x |
tmp[tmp$value == tmp$mintab, "comp"] <- "min" |
| 1279 |
}) |
|
| 1280 | 2x |
tmp[tmp$mean == 0, "comp"] <- "0" |
| 1281 |
|
|
| 1282 | 2x |
tmp$year <- as.factor(as.numeric(as.character(tmp$year))) |
| 1283 | 2x |
if (timesplit == "mois") {
|
| 1284 | ! |
tmp[, timesplit] <- strftime(tmp[, timesplit], format = "%m") |
| 1285 | 2x |
} else if (timesplit == "quinzaine") {
|
| 1286 | ! |
tmp[, timesplit] <- strftime(tmp[, timesplit], format = "%m/%d") |
| 1287 |
} else {
|
|
| 1288 | 2x |
tmp[, timesplit] <- strftime(tmp[, timesplit], format = "%W") |
| 1289 |
} |
|
| 1290 | 2x |
tmp[, timesplit] <- as.factor(tmp[, timesplit]) |
| 1291 | 2x |
tmp[!tmp[, timesplit] %in% newdat[, timesplit], "comp"] <- "?" |
| 1292 | 2x |
newdat$comp <- NA |
| 1293 |
|
|
| 1294 | 2x |
g <- ggplot(tmp, aes_string(x = timesplit, y = "value")) |
| 1295 | 2x |
g <- g + geom_crossbar( |
| 1296 | 2x |
data = newdat, |
| 1297 | 2x |
aes_string( |
| 1298 | 2x |
x = timesplit, |
| 1299 | 2x |
y = "mean", |
| 1300 | 2x |
ymin = "mintab", |
| 1301 | 2x |
ymax = "maxtab" |
| 1302 |
), |
|
| 1303 | 2x |
fill = "grey60", |
| 1304 | 2x |
alpha = 0.5, |
| 1305 | 2x |
size = 0.5, |
| 1306 | 2x |
fatten = 3, |
| 1307 | 2x |
col = "grey60" |
| 1308 |
) |
|
| 1309 | 2x |
g <- |
| 1310 | 2x |
g + geom_bar( |
| 1311 | 2x |
stat = "identity", |
| 1312 | 2x |
aes_string(y = "value", col = "comp"), |
| 1313 | 2x |
fill = NA, |
| 1314 | 2x |
width = 0.6 |
| 1315 |
) |
|
| 1316 | 2x |
g <- |
| 1317 | 2x |
g + geom_bar( |
| 1318 | 2x |
stat = "identity", |
| 1319 | 2x |
aes_string(y = "value", fill = "comp"), |
| 1320 | 2x |
alpha = 0.5, |
| 1321 | 2x |
width = 0.6 |
| 1322 |
) |
|
| 1323 |
#g <- g+scale_x_date(name=paste("mois"),breaks="month",minor_breaks=getvalue(new("ref_period"),label=date_format("%b"),timesplit))
|
|
| 1324 |
#lim=as.POSIXct(c(Hmisc::truncPOSIXt((min(tmp[tmp$com!="0",timesplit])),"month")-delai, |
|
| 1325 |
# Hmisc::ceil((max(tmp[tmp$com!="0",timesplit])),"month")+delai)) |
|
| 1326 |
# pb the limit truncs the value |
|
| 1327 | 2x |
g <- g + ylab("effectif")
|
| 1328 | 2x |
cols <- c( |
| 1329 | 2x |
"max" = "#000080", |
| 1330 | 2x |
"min" = "#BF0000", |
| 1331 | 2x |
">=moy" = "darkgreen", |
| 1332 | 2x |
"<moy" = "darkorange", |
| 1333 | 2x |
"hist_mean" = "black", |
| 1334 | 2x |
"hist_range" = "grey", |
| 1335 | 2x |
"?" = "darkviolet" |
| 1336 |
) |
|
| 1337 | 2x |
fills <- c( |
| 1338 | 2x |
"max" = "blue", |
| 1339 | 2x |
"min" = "red", |
| 1340 | 2x |
">=moy" = "green", |
| 1341 | 2x |
"<moy" = "orange", |
| 1342 | 2x |
"hist_mean" = "black", |
| 1343 | 2x |
"hist_range" = "grey", |
| 1344 | 2x |
"?" = "violet" |
| 1345 |
) |
|
| 1346 |
|
|
| 1347 | 2x |
g <- g + scale_colour_manual( |
| 1348 | 2x |
name = the_choice, |
| 1349 | 2x |
values = cols, |
| 1350 | 2x |
limits = c( |
| 1351 | 2x |
"min", |
| 1352 | 2x |
"max", |
| 1353 | 2x |
"<moy", |
| 1354 | 2x |
">=moy", |
| 1355 | 2x |
"hist_mean", |
| 1356 | 2x |
"hist_range", |
| 1357 |
"?" |
|
| 1358 |
) |
|
| 1359 |
) |
|
| 1360 | 2x |
g <- g + scale_fill_manual( |
| 1361 | 2x |
name = the_choice, |
| 1362 | 2x |
values = fills, |
| 1363 | 2x |
limits = c( |
| 1364 | 2x |
"min", |
| 1365 | 2x |
"max", |
| 1366 | 2x |
"<moy", |
| 1367 | 2x |
">=moy", |
| 1368 | 2x |
"hist_mean", |
| 1369 | 2x |
"hist_range", |
| 1370 |
"?" |
|
| 1371 |
) |
|
| 1372 |
) |
|
| 1373 |
|
|
| 1374 | 2x |
g <- |
| 1375 | 2x |
g + labs( |
| 1376 | 2x |
title = paste( report_mig_interannual@taxa@data[ |
| 1377 | 2x |
report_mig_interannual@taxa@data$tax_code %in% |
| 1378 | 2x |
report_mig_interannual@taxa@taxa_selected, |
| 1379 | 2x |
"tax_nom_latin"], |
| 1380 |
",", |
|
| 1381 | 2x |
report_mig_interannual@stage@data[ |
| 1382 | 2x |
report_mig_interannual@stage@data$std_code %in% |
| 1383 | 2x |
report_mig_interannual@stage@stage_selected, |
| 1384 | 2x |
"std_libelle"], |
| 1385 | 2x |
", bilan par", |
| 1386 | 2x |
timesplit, |
| 1387 | 2x |
unique(as.character(tmp$year)), |
| 1388 |
"/", |
|
| 1389 | 2x |
amplitude |
| 1390 |
) |
|
| 1391 |
) |
|
| 1392 | 2x |
g <- g + theme_minimal() |
| 1393 | 2x |
print(g, vp = vplayout(1, 1)) |
| 1394 | 2x |
assign(paste("g", 1, sep = ""), g, envir_stacomi)
|
| 1395 | 2x |
if (!silent) |
| 1396 | 2x |
funout( |
| 1397 | 2x |
gettextf( |
| 1398 | 2x |
"\"Writing the graphical object into envir_stacomi environment : write g=get(\"gi\",envir_stacomi) with i=%s", |
| 1399 | 2x |
paste(1:length(the_choice), collapse = ",") |
| 1400 |
) |
|
| 1401 |
) |
|
| 1402 |
|
|
| 1403 | 2x |
} # end if |
| 1404 |
|
|
| 1405 |
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
| 1406 | 19x |
} else if (plot.type == "pointrange") {
|
| 1407 |
# below before several plots could be made, it's no longer the case |
|
| 1408 |
# as I remove the chosen year from the observation (reference) set |
|
| 1409 | 2x |
dat = report_mig_interannual@data |
| 1410 |
|
|
| 1411 | 2x |
if (silent == FALSE) {
|
| 1412 | ! |
the_choice <- |
| 1413 | ! |
select.list( |
| 1414 | ! |
choices = as.character(unique(dat$bjo_annee)), |
| 1415 | ! |
preselect = as.character(max(dat$bjo_annee)), |
| 1416 | ! |
gettext("Year choice", domain = "R-stacomiR"),
|
| 1417 | ! |
multiple = FALSE |
| 1418 |
) |
|
| 1419 |
} else {
|
|
| 1420 | 2x |
the_choice <- max(dat$bjo_annee) |
| 1421 |
} |
|
| 1422 | 2x |
dat0 <- fun_report_mig_interannual(dat, timesplit = timesplit) |
| 1423 | 2x |
dat <- |
| 1424 | 2x |
fun_report_mig_interannual(dat, year = the_choice, timesplit = timesplit) |
| 1425 | 2x |
dat$year <- as.factor(dat$year) |
| 1426 | 2x |
dat <- dat[order(dat$year, dat[, timesplit]), ] |
| 1427 | 2x |
dat$keeptimesplit <- dat[, timesplit] |
| 1428 | 2x |
if (timesplit == "mois") {
|
| 1429 | ! |
dat[, timesplit] <- strftime(dat[, timesplit], format = "%m") |
| 1430 | 2x |
} else if (timesplit == "quinzaine") {
|
| 1431 | ! |
dat[, timesplit] <- strftime(dat[, timesplit], format = "%m/%d") |
| 1432 |
} else {
|
|
| 1433 | 2x |
dat[, timesplit] <- strftime(dat[, timesplit], format = "%W") |
| 1434 |
} |
|
| 1435 | 2x |
dat[, timesplit] <- as.factor(dat[, timesplit]) |
| 1436 |
|
|
| 1437 | 2x |
newdat <- dat[match(unique(dat[, timesplit]), dat[, timesplit]), ] |
| 1438 | 2x |
newdat <- |
| 1439 | 2x |
newdat[order(newdat[, "keeptimesplit"]), ] # il peut y avoir des annees pour le calcul de range qui s'ajoutent |
| 1440 |
# et viennent d'autres annees, il faut donc reordonner. |
|
| 1441 |
|
|
| 1442 |
|
|
| 1443 | 2x |
amplitude <- |
| 1444 | 2x |
paste(min(as.numeric(as.character(dat$year))), "-", max(as.numeric(as.character(dat$year))), sep = |
| 1445 |
"") |
|
| 1446 |
|
|
| 1447 |
|
|
| 1448 | 2x |
if (length(the_choice) > 0) {
|
| 1449 |
# le layout pour l'affichage des graphiques |
|
| 1450 | 2x |
vplayout <- |
| 1451 | 2x |
function(x, y) {
|
| 1452 | 2x |
grid::viewport(layout.pos.row = x, |
| 1453 | 2x |
layout.pos.col = y) |
| 1454 |
} |
|
| 1455 | 2x |
grid::grid.newpage() |
| 1456 | 2x |
grid::pushViewport(grid::viewport(layout = grid::grid.layout(length(the_choice), 1, just = |
| 1457 | 2x |
"center"))) |
| 1458 |
|
|
| 1459 | 2x |
selection <- |
| 1460 | 2x |
as.numeric(as.character(dat0$year)) == as.numeric(the_choice) |
| 1461 | 2x |
tmp <- dat0[selection, ] |
| 1462 | 2x |
tmp[tmp$value >= tmp$mean, "comp"] <- ">=moy" |
| 1463 | 2x |
tmp[tmp$value < tmp$mean, "comp"] <- "<moy" |
| 1464 | 2x |
suppressWarnings({
|
| 1465 | 2x |
tmp[tmp$value == tmp$maxtab, "comp"] <- "max" |
| 1466 | 2x |
tmp[tmp$value == tmp$mintab, "comp"] <- "min" |
| 1467 |
}) |
|
| 1468 | 2x |
tmp[tmp$mean == 0, "comp"] <- "0" |
| 1469 | 2x |
tmp$year = as.factor(as.numeric(as.character(tmp$year))) |
| 1470 | 2x |
if (timesplit == "mois") {
|
| 1471 | ! |
tmp[, timesplit] <- strftime(tmp[, timesplit], format = "%m") |
| 1472 | 2x |
} else if (timesplit == "quinzaine") {
|
| 1473 | ! |
tmp[, timesplit] <- strftime(tmp[, timesplit], format = "%m/%d") |
| 1474 |
} else {
|
|
| 1475 | 2x |
tmp[, timesplit] <- strftime(tmp[, timesplit], format = "%W") |
| 1476 |
} |
|
| 1477 | 2x |
tmp[, timesplit] <- as.factor(tmp[, timesplit]) |
| 1478 | 2x |
tmp[!tmp[, timesplit] %in% newdat[, timesplit], "comp"] <- "?" |
| 1479 | 2x |
newdat$comp <- NA |
| 1480 | 2x |
g <- ggplot(tmp, aes_string(x = timesplit, y = "value")) |
| 1481 | 2x |
g <- |
| 1482 | 2x |
g + geom_dotplot( |
| 1483 | 2x |
aes_string(x = timesplit, y = "value"), |
| 1484 | 2x |
data = dat, |
| 1485 | 2x |
stackdir = "center", |
| 1486 | 2x |
binaxis = "y", |
| 1487 | 2x |
position = "dodge", |
| 1488 | 2x |
dotsize = 0.5, |
| 1489 | 2x |
fill = "wheat" |
| 1490 | 2x |
) #position = "dodge",dotsize = 0.4,alpha=0.5,binwidth = 1.5 |
| 1491 | 2x |
g <- |
| 1492 | 2x |
g + geom_pointrange( |
| 1493 | 2x |
data = newdat, |
| 1494 | 2x |
aes_string( |
| 1495 | 2x |
x = timesplit, |
| 1496 | 2x |
y = "mean", |
| 1497 | 2x |
ymin = "mintab", |
| 1498 | 2x |
ymax = "maxtab" |
| 1499 |
), |
|
| 1500 | 2x |
alpha = 1, |
| 1501 | 2x |
size = 0.8 |
| 1502 |
) |
|
| 1503 | 2x |
g <- |
| 1504 | 2x |
g + geom_bar(stat = "identity", |
| 1505 | 2x |
aes_string(y = "value", fill = "comp"), |
| 1506 | 2x |
alpha = 0.6) |
| 1507 | 2x |
g <- g + scale_y_continuous(name = "effectif") |
| 1508 | 2x |
cols <- |
| 1509 | 2x |
c( |
| 1510 | 2x |
"max" = "blue", |
| 1511 | 2x |
"min" = "red", |
| 1512 | 2x |
">=moy" = "darkgreen", |
| 1513 | 2x |
"<moy" = "darkorange", |
| 1514 | 2x |
"0" = "grey10", |
| 1515 | 2x |
"?" = "darkviolet" |
| 1516 |
) |
|
| 1517 | 2x |
g <- g + scale_fill_manual(name = the_choice, values = cols) |
| 1518 | 2x |
g <- |
| 1519 | 2x |
g + labs( |
| 1520 | 2x |
title = paste( |
| 1521 | 2x |
report_mig_interannual@taxa@data[ |
| 1522 | 2x |
report_mig_interannual@taxa@data$tax_code %in% |
| 1523 | 2x |
report_mig_interannual@taxa@taxa_selected, |
| 1524 | 2x |
"tax_nom_latin"], |
| 1525 |
",", |
|
| 1526 | 2x |
report_mig_interannual@stage@data[ |
| 1527 | 2x |
report_mig_interannual@stage@data$std_code %in% |
| 1528 | 2x |
report_mig_interannual@stage@stage_selected, |
| 1529 | 2x |
"std_libelle"], |
| 1530 | 2x |
", report par", |
| 1531 | 2x |
timesplit, |
| 1532 | 2x |
unique(as.character(tmp$year)), |
| 1533 |
"/", |
|
| 1534 | 2x |
amplitude |
| 1535 |
) |
|
| 1536 |
) |
|
| 1537 | 2x |
g <- g + theme_minimal() |
| 1538 | 2x |
print(g, vp = vplayout(1, 1)) |
| 1539 | 2x |
assign(paste("g", 1, sep = ""), g, envir_stacomi)
|
| 1540 | 2x |
if (!silent) |
| 1541 | 2x |
funout( |
| 1542 | 2x |
gettextf( |
| 1543 | 2x |
"\"Writing the graphical object into envir_stacomi environment : write g=get(\"gi\",envir_stacomi) with i=%s", |
| 1544 | 2x |
paste(1:length(the_choice), collapse = ",") |
| 1545 |
) |
|
| 1546 |
) |
|
| 1547 |
|
|
| 1548 | 2x |
} # end if |
| 1549 |
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
| 1550 | 19x |
} else if (plot.type == "density") {
|
| 1551 | 3x |
if (nrow(report_mig_interannual@data) > 0) |
| 1552 |
{
|
|
| 1553 | 3x |
timesplit = "2 weeks" |
| 1554 | 3x |
dat <- report_mig_interannual@data |
| 1555 | 3x |
dat <- fun_report_mig_interannual(dat, year = NULL, timesplit) |
| 1556 | 3x |
dat$year <- as.factor(dat$year) |
| 1557 | 3x |
sum_per_year <- tapply(dat$value, dat$year, sum) |
| 1558 | 3x |
sum_per_year <- |
| 1559 | 3x |
data.frame(year = names(sum_per_year), |
| 1560 | 3x |
sum_per_year = sum_per_year) |
| 1561 | 3x |
dat <- merge(dat, sum_per_year, by = "year") |
| 1562 | 3x |
dat$std_value <- dat$value / dat$sum_per_year |
| 1563 | 3x |
dat <- chnames(dat, "2 weeks", "fortnight") |
| 1564 | 3x |
all_15 <- unique(dat[, "fortnight"]) |
| 1565 |
# below I'm adding 0 instead of nothing for 15 days without value |
|
| 1566 | 3x |
for (i in 1:length(unique(dat$year))) {
|
| 1567 |
#i=5 |
|
| 1568 | 63x |
year <- unique(dat$year)[i] |
| 1569 | 63x |
this_year_15 <- unique(dat[dat$year == year, "fortnight"]) |
| 1570 | 63x |
missing <- all_15[!all_15 %in% this_year_15] |
| 1571 | 63x |
if (length(missing >= 1)) {
|
| 1572 | 55x |
missingdat <- data.frame( |
| 1573 | 55x |
"year" = year, |
| 1574 | 55x |
"fortnight" = missing, # this is what we get from the function |
| 1575 | 55x |
"value" = 0, |
| 1576 | 55x |
"maxtab" = 0, |
| 1577 | 55x |
"mintab" = 0, |
| 1578 | 55x |
"mean" = 0, |
| 1579 | 55x |
"sum_per_year" = 0, |
| 1580 | 55x |
"std_value" = 0 |
| 1581 |
) |
|
| 1582 | 55x |
dat <- rbind(dat, missingdat) |
| 1583 |
} |
|
| 1584 |
} |
|
| 1585 | 3x |
dat = dat[order(dat$year, dat[, "fortnight"]), ] |
| 1586 | 3x |
g <- ggplot(dat, aes_string(x = "fortnight", y = "std_value")) |
| 1587 | 3x |
g <- |
| 1588 | 3x |
g + geom_area(aes_string(y = "std_value", fill = "year"), position = |
| 1589 | 3x |
"stack") |
| 1590 | 3x |
g <- |
| 1591 | 3x |
g + scale_x_datetime( |
| 1592 | 3x |
name = gettext("month", domain = "R-stacomiR"),
|
| 1593 | 3x |
date_breaks = "month", |
| 1594 | 3x |
date_minor_breaks = timesplit, |
| 1595 | 3x |
date_labels = "%b", |
| 1596 | 3x |
limits = as.POSIXct(c( |
| 1597 | 3x |
Hmisc::truncPOSIXt((min(dat[dat$valeur != 0, timesplit])), "month"), |
| 1598 | 3x |
Hmisc::ceil((max(dat[dat$valeur != "0", timesplit])), "month") |
| 1599 |
)) |
|
| 1600 |
) |
|
| 1601 | 1x |
g <- |
| 1602 | 1x |
g + scale_y_continuous(name = gettext("Somme des pourcentages annuels de migration par quinzaine", domain = "R-stacomiR"))
|
| 1603 | 1x |
cols <- grDevices::rainbow(length(levels(dat$year))) |
| 1604 | 1x |
g <- g + scale_fill_manual(name = "year", values = cols) |
| 1605 | 1x |
g <- |
| 1606 | 1x |
g + labs( |
| 1607 | 1x |
title = paste( |
| 1608 | 1x |
paste(report_mig_interannual@dc@dc_selected,collapse=" + "), |
| 1609 | 1x |
report_mig_interannual@taxa@data[ |
| 1610 | 1x |
report_mig_interannual@taxa@data$tax_code %in% |
| 1611 | 1x |
report_mig_interannual@taxa@taxa_selected, |
| 1612 | 1x |
"tax_nom_latin"], |
| 1613 |
",", |
|
| 1614 | 1x |
report_mig_interannual@stage@data[ |
| 1615 | 1x |
report_mig_interannual@stage@data$std_code %in% |
| 1616 | 1x |
report_mig_interannual@stage@stage_selected, |
| 1617 | 1x |
"std_libelle"], |
| 1618 |
", ", |
|
| 1619 | 1x |
gettext("migration seasonality", domain = "R-stacomiR")
|
| 1620 |
) |
|
| 1621 |
) |
|
| 1622 | 1x |
g <- g + theme_minimal() |
| 1623 | 1x |
print(g) |
| 1624 | 1x |
assign(paste("g", sep = ""), g, envir_stacomi)
|
| 1625 | 1x |
if (!silent) |
| 1626 | 1x |
funout( |
| 1627 | 1x |
gettext( |
| 1628 | 1x |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 1629 | 1x |
domain = "R-stacomiR" |
| 1630 |
) |
|
| 1631 |
) |
|
| 1632 |
|
|
| 1633 |
} else {
|
|
| 1634 | ! |
if (!silent) |
| 1635 | ! |
funout( |
| 1636 | ! |
gettext( |
| 1637 | ! |
"Warning : you have to complete a migration summary for at least one of the selected year before launching a inter-annual summary", |
| 1638 | ! |
domain = "R-stacomiR" |
| 1639 |
) |
|
| 1640 |
) |
|
| 1641 |
} |
|
| 1642 |
##################################################################### |
|
| 1643 | 19x |
} else if (plot.type == "seasonal") {
|
| 1644 | 5x |
if (!silent) |
| 1645 | 5x |
funout("Seasonal graph to show the phenology of migration")
|
| 1646 |
#report_mig_interannual<-r_mig_interannual_vichy;silent=FALSE;timesplit="mois";require(ggplot2) |
|
| 1647 | 5x |
report_mig_interannual <- |
| 1648 | 5x |
calcule(report_mig_interannual, timesplit = timesplit) |
| 1649 |
#if (!silent& nrow(report_mig_interannual@calcdata)==0) stop("You should run calculation before plotting seasonal data")
|
|
| 1650 | 5x |
dat3 <- report_mig_interannual@calcdata |
| 1651 | 5x |
datadic <- report_mig_interannual@data |
| 1652 | 5x |
datadic <- |
| 1653 | 5x |
fun_date_extraction( |
| 1654 | 5x |
datadic, |
| 1655 | 5x |
nom_coldt = "bjo_jour", |
| 1656 | 5x |
jour_an = TRUE, |
| 1657 | 5x |
quinzaine = TRUE |
| 1658 |
) |
|
| 1659 | 5x |
datadic <- chnames(datadic, c("jour_365","mois","quinzaine","semaine"), c("day","month","fortnight","week"))
|
| 1660 | 5x |
datadic <- killfactor(datadic) |
| 1661 |
#datadic[,timesplit]<-as.numeric(datadic[,timesplit]) |
|
| 1662 |
# to get nicer graphs we don't use a "numeric but transform our data into dates |
|
| 1663 |
# this function takes a vector of column as argument (col), a timesplit argument |
|
| 1664 |
# and a year. So far it does not handle quinzaine so will issue an error if quinzaine is selected |
|
| 1665 | 5x |
dat3[, c("Q0", "Q5", "Q50", "Q95", "Q100", "d90")] <-
|
| 1666 | 5x |
round(dat3[, c("Q0", "Q5", "Q50", "Q95", "Q100", "d90")])
|
| 1667 | 5x |
fn_getbacktodate <- function(dat, col, timesplit_, year = 2000) {
|
| 1668 | 10x |
for (i in 1:length(col)) {
|
| 1669 | 35x |
dat[, col[i]] <- switch( |
| 1670 | 35x |
timesplit_, |
| 1671 | 35x |
"day" = {
|
| 1672 | 7x |
as.Date(paste(year, "-", dat[, col[i]], sep = ""), "%Y-%j") |
| 1673 |
}, |
|
| 1674 | 35x |
"week" = {
|
| 1675 | 14x |
as.Date(paste(year, "-", dat[, col[i]], "-", 6, sep = ""), "%Y-%U-%w") |
| 1676 |
}, |
|
| 1677 | 35x |
"month" = {
|
| 1678 | 14x |
as.Date(paste(year, "-", dat[, col[i]], "-", 1, sep = ""), "%Y-%m-%d") |
| 1679 |
}, |
|
| 1680 | 35x |
stop( |
| 1681 | 35x |
stringr::str_c( |
| 1682 | 35x |
"Internal error, timesplit ", |
| 1683 | 35x |
timesplit_, |
| 1684 | 35x |
" not working for seasonal plot" |
| 1685 |
) |
|
| 1686 |
) |
|
| 1687 |
) |
|
| 1688 |
} |
|
| 1689 | 10x |
return(dat) |
| 1690 |
} |
|
| 1691 | 5x |
datadic <- fn_getbacktodate(dat = datadic, |
| 1692 | 5x |
col = timesplit, |
| 1693 | 5x |
timesplit_ = timesplit) |
| 1694 | 5x |
dat3 <- fn_getbacktodate( |
| 1695 | 5x |
dat = dat3, |
| 1696 | 5x |
col = c("Q0", "Q5", "Q50", "Q95", "Q100", "d90"),
|
| 1697 | 5x |
timesplit_ = timesplit |
| 1698 |
) |
|
| 1699 |
|
|
| 1700 | 5x |
datadic1 <- |
| 1701 | 5x |
dplyr::select(datadic, |
| 1702 | 5x |
{{timesplit}},
|
| 1703 | 5x |
bjo_annee, |
| 1704 | 5x |
bjo_valeur, |
| 1705 | 5x |
bjo_labelquantite) |
| 1706 | 5x |
datadic1 <- |
| 1707 | 5x |
dplyr::group_by(datadic1, bjo_annee, dplyr::across(dplyr::all_of(timesplit)), bjo_labelquantite) |
| 1708 | 5x |
datadic1 <- dplyr::summarize(datadic1, bjo_valeur = sum(bjo_valeur)) |
| 1709 | 5x |
datadic1 <- |
| 1710 | 5x |
dplyr::ungroup(datadic1) %>% dplyr::filter(bjo_labelquantite == "Effectif_total") |
| 1711 | 5x |
g <- ggplot(data = datadic1) + |
| 1712 | 5x |
geom_rect( |
| 1713 | 5x |
aes( |
| 1714 | 5x |
xmin = Q0, |
| 1715 | 5x |
xmax = Q100, |
| 1716 | 5x |
ymin = year - 0.5, |
| 1717 | 5x |
ymax = year + 0.5 |
| 1718 |
), |
|
| 1719 | 5x |
fill = "grey90", |
| 1720 | 5x |
data = dat3 |
| 1721 |
) + |
|
| 1722 | 5x |
geom_tile( |
| 1723 | 5x |
aes_string(x = timesplit, y = "bjo_annee", fill = "bjo_valeur"), |
| 1724 | 5x |
color = ifelse(timesplit == "day", "transparent", "grey80") |
| 1725 |
) + |
|
| 1726 | 5x |
scale_fill_distiller(palette = "Spectral", name = "Effectif") + |
| 1727 | 5x |
geom_path( |
| 1728 | 5x |
aes(x = Q50, y = year), |
| 1729 | 5x |
col = "black", |
| 1730 | 5x |
lty = 2, |
| 1731 | 5x |
data = dat3 |
| 1732 |
) + |
|
| 1733 | 5x |
geom_point( |
| 1734 | 5x |
aes(x = Q50, y = year), |
| 1735 | 5x |
col = "black", |
| 1736 | 5x |
size = 2, |
| 1737 | 5x |
data = dat3 |
| 1738 |
) + |
|
| 1739 | 5x |
geom_errorbarh( |
| 1740 | 5x |
aes( |
| 1741 | 5x |
y = year, |
| 1742 | 5x |
xmin = Q5, |
| 1743 | 5x |
xmax = Q95 |
| 1744 |
), |
|
| 1745 | 5x |
height = 0, |
| 1746 | 5x |
data = dat3, |
| 1747 | 5x |
col = "black" |
| 1748 |
) + |
|
| 1749 | 5x |
ylab(Hmisc::capitalize(gettext("year", domain = "R-stacomiR"))) +
|
| 1750 | 5x |
xlab(Hmisc::capitalize({{timesplit}})) +
|
| 1751 | 5x |
scale_x_date( |
| 1752 | 5x |
name = timesplit, |
| 1753 | 5x |
date_breaks = "month", |
| 1754 | 5x |
date_minor_breaks = {{timesplit}},
|
| 1755 | 5x |
date_labels = "%b" |
| 1756 |
) + |
|
| 1757 | 5x |
theme_bw() |
| 1758 | 5x |
print(g) |
| 1759 | 5x |
assign("g", g, envir = envir_stacomi)
|
| 1760 | 5x |
if (!silent) |
| 1761 | 5x |
funout( |
| 1762 | 5x |
gettext( |
| 1763 | 5x |
"Writing the graphical object into envir_stacomi environment : write g=get('g',envir_stacomi)\n",
|
| 1764 | 5x |
domain = "R-stacomiR" |
| 1765 |
) |
|
| 1766 |
) |
|
| 1767 |
|
|
| 1768 |
} |
|
| 1769 |
|
|
| 1770 |
else {
|
|
| 1771 |
# end if |
|
| 1772 | ! |
stop ("plot.type argument invalid")
|
| 1773 |
} |
|
| 1774 |
|
|
| 1775 |
} else {
|
|
| 1776 | 1x |
if (!silent) |
| 1777 | 1x |
funout( |
| 1778 | 1x |
gettext( |
| 1779 | 1x |
"Attention : you have to complete a migration summary for at least one of the selected year before launching a inter-annual summary", |
| 1780 | 1x |
domain = "R-stacomiR" |
| 1781 |
) |
|
| 1782 |
) |
|
| 1783 |
} |
|
| 1784 | 18x |
return(invisible(NULL)) |
| 1785 |
} |
|
| 1786 |
) |
|
| 1787 | ||
| 1788 | ||
| 1789 | ||
| 1790 |
#' summary for report_mig_interannual |
|
| 1791 |
#' provides summary statistics for the latest year (if silent=TRUE), or the year selected in the interface, |
|
| 1792 |
#' if silent=FALSE. Mean, min and max are historical statistics with the selected year excluded from the |
|
| 1793 |
#' historical dataset. |
|
| 1794 |
#' @param object An object of class \code{\link{report_mig_interannual-class}}
|
|
| 1795 |
#' @param year_choice The year chosen to calculate statistics which will be displayed beside the historical series, |
|
| 1796 |
#' @param silent Should the program stay silent or display messages, default FALSE |
|
| 1797 |
#' @param ... Additional parameters (not used there) |
|
| 1798 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 1799 |
#' @aliases summary.report_mig_interannual |
|
| 1800 |
#' @return A list, one element per DC |
|
| 1801 |
#' @export |
|
| 1802 |
setMethod( |
|
| 1803 |
"summary", |
|
| 1804 |
signature = signature(object = "report_mig_interannual"), |
|
| 1805 |
definition = function(object, year_choice=NULL, silent = FALSE, ...) {
|
|
| 1806 |
# table generated with funtable |
|
| 1807 |
# TODO traitement des poids |
|
| 1808 |
# object<-r_mig_interannual; object <- rmi |
|
| 1809 | 2x |
dat0 <- object@data |
| 1810 | 2x |
dat0 <- dat0[dat0$bjo_labelquantite == "Effectif_total", ] |
| 1811 | 2x |
dat0 <- |
| 1812 | 2x |
stacomirtools::chnames( |
| 1813 | 2x |
dat0, |
| 1814 | 2x |
c( |
| 1815 | 2x |
"bjo_dis_identifiant", |
| 1816 | 2x |
"bjo_tax_code", |
| 1817 | 2x |
"bjo_std_code", |
| 1818 | 2x |
"bjo_annee", |
| 1819 | 2x |
"bjo_jour", |
| 1820 | 2x |
"bjo_labelquantite", |
| 1821 | 2x |
"bjo_valeur", |
| 1822 | 2x |
"bjo_horodateexport" |
| 1823 |
), |
|
| 1824 | 2x |
c( |
| 1825 | 2x |
"DC", |
| 1826 | 2x |
"taxa", |
| 1827 | 2x |
"stage", |
| 1828 | 2x |
"year", |
| 1829 | 2x |
"day", |
| 1830 | 2x |
"label_quantity", |
| 1831 | 2x |
"number", |
| 1832 | 2x |
"date of report export" |
| 1833 |
) |
|
| 1834 |
) |
|
| 1835 | 2x |
dat0$year <- as.factor(dat0$year) |
| 1836 | 2x |
dat0 <- dat0[, -1] |
| 1837 | 2x |
tmp <- dat0$day |
| 1838 | 2x |
DC <- object@dc@dc_selected |
| 1839 | 2x |
dat0 <- chnames(dat0, "day", "debut_pas") |
| 1840 |
# debut_pas must be column name in tableau |
|
| 1841 | 2x |
listDC <- list() |
| 1842 | 2x |
for (i in 1:length(DC)) {
|
| 1843 |
# this table will write an html table of data |
|
| 1844 | 2x |
funtable( |
| 1845 | 2x |
tableau = dat0[dat0$bjo_dis_identifiant == DC, ], |
| 1846 | 2x |
time.sequence = tmp, |
| 1847 | 2x |
taxa = object@taxa@data[object@taxa@data$tax_code %in% object@taxa@taxa_selected, "tax_nom_latin"], |
| 1848 | 2x |
stage = object@stage@data[object@stage@data$std_code %in% object@stage@stage_selected, "std_libelle"], |
| 1849 | 2x |
DC[i], |
| 1850 | 2x |
resum = NULL, |
| 1851 | 2x |
silent = silent |
| 1852 |
) |
|
| 1853 |
# Summary statistics |
|
| 1854 | 2x |
dat1 = object@data |
| 1855 | 2x |
if (is.null(year_choice)){
|
| 1856 | ! |
if (silent == FALSE) {
|
| 1857 | ! |
the_choice <- as.numeric( |
| 1858 | ! |
select.list( |
| 1859 | ! |
choices = as.character(unique(dat1$bjo_annee)[order(unique(dat1$bjo_annee))]), |
| 1860 | ! |
preselect = as.character(max(dat1$bjo_annee)), |
| 1861 | ! |
gettext("Year choice", domain = "R-stacomiR"),
|
| 1862 | ! |
multiple = FALSE |
| 1863 |
) |
|
| 1864 |
) |
|
| 1865 |
} else {
|
|
| 1866 | ! |
the_choice <- max((dat1$bjo_annee)) |
| 1867 |
} |
|
| 1868 |
} else {
|
|
| 1869 | 2x |
if (!year_choice %in% unique(dat1$bjo_annee)) {
|
| 1870 | ! |
stop(paste("The chosen year",year_choice,"should be in available years : ",
|
| 1871 | ! |
paste(as.character(unique(dat1$bjo_annee)[order(unique(dat1$bjo_annee))]), collapse=","))) |
| 1872 |
} |
|
| 1873 | 2x |
the_choice <- as.numeric(year_choice) |
| 1874 |
} |
|
| 1875 |
# we use the function that split data per time stamp to generate the full sequence of monthly data |
|
| 1876 | 2x |
dat2 <- |
| 1877 | 2x |
fun_report_mig_interannual(dat1[dat1$bjo_dis_identifiant == DC[i], ], timesplit = |
| 1878 | 2x |
"month") |
| 1879 |
# then we extract only current year for summary |
|
| 1880 | 2x |
colnames(dat2)[colnames(dat2) == "maxtab"] <- "max" |
| 1881 | 2x |
colnames(dat2)[colnames(dat2) == "mintab"] <- "min" |
| 1882 | 2x |
dat2$nummonth <- as.numeric(strftime(dat2$month, "%m")) # to order later on |
| 1883 | 2x |
dat2$month <- strftime(dat2$month, "%b") |
| 1884 | 2x |
dat2$mean <- round(dat2$mean) |
| 1885 | 2x |
dat3 <- dat2[dat2$year == the_choice, ] |
| 1886 |
# dat3 only shows the month that have data for one year, here we collect the others |
|
| 1887 | 2x |
missing_month <- unique(dat2$month)[!unique(dat2$month) %in% unique(dat3$month)] |
| 1888 | 2x |
dat_other_month <- dat2[dat2$month %in% missing_month, ] # data for missing month but many years |
| 1889 | 2x |
if (nrow(dat_other_month)>0){
|
| 1890 | 1x |
dat_other_month$value <- NA # we will no value for the choice |
| 1891 | 1x |
dat_other_month$year <- the_choice # setting actual year |
| 1892 | 1x |
dat_other_month <- dat_other_month [!duplicated(dat_other_month$month),] # keep only one month |
| 1893 |
} |
|
| 1894 | 2x |
dat4 <- rbind(dat3, dat_other_month) |
| 1895 | 2x |
dat4 <- dat4[order(dat4$nummonth), c("year", "month", "min", "mean", "max", "value")]
|
| 1896 | 2x |
colnames(dat4) <- c( |
| 1897 | 2x |
gettext("year", domain = "R-stacomiR"),
|
| 1898 | 2x |
gettext("month", domain = "R-stacomiR"),
|
| 1899 | 2x |
"min", |
| 1900 | 2x |
gettext("mean", domain = "R-stacomiR"),
|
| 1901 | 2x |
"max", |
| 1902 | 2x |
gettext("value", domain = "R-stacomiR"))
|
| 1903 | 2x |
listDC[[as.character(DC[i])]] <- dat4 |
| 1904 | 2x |
}# end for |
| 1905 | 2x |
return(listDC) |
| 1906 |
} |
|
| 1907 |
) |
| 1 |
#' |
|
| 2 |
#' |
|
| 3 |
#' Internal function, tests the connection and if it works loads the stacomi interface |
|
| 4 |
#' @note \code{base} is copied by stacomi into envir_stacomi. Same for \code{database_expected}
|
|
| 5 |
#' |
|
| 6 |
#' @param ... Other arguments |
|
| 7 |
#' @return Nothing |
|
| 8 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 9 |
#' @keywords internal |
|
| 10 |
load_stacomi <- function(...) {
|
|
| 11 |
|
|
| 12 |
# assigned when passing through stacomi |
|
| 13 | 110x |
database_expected <- get("database_expected", envir_stacomi) # logical true or false
|
| 14 |
|
|
| 15 | 110x |
if (database_expected) {
|
| 16 | 80x |
sch <- get_schema() |
| 17 | 80x |
dbname <- options("stacomiR.dbname")[[1]]
|
| 18 | 80x |
host <- options("stacomiR.host")[[1]]
|
| 19 | 80x |
port <- options("stacomiR.port")[[1]]
|
| 20 | 80x |
user <- options("stacomiR.user")[[1]]
|
| 21 | 80x |
password <- options("stacomiR.password")[[1]]
|
| 22 | 80x |
if (user=="") {
|
| 23 |
# this is the default options at start |
|
| 24 |
# if interactive will try to set the options upon loading |
|
| 25 | ! |
if (interactive()){
|
| 26 | ! |
user <- readline(prompt="Enter user: ") |
| 27 | ! |
options("stacomiR.user"=user)
|
| 28 | ! |
password <- readline(prompt="Enter password: ") |
| 29 | ! |
options("stacomiR.password"=password)
|
| 30 |
} else {
|
|
| 31 | ! |
user <- "postgres" |
| 32 | ! |
password <- "postgres" |
| 33 | ! |
warning('no user set by default, reverted to user <- "postgres" and password <- "postgres",
|
| 34 | ! |
you can change it with options("stacomiR.user"=user) and options("stacomiR.password"=password)')
|
| 35 |
} |
|
| 36 |
} |
|
| 37 | ||
| 38 |
|
|
| 39 | 80x |
con = new("ConnectionDB")
|
| 40 | 80x |
e = expression(con <- connect(con)) |
| 41 | 80x |
con = tryCatch(eval(e), error = function(e) e) |
| 42 | 80x |
if ("Rcpp::exception"%in%class(con)){
|
| 43 | ! |
cat(con$message) |
| 44 | ! |
test <- FALSE |
| 45 |
} else {
|
|
| 46 | 80x |
test <- TRUE |
| 47 | 80x |
pool::poolClose(con@connection) |
| 48 |
} |
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
# second test to check that the database is working OK |
|
| 53 |
|
|
| 54 | 80x |
if (test) {
|
| 55 | 80x |
requete = new("RequeteDB")
|
| 56 | 80x |
requete@sql = paste0("select count(*) from ", sch, "t_lot_lot")
|
| 57 | 80x |
requete <- stacomirtools::query(requete) |
| 58 | 80x |
if (nrow(requete@query) == 0) {
|
| 59 |
# the database link is not working or the |
|
| 60 |
# schema |
|
| 61 | ! |
funout(paste(gettext("Problem during the test, connection to the database is established but failed to connect to the right schema argument passed to stacomi",
|
| 62 | ! |
domain = "R-stacomiR"), "\n", |
| 63 | ! |
gettext("dbname", domain = "R-stacomiR")," :", dbname, "\n",
|
| 64 | ! |
gettext("User", domain = "R-stacomiR"), " :", user, "\n",
|
| 65 | ! |
gettext("Port", domain = "R-stacomiR"), " :", port, "\n",
|
| 66 | ! |
gettext("Host", domain = "R-stacomiR"), " :", host, "\n",
|
| 67 | ! |
gettext("Password", domain = "R-stacomiR"), " :", password),
|
| 68 | ! |
gettext("schema", domain = "R-stacomiR"), " :", sch)
|
| 69 |
} |
|
| 70 |
|
|
| 71 |
} else {
|
|
| 72 |
# the test has failed and the user will be prompted to another |
|
| 73 | ! |
funout(paste(gettext("Problem when testing the DB connection", domain = "R-stacomiR"),
|
| 74 | ! |
gettext("dbname", domain = "R-stacomiR")," :", dbname, "\n",
|
| 75 | ! |
gettext("User", domain = "R-stacomiR"), " :", user, "\n",
|
| 76 | ! |
gettext("Port", domain = "R-stacomiR"), " :", port, "\n",
|
| 77 | ! |
gettext("Host", domain = "R-stacomiR"), " :", host, "\n",
|
| 78 | ! |
gettext("Password", domain = "R-stacomiR"), " :", password))
|
| 79 | 80x |
} # end else test (else == the test didn't pass, we have to change the name and password |
| 80 |
} else {
|
|
| 81 |
# here : database_expected=FALSE we don't want to check the connection |
|
| 82 |
# at all... |
|
| 83 |
} |
|
| 84 |
} |
|
| 85 | ||
| 86 | ||
| 87 | ||
| 88 | ||
| 89 | ||
| 90 | ||
| 91 |
#' stacomi Main launcher for program stacomi |
|
| 92 |
#' |
|
| 93 |
#' When \code{database_expected=FALSE} a connection to the database is not expected. Therefore test are run by calling examples object stored in Rdata.
|
|
| 94 |
#' To change the language use Sys.setenv(LANG = 'fr') or Sys.setenv(LANG = 'en') |
|
| 95 |
#' @param database_expected Boolean, if \code{TRUE} pre launch tests will be run to test the connection validity
|
|
| 96 |
#' @param datawd The data working directory |
|
| 97 |
#' @param sch The schema in the stacomi database default 'iav.' |
|
| 98 |
#' @return Nothing, called for its side effect of loading |
|
| 99 |
#' @usage stacomi(database_expected=TRUE, datawd = "~", sch = "iav") |
|
| 100 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 101 |
#' @examples |
|
| 102 |
#' |
|
| 103 |
#' require(stacomiR) |
|
| 104 |
#' #launch stacomi |
|
| 105 |
#' \dontrun{
|
|
| 106 |
#' stacomi(database_expected=TRUE, datawd='~',sch= "iav") |
|
| 107 |
#' } |
|
| 108 |
#' # launch stacomi without connection to the database |
|
| 109 |
#' stacomi(database_expected=FALSE) |
|
| 110 |
#' # launch stacomi with options |
|
| 111 |
#' options( |
|
| 112 |
#' stacomiR.dbname = "bd_contmig_nat", |
|
| 113 |
#' stacomiR.host = readline(prompt = "Enter host: "), |
|
| 114 |
#' stacomiR.port = "5432", |
|
| 115 |
#' stacomiR.user = readline(prompt = "Enter user: "), |
|
| 116 |
#' stacomiR.password = readline(prompt = "Enter password: ") |
|
| 117 |
#') |
|
| 118 |
#' @export |
|
| 119 |
stacomi = function(database_expected = TRUE, datawd = "~", sch = "iav") {
|
|
| 120 | 110x |
assign("database_expected", database_expected, envir = envir_stacomi)
|
| 121 |
# values assigned in the envir_stacomi |
|
| 122 | 110x |
assign("datawd", datawd, envir = envir_stacomi)
|
| 123 | 110x |
assign("sch", paste(sch, ".", sep = ""), envir = envir_stacomi)
|
| 124 | 110x |
load_stacomi() |
| 125 | 110x |
invisible(NULL) |
| 126 |
} |
|
| 127 | ||
| 128 | ||
| 129 | ||
| 130 | ||
| 131 | ||
| 132 | ||
| 133 |
#' Working environment for stacomiR created when launching stacomi() |
|
| 134 |
#' |
|
| 135 |
#' This is where the graphical interface stores its objects |
|
| 136 |
#' try \code{ls(envir=envir_stacomi)}
|
|
| 137 |
#' @keywords environment |
|
| 138 |
#' @export |
|
| 139 |
envir_stacomi <- new.env(parent = asNamespace("stacomiR"))
|
|
| 140 |
# calcmig<-data.frame() |
| 1 |
#' Class "report_mig_env" |
|
| 2 |
#' |
|
| 3 |
#' Enables to compute an annual overview of fish migration and environmental |
|
| 4 |
#' conditions in the same chart. Environmental conditions may trigger migration events, variation in flow |
|
| 5 |
#' or temperatures can be plotted along migration to check graphically for a possible relation. To enable this, |
|
| 6 |
#' environmental conditions are loaded from an "environmental monitoring station", which records environmental |
|
| 7 |
#' parameters and is attached to a migratory station in the database. |
|
| 8 |
#' This class enables both continuous output (temperature -flow) as well as discrete parameters (qualitative = moon |
|
| 9 |
#' phase, type of operation of a gate, opening of a gate...) which will be displayed on the graph. Values are scaled so that |
|
| 10 |
#' single plot can display migration numbers and environmental parameters. Environmental parameters when stored at a |
|
| 11 |
#' time scale lower that a day are averaged per day, unless they are qualitative parameters, in which case only the first |
|
| 12 |
#' event of the day is displayed on the annual plot. |
|
| 13 |
#' |
|
| 14 |
#' @include report_mig_mult.R |
|
| 15 |
#' @include report_env.R |
|
| 16 |
#' @include create_generic.R |
|
| 17 |
#' @include utilities.R |
|
| 18 |
#' @slot report_mig_mult \link{report_mig_mult-class}
|
|
| 19 |
#' @slot report_env \link{report_env-class}
|
|
| 20 |
#' @author cedric.briand@eptb-vilaine.fr marion.legrand@logrami.fr |
|
| 21 |
#' @family report Objects |
|
| 22 |
#' @keywords classes |
|
| 23 |
#' @aliases report_mig_env |
|
| 24 |
#' @keywords classes |
|
| 25 |
#' @example inst/examples/report_mig_env-example.R |
|
| 26 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 27 |
#' @family report Objects |
|
| 28 |
#' @keywords classes |
|
| 29 |
#' @export |
|
| 30 |
setClass( |
|
| 31 |
Class = "report_mig_env", |
|
| 32 |
representation = |
|
| 33 |
representation(report_mig_mult = "report_mig_mult", |
|
| 34 |
report_env = "report_env"), |
|
| 35 |
prototype = prototype( |
|
| 36 |
report_mig_mult = new("report_mig_mult"),
|
|
| 37 |
report_env = new("report_env")
|
|
| 38 |
|
|
| 39 |
) |
|
| 40 |
) |
|
| 41 | ||
| 42 | ||
| 43 |
setValidity("report_mig_env",
|
|
| 44 |
function(object) |
|
| 45 |
{
|
|
| 46 |
rep1 = validObject(object@report_mig_mult, test = TRUE) |
|
| 47 |
rep2 = validObject(object@report_env, test = TRUE) |
|
| 48 |
return(ifelse(rep1 & rep2 , TRUE, c(1:2)[!c(rep1, rep2)])) |
|
| 49 |
}) |
|
| 50 |
#' connect method for report_mig_env class |
|
| 51 |
#' @param object An object of class \link{report_mig_env-class}
|
|
| 52 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 53 |
#' @return an object of report_mig_env class |
|
| 54 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 55 |
#' @aliases summary.report_mig_env |
|
| 56 |
setMethod( |
|
| 57 |
"connect", |
|
| 58 |
signature = signature("report_mig_env"),
|
|
| 59 |
definition = function(object, silent = FALSE) {
|
|
| 60 |
#object<-r_mig_env |
|
| 61 | 2x |
r_mig_env <- object |
| 62 | 2x |
r_mig_env@report_mig_mult <- |
| 63 | 2x |
connect(r_mig_env@report_mig_mult, silent = silent) |
| 64 | 2x |
r_mig_env@report_env <- connect(r_mig_env@report_env, silent = silent) |
| 65 | 2x |
return(r_mig_env) |
| 66 |
} |
|
| 67 |
) |
|
| 68 |
#' command line interface for report_env class |
|
| 69 |
#' |
|
| 70 |
#' @param object An object of class \link{report_env-class}
|
|
| 71 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 72 |
#' @param taxa '2038=Anguilla anguilla', |
|
| 73 |
#' these should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 74 |
#' @param stage 'AGJ=Yellow eel', 'AGG=Silver eel', 'CIV=glass eel' |
|
| 75 |
#' @param stationMesure A character, the code of the monitoring station, which records environmental parameters \link{choice_c,ref_env-method}
|
|
| 76 |
#' @param datedebut The starting date as a character, formats like \code{\%Y-\%m-\%d} or \code{\%d-\%m-\%Y} can be used as input
|
|
| 77 |
#' @param datefin The finishing date of the report, for this class this will be used to calculate the number of daily steps. |
|
| 78 |
#' @param silent Boolean default FALSE, if TRUE information messages not displayed. |
|
| 79 |
#' @aliases choice_c.report_mig_env |
|
| 80 |
#' @return An object of class \link{report_env-class} with data selected
|
|
| 81 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 82 |
setMethod( |
|
| 83 |
"choice_c", |
|
| 84 |
signature = signature("report_mig_env"),
|
|
| 85 |
definition = function(object, |
|
| 86 |
dc, |
|
| 87 |
taxa, |
|
| 88 |
stage, |
|
| 89 |
stationMesure, |
|
| 90 |
datedebut, |
|
| 91 |
datefin, |
|
| 92 |
silent = FALSE) {
|
|
| 93 |
# code for debug |
|
| 94 |
# dc=c(5,6,12); taxa=c("Anguilla anguilla");stage=c("AGJ","AGG","CIV");
|
|
| 95 |
# stationMesure=c("temp_gabion","coef_maree");
|
|
| 96 |
# datedebut="2008-01-01";datefin="2008-12-31";silent=FALSE |
|
| 97 | 2x |
r_mig_env <- object |
| 98 | 2x |
r_mig_env@report_mig_mult = |
| 99 | 2x |
choice_c( |
| 100 | 2x |
r_mig_env@report_mig_mult, |
| 101 | 2x |
dc = dc, |
| 102 | 2x |
taxa = taxa, |
| 103 | 2x |
stage = stage, |
| 104 | 2x |
datedebut = datedebut, |
| 105 | 2x |
datefin = datefin, |
| 106 | 2x |
silent = silent |
| 107 |
) |
|
| 108 | 2x |
r_mig_env@report_env = choice_c( |
| 109 | 2x |
r_mig_env@report_env, |
| 110 | 2x |
stationMesure = stationMesure, |
| 111 | 2x |
datedebut = datedebut, |
| 112 | 2x |
datefin = datefin, |
| 113 | 2x |
silent = silent |
| 114 |
) |
|
| 115 | 2x |
return(r_mig_env) |
| 116 |
} |
|
| 117 |
) |
|
| 118 |
#' charge method for report_mig_env class |
|
| 119 |
#' |
|
| 120 |
#' #' Unique the other report classes where the charge method is only used by the graphical interface |
|
| 121 |
#' to collect and test objects in the environment envir_stacomi, and see if the right choices have |
|
| 122 |
#' been made in the graphical interface, this methods runs the \link{charge,report_mig_mult-method}
|
|
| 123 |
#' and needs to be called from the command line (see examples) |
|
| 124 |
#' @param object An object of class \link{report_mig_env-class}
|
|
| 125 |
#' @param silent Should the function remain silent (boolean) |
|
| 126 |
#' @return An object of class \link{report_mig_env-class} with data set from values assigned in \code{envir_stacomi} environment
|
|
| 127 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 128 |
#' @aliases charge.report_mig_env |
|
| 129 |
setMethod( |
|
| 130 |
"charge", |
|
| 131 |
signature = signature("report_mig_env"),
|
|
| 132 |
definition = function(object, silent = FALSE) {
|
|
| 133 |
# silent=FALSE |
|
| 134 | 2x |
r_mig_env <- object |
| 135 | 2x |
r_mig_env@report_mig_mult <- |
| 136 | 2x |
charge(r_mig_env@report_mig_mult, silent = silent) |
| 137 |
# the values for date are not initiated by the interface |
|
| 138 | 2x |
assign( |
| 139 | 2x |
"report_env_date_debut", |
| 140 | 2x |
get("timestep", envir_stacomi)@"dateDebut",
|
| 141 | 2x |
envir_stacomi |
| 142 |
) |
|
| 143 | 2x |
assign("report_env_date_fin",
|
| 144 | 2x |
as.POSIXlt(end_date(get( |
| 145 | 2x |
"timestep", envir_stacomi |
| 146 |
))), |
|
| 147 | 2x |
envir_stacomi) |
| 148 | 2x |
r_mig_env@report_env <- |
| 149 | 2x |
charge(r_mig_env@report_env, silent = silent) |
| 150 | 2x |
return(r_mig_env) |
| 151 |
} |
|
| 152 |
) |
|
| 153 | ||
| 154 |
#' Calculations for migration in the class \link{report_mig_env-class}
|
|
| 155 |
#' |
|
| 156 |
#' Runs the calcule method in \link{report_mig_mult-class}
|
|
| 157 |
#' @param object An object of class \link{report_mig_env-class}
|
|
| 158 |
#' @param silent Boolean default FALSE, if TRUE information messages not displayed |
|
| 159 |
#' @aliases calcule.report_mig_env |
|
| 160 |
#' @return \link{report_mig_env-class} with data in slot r_mig_env@report_mig_mult@calcdata
|
|
| 161 |
setMethod( |
|
| 162 |
"calcule", |
|
| 163 |
signature = signature("report_mig_env"),
|
|
| 164 |
definition = function(object, silent = FALSE) {
|
|
| 165 |
# silent=FALSE |
|
| 166 | 1x |
r_mig_env <- object |
| 167 | 1x |
r_mig_env@report_mig_mult <- |
| 168 | 1x |
calcule(r_mig_env@report_mig_mult, silent = silent) |
| 169 | 1x |
if (!silent) |
| 170 | 1x |
funout( |
| 171 | 1x |
gettext( |
| 172 | 1x |
"r_mig_env object is stocked into envir_stacomi environment\n", |
| 173 | 1x |
domain = "R-stacomiR" |
| 174 |
) |
|
| 175 |
) |
|
| 176 | 1x |
return(r_mig_env) |
| 177 |
} |
|
| 178 |
) |
|
| 179 | ||
| 180 | ||
| 181 |
#' Plot method for report_mig_env |
|
| 182 |
#' @param x An object of class \link{report_mig_env}
|
|
| 183 |
#' @param silent Stops displaying the messages. |
|
| 184 |
#' @param color_station A named vector of station color (e.g. c("temp_gabion"="red","coef_maree"="blue","phases_lune"="green")) default null
|
|
| 185 |
#' @param color_dc A named vector giving the color for each dc default null (e.g. c("5"="#4D4D4D","6"="#E6E6E6","12"="#AEAEAE"))
|
|
| 186 |
#' @return Nothing, called for its side effect of plotting |
|
| 187 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 188 |
#' @aliases plot.report_mig_env |
|
| 189 |
#' @export |
|
| 190 |
setMethod( |
|
| 191 |
"plot", |
|
| 192 |
signature(x = "report_mig_env", y = "missing"), |
|
| 193 |
definition = function(x, |
|
| 194 |
color_station = NULL, |
|
| 195 |
color_dc = NULL, |
|
| 196 |
silent = FALSE) {
|
|
| 197 |
#color_station=NULL;color_dc=NULL |
|
| 198 |
# color_station<-c("temp_gabion"="red","coef_maree"="blue","phases_lune"="green")
|
|
| 199 |
# color_dc=c("5"="#4D4D4D","6"="#E6E6E6","12"="#AEAEAE")
|
|
| 200 | 3x |
r_mig_env <- x |
| 201 |
|
|
| 202 |
|
|
| 203 | 3x |
grdata <- fun_aggreg_for_plot(r_mig_env@report_mig_mult) |
| 204 |
# we collect the dataset used to build the graph |
|
| 205 |
|
|
| 206 | 3x |
taxa = as.character(r_mig_env@report_mig_mult@taxa@data[ |
| 207 | 3x |
r_mig_env@report_mig_mult@taxa@data$tax_code %in% |
| 208 | 3x |
r_mig_env@report_mig_mult@taxa@taxa_selected, "tax_nom_latin"] |
| 209 |
) |
|
| 210 | 3x |
stage = as.character(r_mig_env@report_mig_mult@stage@data[ |
| 211 | 3x |
r_mig_env@report_mig_mult@stage@data$std_code%in% |
| 212 | 3x |
r_mig_env@report_mig_mult@stage@stage_selected,"std_libelle"] |
| 213 |
) |
|
| 214 | 3x |
dc <- unique(grdata$DC) |
| 215 | 3x |
stations <- r_mig_env@report_env@stationMesure@data |
| 216 | 3x |
dc_code <- r_mig_env@report_mig_mult@dc@data$dc_code[match(dc, r_mig_env@report_mig_mult@dc@data$dc)] |
| 217 |
# tableau conditions environnementales |
|
| 218 | 3x |
tableauCE <- r_mig_env@report_env@data |
| 219 | 3x |
if (nrow(tableauCE) == 0) {
|
| 220 | ! |
funout( |
| 221 | ! |
gettext( |
| 222 | ! |
"You don't have any environmental conditions within the time period\n", |
| 223 | ! |
domain = "R-stacomiR" |
| 224 |
), |
|
| 225 | ! |
arret = TRUE |
| 226 |
) |
|
| 227 |
} |
|
| 228 |
|
|
| 229 |
# we collect libelle and source of data from station |
|
| 230 | 3x |
source <- unlist(lapply(regmatches(stations$stm_description,gregexpr("(?<=:([[:space:]])).*", stations$stm_description, perl =TRUE),invert=NA),
|
| 231 | 3x |
function(X) X[2])) |
| 232 | 3x |
if (any(is.na(source))) {
|
| 233 | 2x |
missing <- stations[is.na(source),"stm_libelle"] |
| 234 | 2x |
missing_and_selected <- missing[missing%in%r_mig_env@report_env@stationMesure@env_selected] |
| 235 | 2x |
missing_and_selected <- paste(missing_and_selected, collapse=" , ") |
| 236 | 2x |
warning(sprintf("the source of data is not present in column stm_description of table tj_stationmesure_stm for stations: %s. \n Please consider adding it by adding something like source: Banque hydro at the end of the line in table tj_stationmesure_stm column stm_description",
|
| 237 | 2x |
missing_and_selected)) |
| 238 | 2x |
source[is.na(source)] <- "Not defined" |
| 239 |
} |
|
| 240 | ||
| 241 | 3x |
stations$source <- source |
| 242 | 3x |
tableauCE <- merge(tableauCE,stations, by.x="env_stm_identifiant", by.y="stm_identifiant") |
| 243 |
# (?<= ) lookbehind, get the string : with a space after but not capture it. .* capture anything after |
|
| 244 |
# currently we've added the source of data within the stm_description string so as not to change the db |
|
| 245 |
# regmatches, invert = NA returns a list with first elt matching and second non mathing |
|
| 246 |
# we want the full list returned with NA when missing match |
|
| 247 | ||
| 248 | ||
| 249 | ||
| 250 |
|
|
| 251 |
# the data can be in the POSIXct format, we need to round them |
|
| 252 | 3x |
tableauCE$date <- |
| 253 | 3x |
as.POSIXct(Hmisc::roundPOSIXt(tableauCE$env_date_debut, digits = "days")) |
| 254 | 3x |
qualitative <- !is.na(tableauCE$env_val_identifiant) |
| 255 | 3x |
tableauCEquan <- tableauCE[!qualitative, ] |
| 256 | 3x |
tableauCEqual <- tableauCE[qualitative, ] |
| 257 | 3x |
if (nrow(unique(cbind(tableauCE$date, tableauCE$stm_libelle))) != nrow(tableauCE)) {
|
| 258 |
# do not cut character chain below... |
|
| 259 | ! |
funout( |
| 260 | ! |
gettextf( |
| 261 | ! |
"Attention, on one station :%s there are several entries for the same day :%s we will calculate average for numeric and use the first value for qualitative parameter", |
| 262 | ! |
sta, |
| 263 | ! |
paste(unique(tableauCEst$env_date_debut[duplicated(tableauCEst$env_date_debut)]), sep = |
| 264 |
"") |
|
| 265 |
), |
|
| 266 | ! |
arret = FALSE |
| 267 |
) |
|
| 268 |
# for quantitative parameters we group by date and station and use the average to |
|
| 269 |
# extract one value per day |
|
| 270 | ! |
tableauCEquan <- |
| 271 | ! |
dplyr::select(tableauCEquan, date, stm_libelle, env_valeur_quantitatif) %>% |
| 272 | ! |
dplyr::group_by(date, stm_libelle) %>% |
| 273 | ! |
dplyr::summarize(valeur = mean(env_valeur_quantitatif)) %>% |
| 274 | ! |
dplyr::ungroup() |
| 275 |
# for qualitative value, when there are several values for the same date |
|
| 276 |
# we arbitrarily select the first |
|
| 277 | ! |
tableauCEqual <- |
| 278 | ! |
dplyr::select(tableauCEqual, date, stm_libelle, env_val_identifiant) %>% |
| 279 | ! |
dplyr::group_by(date, stm_libelle) %>% |
| 280 | ! |
dplyr::summarize(valeur = first(env_val_identifiant)) %>% |
| 281 | ! |
dplyr::ungroup() |
| 282 |
} else {
|
|
| 283 |
# we want the same format as above |
|
| 284 | 3x |
tableauCEquan <- |
| 285 | 3x |
dplyr::select(tableauCEquan, date, stm_libelle, env_valeur_quantitatif) %>% |
| 286 | 3x |
dplyr::rename(valeur = env_valeur_quantitatif) |
| 287 | 3x |
tableauCEqual <- |
| 288 | 3x |
dplyr::select(tableauCEqual, date, stm_libelle, env_val_identifiant) %>% |
| 289 | 3x |
dplyr::rename(valeur = env_val_identifiant) |
| 290 |
} |
|
| 291 | 3x |
variables_quant <- unique(tableauCEquan$stm_libelle) |
| 292 | 3x |
variables_qual <- unique(tableauCEqual$stm_libelle) |
| 293 | 3x |
grdata <- fun_date_extraction( |
| 294 | 3x |
grdata, |
| 295 | 3x |
nom_coldt = "debut_pas", |
| 296 | 3x |
annee = FALSE, |
| 297 | 3x |
mois = TRUE, |
| 298 | 3x |
quinzaine = TRUE, |
| 299 | 3x |
semaine = TRUE, |
| 300 | 3x |
jour_an = TRUE, |
| 301 | 3x |
jour_mois = FALSE, |
| 302 | 3x |
heure = FALSE |
| 303 |
) |
|
| 304 |
|
|
| 305 |
# to rescale everything on the same graph |
|
| 306 | 3x |
maxeff = floor(log10(max(grdata$effectif_total, na.rm = TRUE))) |
| 307 |
|
|
| 308 | 3x |
for (i in 1:length(variables_quant)) {
|
| 309 | 6x |
diff = maxeff - round(log10(max(tableauCEquan[tableauCEquan$stm_libelle == |
| 310 | 6x |
variables_quant[i], "valeur"], na.rm = TRUE))) |
| 311 | 6x |
if (diff != 0 & !is.na(diff)) {
|
| 312 | 6x |
tableauCEquan[tableauCEquan$stm_libelle == variables_quant[i], "valeur"] = as.numeric(tableauCEquan[tableauCEquan$stm_libelle == |
| 313 | 6x |
variables_quant[i], "valeur"]) * 10 ^ diff |
| 314 | 6x |
variables_quant[i] = paste(variables_quant[i], ".10^", diff, sep = "") |
| 315 | 6x |
} # end if |
| 316 | 3x |
} #end for |
| 317 | 3x |
yqualitatif = (10 ^ (maxeff)) / 2 |
| 318 |
|
|
| 319 | 3x |
ylegend = gettextf( |
| 320 | 3x |
"Number, %s, %s", |
| 321 | 3x |
paste(variables_quant, collapse = ", "), |
| 322 | 3x |
paste(variables_qual, collapse = ", ") |
| 323 |
) |
|
| 324 |
|
|
| 325 |
|
|
| 326 |
###################### |
|
| 327 |
# treatment of data to group by dc |
|
| 328 |
# if several taxa or stages are passed, they are aggregated with a warning |
|
| 329 |
################################# |
|
| 330 | 3x |
if (length(unique(taxa)) > 1) |
| 331 | 3x |
warning(gettextf( |
| 332 | 3x |
"you have %s taxa in the report, those will be aggregated", |
| 333 | 3x |
length(unique(taxa)) |
| 334 |
)) |
|
| 335 | 3x |
if (length(unique(stage)) > 1) |
| 336 | 3x |
warning(gettextf( |
| 337 | 3x |
"you have %s stages in the report, those will be aggregated", |
| 338 | 3x |
length(unique(stage)) |
| 339 |
)) |
|
| 340 | 3x |
plotdata <- |
| 341 | 3x |
dplyr::select(grdata, debut_pas, DC, effectif_total) %>% dplyr::rename(date = |
| 342 | 3x |
debut_pas) %>% |
| 343 | 3x |
dplyr::group_by(date, DC) %>% dplyr::summarize(effectif = sum(effectif_total)) %>% |
| 344 | 3x |
dplyr::ungroup() |
| 345 |
|
|
| 346 |
####################### |
|
| 347 |
# color scheme for station |
|
| 348 |
####################### |
|
| 349 |
|
|
| 350 | 3x |
cs <- |
| 351 | 3x |
colortable(color = color_station, |
| 352 | 3x |
vec = unique(tableauCE$stm_libelle), |
| 353 | 3x |
palette = "Accent") |
| 354 | 3x |
cs <- stacomirtools::chnames(cs, "name", "stm_libelle") |
| 355 |
####################### |
|
| 356 |
# color scheme for dc |
|
| 357 |
####################### |
|
| 358 | 3x |
cdc <- |
| 359 | 3x |
colortable(color = color_dc, |
| 360 | 3x |
vec = dc, |
| 361 | 3x |
color_function = "gray.colors") |
| 362 | 3x |
cdc <- stacomirtools::chnames(cdc, "name", "DC") |
| 363 |
####################### |
|
| 364 |
# merging with colors for manual scales |
|
| 365 |
###################### |
|
| 366 | 3x |
plotdata <- killfactor(merge(plotdata, cdc, by = "DC")) |
| 367 | 3x |
tableauCEquan <- |
| 368 | 3x |
killfactor(merge(tableauCEquan, cs, by = "stm_libelle")) |
| 369 | 3x |
tableauCEqual <- |
| 370 | 3x |
killfactor(merge(tableauCEqual, cs, by = "stm_libelle")) |
| 371 |
###################### |
|
| 372 |
# source of data |
|
| 373 |
####################### |
|
| 374 | 3x |
source <- paste("source:",paste(unique(tableauCE$source), collapse=", "))
|
| 375 |
|
|
| 376 |
|
|
| 377 | 3x |
g <- ggplot(plotdata) + |
| 378 | 3x |
geom_bar(aes(x = date, y = effectif, fill = color), |
| 379 | 3x |
position = "stack", |
| 380 | 3x |
stat = "identity") + |
| 381 | 3x |
ylab(ylegend) + |
| 382 | 3x |
geom_line(aes(x = date, y = valeur, colour = color), |
| 383 | 3x |
data = tableauCEquan, |
| 384 | 3x |
size = 1) + |
| 385 | 3x |
geom_point( |
| 386 | 3x |
aes( |
| 387 | 3x |
x = date, |
| 388 | 3x |
shape = valeur, |
| 389 | 3x |
colour = color |
| 390 |
), |
|
| 391 | 3x |
y = yqualitatif, |
| 392 | 3x |
data = tableauCEqual, |
| 393 | 3x |
size = 3 |
| 394 |
) + |
|
| 395 | 3x |
scale_fill_identity(name = gettext("DC"),
|
| 396 | 3x |
labels = dc_code, |
| 397 | 3x |
guide = "legend") + |
| 398 | 3x |
scale_colour_identity( |
| 399 | 3x |
name = gettext("stations"),
|
| 400 | 3x |
labels = cs[, "stm_libelle"], |
| 401 | 3x |
breaks = cs[, "color"], |
| 402 | 3x |
guide = "legend" |
| 403 |
) + |
|
| 404 | 3x |
scale_shape(guide = "legend", name = gettext("Qualitative parm")) +
|
| 405 | 3x |
theme_bw() + |
| 406 | 3x |
labs(caption=source) |
| 407 | 3x |
print(g) |
| 408 | 3x |
assign("g", g, envir_stacomi)
|
| 409 | 3x |
if (!silent) |
| 410 | 3x |
funout( |
| 411 | 3x |
gettext( |
| 412 | 3x |
"the ggplot object has been assigned to envir_stacomi, type g<-get('g',envir_stacomi)"
|
| 413 |
) |
|
| 414 |
) |
|
| 415 | 3x |
return(invisible(NULL)) |
| 416 |
} |
|
| 417 |
)# end function |
| 1 | ||
| 2 | ||
| 3 |
#' function used for some lattice graphs with dates |
|
| 4 |
#' @param vectordate date or POSIXt |
|
| 5 |
#' @return vectordate (without class) |
|
| 6 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 7 |
graphdate<-function(vectordate){
|
|
| 8 | ! |
vectordate <- as.POSIXct(vectordate) |
| 9 | ! |
attributes(vectordate) <- NULL |
| 10 | ! |
unclass(vectordate) |
| 11 | ! |
return(vectordate) |
| 12 |
} |
|
| 13 | ||
| 14 | ||
| 15 | ||
| 16 | ||
| 17 | ||
| 18 | ||
| 19 | ||
| 20 |
#' function used to remove special non utf8 character which cause the gtk |
|
| 21 |
#' interface to crash |
|
| 22 |
#' |
|
| 23 |
#' |
|
| 24 |
#' @param text a text string which might contain no utf8 characters |
|
| 25 |
#' @return text |
|
| 26 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 27 |
fun_char_spe<-function(text){
|
|
| 28 | ! |
text <- gsub("\u00e9","e",text)
|
| 29 | ! |
text <- gsub("\u00e8","e",text)
|
| 30 | ! |
text <- gsub("\u00ea","e",text)
|
| 31 | ! |
text <- gsub("\u00e0","a",text)
|
| 32 | ! |
return(text)} |
| 33 | ||
| 34 | ||
| 35 | ||
| 36 | ||
| 37 | ||
| 38 | ||
| 39 | ||
| 40 | ||
| 41 |
#' Transforms a vector into a string called within an sql command e.g. |
|
| 42 |
#' c('A','B','C') => in ('A','B','C')
|
|
| 43 |
#' |
|
| 44 |
#' Transforms a vector into a string called within an sql command e.g. c(A,B,C) |
|
| 45 |
#' => in ('A','B','C')
|
|
| 46 |
#' |
|
| 47 |
#' |
|
| 48 |
#' @param vect a character vector |
|
| 49 |
#' @return A list of value |
|
| 50 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 51 |
#' @export |
|
| 52 |
vector_to_listsql<-function(vect) |
|
| 53 |
{
|
|
| 54 | 531x |
if (is.null(vect)) stop("The vector passed to vector_to_listsql should not be null")
|
| 55 | 531x |
if (any(is.na(vect))) stop("The vector passed to vector_to_listsql should not be NA")
|
| 56 | 531x |
if (length(vect)==0) stop("The vector passed to vector_to_listsql should not be of lenght zero")
|
| 57 | 530x |
if (length(vect)==1) |
| 58 |
{
|
|
| 59 | 301x |
listsql=paste("(","'",vect,"'",")",sep="")
|
| 60 |
} |
|
| 61 |
|
|
| 62 | 530x |
if (length(vect)>2) |
| 63 |
{
|
|
| 64 | 130x |
listsql=paste("(","'",vect[1],"'",",", sep="")
|
| 65 | 130x |
for(j in 2:(length(vect)-1)){
|
| 66 | 2273x |
listsql=paste(listsql,"'",vect[j],"'",",",sep="") |
| 67 |
} |
|
| 68 | 130x |
listsql=paste(listsql,"'",vect[length(vect)],"'",")", sep="") |
| 69 |
} |
|
| 70 | 530x |
else if (length(vect)==2) |
| 71 |
{
|
|
| 72 | 99x |
listsql=paste("(","'",vect[1],"'",",", sep="")
|
| 73 | 99x |
listsql=paste(listsql,"'",vect[length(vect)],"'",")", sep="") |
| 74 |
} |
|
| 75 |
|
|
| 76 | 530x |
return(listsql) |
| 77 |
} |
|
| 78 | ||
| 79 | ||
| 80 | ||
| 81 |
#' Create a dataframe suitable for charts per 24h and day |
|
| 82 |
#' |
|
| 83 |
#' This functions takes a data frame with a column with starting time and another with ending time |
|
| 84 |
#' If the period extends over midnight, it will be split into new lines, starting and ending at midnight |
|
| 85 |
#' |
|
| 86 |
#' @param data The dataframe |
|
| 87 |
#' @param horodatedebut The beginning time |
|
| 88 |
#' @param horodatefin The ending time |
|
| 89 |
#' @return A data frame with four new columns, Hmin (hour min), Hmax (hmax), xmin (day) and xmax (next day), |
|
| 90 |
#' and new rows |
|
| 91 |
#' @author cedric.briand |
|
| 92 |
#' @examples |
|
| 93 |
#' datatemp<-structure(list(per_dis_identifiant = c(1L, 1L, 1L), |
|
| 94 |
#' per_date_debut = structure(c(1420056600, |
|
| 95 |
#' 1420071000, 1420081200), class = c("POSIXct", "POSIXt"), tzone = ""),
|
|
| 96 |
#' per_date_fin = structure(c(1420071000, 1420081200, 1421000000 |
|
| 97 |
#' ), class = c("POSIXct", "POSIXt"), tzone = ""), per_commentaires = c("fonct calcul",
|
|
| 98 |
#' "fonct calcul", "fonct calcul"), per_etat_fonctionnement = c(1L, |
|
| 99 |
#' 0L, 0L), per_tar_code = 1:3, libelle = c("Fonc normal", "Arr ponctuel",
|
|
| 100 |
#' "Arr maint")), .Names = c("per_dis_identifiant", "per_date_debut",
|
|
| 101 |
#' "per_date_fin", "per_commentaires", "per_etat_fonctionnement", |
|
| 102 |
#' "per_tar_code", "libelle"), row.names = c(NA, 3L), class = "data.frame") |
|
| 103 |
#' newdf<-split_per_day(data=datatemp,horodatedebut="per_date_debut", |
|
| 104 |
#' horodatefin="per_date_fin") |
|
| 105 |
#' @export |
|
| 106 |
split_per_day<-function(data,horodatedebut,horodatefin){
|
|
| 107 | 5x |
if(!horodatedebut%in%colnames(data)) stop("horodatedebut not in column names for data")
|
| 108 | 5x |
if(!horodatefin%in%colnames(data)) stop("horodatefin not column names for data")
|
| 109 | 5x |
data$Hdeb<-as.numeric(strftime(data[,horodatedebut],"%H"))+as.numeric(strftime(data[,horodatedebut],"%M"))/60 |
| 110 | 5x |
data$Hfin<-as.numeric(strftime(data[,horodatefin],"%H"))+round(as.numeric(strftime(data[,horodatefin],"%M"))/60,2) |
| 111 | 5x |
data$xmin<-lubridate::floor_date(data[,horodatedebut],unit="day") # pour les graphiques en rectangle |
| 112 | 5x |
data$xmax<-data$xmin+lubridate::days(1) |
| 113 |
# number of times we pass to midnigth |
|
| 114 |
# round is for when we switch hour |
|
| 115 | 5x |
data$n0<-round(difftime(floor_date(data[,horodatefin],unit="day"),floor_date(data[,horodatedebut],unit="day"),units="days")) |
| 116 |
# rows that will be duplicated |
|
| 117 | 5x |
data$id=sequence(nrow(data)) |
| 118 | 5x |
data<-data[rep(sequence(nrow(data)),data$n0+1),] |
| 119 | 5x |
data$newid<-sequence(nrow(data)) |
| 120 |
# within a group where dates overlap between two days |
|
| 121 |
#the first will and all lines except the last be set 24 for Hfin |
|
| 122 | 5x |
data1<-data%>%filter(n0>0)%>%group_by(id)%>%filter(min_rank(desc(newid)) !=1)%>%mutate("Hfin"=24)
|
| 123 |
#replacing rows in data |
|
| 124 | 5x |
data[match(data1$newid,data$newid),]<-data1 |
| 125 |
# all except the first will be set 0 to Hdeb |
|
| 126 | 5x |
data2<-data%>%filter(n0>0)%>%group_by(id)%>%filter(min_rank(newid) !=1)%>%mutate("Hdeb"=0)
|
| 127 |
#replacing rows in data |
|
| 128 | 5x |
data[match(data2$newid,data$newid),]<-data2 |
| 129 |
# now get the sequence of days righly set by adding the number of days to xmin and xmax |
|
| 130 | 5x |
data3<-data%>%filter(n0>0)%>%group_by(id)%>%mutate(xmin=xmin+ as.difftime(rank(newid)-1, units="days"), |
| 131 | 5x |
xmax=xmax+as.difftime(rank(newid)-1, units="days")) |
| 132 | 5x |
data[match(data3$newid,data$newid),]<-data3 |
| 133 | 5x |
data<-as.data.frame(data) |
| 134 | 5x |
return(data) |
| 135 |
} |
|
| 136 | ||
| 137 |
#' This function extracts temporal characteristics from a dataframe |
|
| 138 |
#' |
|
| 139 |
#' |
|
| 140 |
#' @param data a data frame containing a Date or POSIXt column |
|
| 141 |
#' @param nom_coldt the name of the column containing date or POSIXt entry to |
|
| 142 |
#' be processed |
|
| 143 |
#' @param annee logical do you want a column describing year to be added to the |
|
| 144 |
#' dataframe |
|
| 145 |
#' @param mois logical, add column with month |
|
| 146 |
#' @param quinzaine logical, add column with 15 days |
|
| 147 |
#' @param semaine logical, add column with weeks |
|
| 148 |
#' @param semaine_std logical, add column with standard weeks (using isoweek from lubridate) |
|
| 149 |
#' @param jour_an logical, add column with day of year |
|
| 150 |
#' @param jour_mois logical, add column with day of month |
|
| 151 |
#' @param heure logical, add column with hour |
|
| 152 |
#' @return The dataframe with date column filled |
|
| 153 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 154 |
#' @export |
|
| 155 |
fun_date_extraction=function(data, # tableau de donnees e importer |
|
| 156 |
nom_coldt, # nom de la colonne |
|
| 157 |
annee=TRUE, |
|
| 158 |
mois=TRUE, |
|
| 159 |
quinzaine=FALSE, |
|
| 160 |
semaine=TRUE, |
|
| 161 |
semaine_std=FALSE, |
|
| 162 |
jour_an=FALSE, |
|
| 163 |
jour_mois=TRUE, |
|
| 164 |
heure=FALSE |
|
| 165 |
){
|
|
| 166 | 44x |
if (annee) data$annee <- as.factor(strftime(as.POSIXlt(data[,nom_coldt]), format="%Y")) |
| 167 | 44x |
if (mois) data$mois <- as.factor(strftime(as.POSIXlt(data[,nom_coldt]), format="%m")) |
| 168 |
# %b Abbreviated month name in the current locale. (Also matches full name on input.) |
|
| 169 | 44x |
if (quinzaine) {
|
| 170 | 29x |
data$quinzaine=ceiling(as.numeric(strftime(as.POSIXlt(data[,nom_coldt]), |
| 171 | 29x |
format="%W"))/2) |
| 172 | 29x |
data$quinzaine <- as.character(data$quinzaine) |
| 173 | 29x |
data$quinzaine[as.numeric(data$quinzaine)<10] <- paste("0", data$quinzaine[as.numeric(data$quinzaine)<10],sep="")
|
| 174 | 29x |
data$quinzaine <- as.factor(data$quinzaine) |
| 175 |
} |
|
| 176 | 44x |
if (semaine) data$semaine <- as.factor(strftime(as.POSIXlt(data[,nom_coldt]), format="%W")) |
| 177 |
#%W : Week of the year as decimal number (00e53) using Monday as the first day of week (and typically with the first Monday of the year as day 1 of week 1). The UK convention |
|
| 178 | 44x |
if (jour_an) data$jour_365 <- strftime(as.POSIXlt(data[,nom_coldt]), format="%j") |
| 179 | 44x |
if (jour_mois) data$jour_mois <- as.factor(strftime(as.POSIXlt(data[,nom_coldt]), format="%d")) |
| 180 |
# %d : Day of the month as decimal number (01e31). |
|
| 181 | 44x |
if (heure) data$heure <- as.factor(strftime(as.POSIXlt(data[,nom_coldt]), format="%H")) |
| 182 |
#%H Hours as decimal number (00e23). |
|
| 183 | 44x |
if (semaine_std) data$semaine_std=lubridate::isoweek(as.POSIXlt(data[,nom_coldt])) |
| 184 | 44x |
return(data) |
| 185 |
} |
|
| 186 | ||
| 187 | ||
| 188 |
#' Builds a table with colors to merge with a dataframe for later |
|
| 189 |
#' use in ggplot. An initial check will be done |
|
| 190 |
#' on the name of the color vector. A data frame is built. It contains a column color which is a factor. |
|
| 191 |
#' The factor order match the order of the vector (not the alphabetical order of the colors). |
|
| 192 | ||
| 193 |
#' |
|
| 194 |
#' @param color Either null (default) or a named vector of colors, the |
|
| 195 |
#' names should correspond to the values of vec |
|
| 196 |
#' @param vec The vector to match the color with, if a named vector |
|
| 197 |
#' or color is supplied the names should match |
|
| 198 |
#' @param palette, the name of the RColorBrewer palette, defaults to "Set2", ignored for other |
|
| 199 |
#' color gradient functions and if a named vector of colors is provided |
|
| 200 |
#' @param color_function, the name of the function used to brew the colors, one for |
|
| 201 |
#' "brewer.pal", "gray.colors", "random", default to "brewer.pal, this argument is ignored if a |
|
| 202 |
#' named vector of color is passed. |
|
| 203 |
#' @return A dataframe with two columns, the vector (name) and the color (color) as a reordered factor |
|
| 204 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 205 |
#' @export |
|
| 206 |
colortable <- function(color=NULL, vec, palette="Set2", color_function=c("brewer.pal","gray.colors","random")){
|
|
| 207 | 19x |
color_function <- match.arg(color_function, choices = c("brewer.pal","gray.colors","random"))
|
| 208 | 19x |
if (is.null(color)) {
|
| 209 | 11x |
if (color_function == "brewer.pal") {
|
| 210 | 10x |
number_available <- RColorBrewer::brewer.pal.info[rownames(RColorBrewer::brewer.pal.info)==palette,"maxcolors"] |
| 211 | 10x |
if (number_available>=length(vec)){
|
| 212 | 10x |
color <- RColorBrewer::brewer.pal(length(vec),name=palette)[1:length(vec)] # 1:length(vec) as palette return minimum 3 values |
| 213 |
} else {
|
|
| 214 | ! |
message(gettextf("Palette %s has only got %s values and you need %s", palette, number_available, length(vec)))
|
| 215 | ! |
qual_col_pals <- RColorBrewer::brewer.pal.info[RColorBrewer::brewer.pal.info$category == 'qual',] |
| 216 | ! |
color <- sample(unlist(mapply(RColorBrewer::brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals))),length(vec)) |
| 217 |
} |
|
| 218 | 11x |
} else if (color_function == "gray.colors"){
|
| 219 | 1x |
color=grDevices::gray.colors(length(vec)) |
| 220 | 11x |
} else if (color_function == "random"){
|
| 221 | ! |
color <- grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)]
|
| 222 | ! |
color <- sample(color, size=length(vec)) |
| 223 |
} |
|
| 224 | 10x |
names(color)<-vec |
| 225 | 19x |
} else if (length(color) != length(vec)){
|
| 226 | 1x |
funout(gettextf("The color argument should have length %s", length(vec)), arret=TRUE)
|
| 227 |
} |
|
| 228 | 17x |
if (!all(names(color)%in%vec)) {
|
| 229 | ! |
stop (gettextf("The following name(s) %s do not match vector name: %s",
|
| 230 | ! |
names(color)[!names(color)%in%vec], |
| 231 | ! |
paste(vec, collapse=", "))) |
| 232 |
} |
|
| 233 |
# creating a data frame to pass to merge later (to get the color in the data frame) |
|
| 234 | 17x |
cs <- data.frame(name=names(color), color=color) |
| 235 |
# problem with different order (set by color name) implying different order |
|
| 236 |
# in the graph (ie by color not by car_val_identifiant |
|
| 237 | 17x |
cs$color <- as.factor(cs$color) |
| 238 | 17x |
bonordre <- match(cs$color, levels(cs$color)) |
| 239 | 17x |
cs$color <- factor(cs$color, levels(cs$color)[bonordre]) |
| 240 | 17x |
return(cs) |
| 241 |
} |
|
| 242 | ||
| 243 | ||
| 244 |
#' this function displays text and will be used to convey stacomiR message in shiny |
|
| 245 |
#' |
|
| 246 |
#' |
|
| 247 |
#' @param text The text to displaying the R |
|
| 248 |
#' console and later in shiny |
|
| 249 |
#' @param arret Should this cause the program to stop ? |
|
| 250 |
#' @param ... Additional parameters passed to print |
|
| 251 |
#' @return nblignes Assigned in envir_stacomi |
|
| 252 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 253 |
#' @keywords internal |
|
| 254 |
funout <- function(text,arret=FALSE,...){
|
|
| 255 | 101x |
if(arret) stop(text) else print(text,quote=FALSE,...) |
| 256 |
} |
|
| 257 | ||
| 258 |
#' this function gets the schema from envir stacomi and throws warning |
|
| 259 |
#' |
|
| 260 |
#' @param default passed to rlang::get_env |
|
| 261 |
#' @return The schema in envir_stacomi |
|
| 262 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 263 |
#' @keywords internal |
|
| 264 |
get_schema <- function(default=NULL){
|
|
| 265 | 1448x |
if (!exists("envir_stacomi")) stop("envir_stacomi not created did you run stacomi() ?")
|
| 266 | 1448x |
sch <- rlang::env_get(envir_stacomi, "sch", default=default) |
| 267 | 1448x |
if (is.null(sch)) stop("program failure, sch not in envir_stacomi")
|
| 268 | 1448x |
return(sch) |
| 269 |
} |
|
| 270 | ||
| 271 |
#' this function gets the name of the stucture as it is set in the database |
|
| 272 |
#' |
|
| 273 |
#' @return The name of the structure (org_code) |
|
| 274 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 275 |
#' @keywords internal |
|
| 276 |
get_org <- function(){
|
|
| 277 | 13x |
return(toupper(gsub("\\.", "", get_schema())))
|
| 278 |
} |
| 1 |
#' functions called in DF and DC |
|
| 2 |
#' |
|
| 3 |
#' |
|
| 4 |
#' |
|
| 5 |
#' @param typeperiode ref.tr_typearretdisp_tar(per_tar_code) the code of the |
|
| 6 |
#' period (see table ref.tr_typearretdisp_tar) |
|
| 7 |
#' @param tempsdebut ref.tr_typearretdisp_tar(per_date_debut) starting |
|
| 8 |
#' timestamp of the period |
|
| 9 |
#' @param tempsfin The postgres column ref.tr_typearretdisp_tar(per_date_fin) ending timestamp of |
|
| 10 |
#' the period |
|
| 11 |
#' @param libelle The postgres column ref.tr_typearretdisp_tar(libelle )description of the period |
|
| 12 |
#' type |
|
| 13 |
#' @param color A named vector of color matching libelle. |
|
| 14 |
#' @param date Boolean, should the function return a POSIXt or date value |
|
| 15 |
#' @return A list |
|
| 16 |
#' @note returns either POSIXt or date if date=TRUE |
|
| 17 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 18 |
fun_table_per_dis <- function(typeperiode, tempsdebut, tempsfin, libelle, color, date = TRUE) {
|
|
| 19 | 16x |
listeg = list() |
| 20 | 16x |
for (j in 1:5) {
|
| 21 | 80x |
if (!date) {
|
| 22 |
# pour utilisation ulterieure de la classe Posixct |
|
| 23 | 60x |
if (sum(unique(typeperiode) == j) > 0) {
|
| 24 | 38x |
choice_periode <- typeperiode == j |
| 25 | 38x |
liste <- list(debut = tempsdebut[choice_periode], |
| 26 | 38x |
fin = tempsfin[choice_periode], |
| 27 | 38x |
nom = libelle[choice_periode][1], |
| 28 | 38x |
color = color[choice_periode][1]) |
| 29 | 38x |
listeg[[as.character(j)]] <- liste |
| 30 |
} |
|
| 31 |
# pour utilisation ulterieure de la classe date |
|
| 32 |
} else {
|
|
| 33 | 20x |
if (sum(unique(typeperiode) == j) > 0) {
|
| 34 | 10x |
choice_periode <- typeperiode == j |
| 35 | 10x |
liste <- list( |
| 36 | 10x |
debut = as.Date(tempsdebut[choice_periode]), |
| 37 | 10x |
fin = as.Date(tempsfin[choice_periode]), |
| 38 | 10x |
nom = as.character(libelle[choice_periode][1]), |
| 39 | 10x |
color = color[choice_periode][1]) |
| 40 | 10x |
listeg[[as.character(j)]] <- liste |
| 41 |
} |
|
| 42 |
} |
|
| 43 |
} |
|
| 44 | 16x |
return(listeg) |
| 45 |
} |
| 1 |
#' Validity check for ref_horodate |
|
| 2 |
#' |
|
| 3 |
#' @param object A ref_horodate object |
|
| 4 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 5 |
#' @keywords internal |
|
| 6 |
validity_ref_horodate = function(object) |
|
| 7 |
{
|
|
| 8 | ! |
rep1 = inherits(object@horodate[2], "POSIXt") |
| 9 |
|
|
| 10 | ! |
return(ifelse(rep1, TRUE, FALSE)) |
| 11 |
} |
|
| 12 | ||
| 13 | ||
| 14 |
#' Class ref_horodate |
|
| 15 |
#' |
|
| 16 |
#' choice of date with method to show current and previous year |
|
| 17 |
#' |
|
| 18 |
#' |
|
| 19 |
#' @slot horodate a "POSIXt" |
|
| 20 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 21 |
#' \code{new("ref_horodate", \dots{})}.
|
|
| 22 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 23 |
#' @family referential objects |
|
| 24 |
setClass( |
|
| 25 |
Class = "ref_horodate", |
|
| 26 |
representation = |
|
| 27 |
representation(horodate = "POSIXt"), |
|
| 28 |
validity = validity_ref_horodate, |
|
| 29 |
prototype = prototype(horodate = Hmisc::roundPOSIXt(Sys.time(), "years")) |
|
| 30 |
) |
|
| 31 | ||
| 32 | ||
| 33 |
#' Choice_c method for ref_horodate |
|
| 34 |
#' @aliases choice_c.ref_horodate |
|
| 35 |
#' @param object An object of class \link{ref_horodate-class}
|
|
| 36 |
#' @param nomassign The name assigned in environment envir_stacomi |
|
| 37 |
#' @param funoutlabel, text displayed by the interface |
|
| 38 |
#' @param silent Default FALSE, should messages be displayed |
|
| 39 |
#' @param horodate The horodate to set, formats "\%d/\%m/\%Y \%H:\%M:\%s", "\%d/\%m/\%y \%H:\%M:\%s", "\%Y-\%m-\%d \%H:\%M:\%s" formats |
|
| 40 |
#' can also be passed with the date set to the minute \%d/\%m/\%Y \%H:\%M or the day \%d/\%m/\%Y |
|
| 41 |
#' \dots are accepted. The choice_c method assigns and |
|
| 42 |
#' @return An object of class \link{ref_horodate-class} with slot \emph{horodate} set,
|
|
| 43 |
#' and assigns an object of class POSIXt with name nomassign in envir_stacomi |
|
| 44 |
setMethod( |
|
| 45 |
"choice_c", |
|
| 46 |
signature = signature("ref_horodate"),
|
|
| 47 |
definition = function(object, |
|
| 48 |
nomassign = "horodate", |
|
| 49 |
funoutlabel = "nous avons le choix dans la date\n", |
|
| 50 |
#decal=0, |
|
| 51 |
horodate, |
|
| 52 |
silent = FALSE) {
|
|
| 53 |
# horodate="2013-01-01" |
|
| 54 |
# parse the horohorodate |
|
| 55 | 57x |
if (length(horodate) > 1) |
| 56 | ! |
stop("horodate should be a vector of length 1")
|
| 57 | 57x |
if (is.null(horodate)) |
| 58 | ! |
stop("horodate should not be null")
|
| 59 | 57x |
if (inherits(horodate, "character")) {
|
| 60 | 57x |
if (grepl("/", horodate)) {
|
| 61 | 3x |
.horodate = strptime(horodate, format = "%d/%m/%Y %H:%M:%s") |
| 62 | 3x |
if (is.na(.horodate)) {
|
| 63 | 2x |
.horodate = strptime(horodate, format = "%d/%m/%y %H:%M:%s") |
| 64 |
} |
|
| 65 | 3x |
if (is.na(.horodate)) {
|
| 66 | 2x |
.horodate = strptime(horodate, format = "%d/%m/%y %H:%M") |
| 67 |
} |
|
| 68 | 3x |
if (is.na(.horodate)) {
|
| 69 | 2x |
.horodate = strptime(horodate, format = "%d/%m/%Y %H:%M") |
| 70 |
} |
|
| 71 | 3x |
if (is.na(.horodate)) {
|
| 72 | 1x |
.horodate = strptime(horodate, format = "%d/%m/%y") |
| 73 |
} |
|
| 74 | 3x |
if (is.na(.horodate)) {
|
| 75 | 1x |
.horodate = strptime(horodate, format = "%d/%m/%Y") |
| 76 |
} |
|
| 77 | 54x |
} else if (grepl("-", horodate)) {
|
| 78 | 51x |
.horodate = strptime(horodate, format = "%Y-%m-%d %H:%M:%s") |
| 79 | 51x |
if (is.na(.horodate)) {
|
| 80 | 51x |
.horodate = strptime(horodate, format = "%d-%m-%Y %H:%M:%s") |
| 81 |
} |
|
| 82 | 51x |
if (is.na(.horodate)) {
|
| 83 | 51x |
.horodate = strptime(horodate, format = "%Y-%m-%d %H:%M") |
| 84 |
} |
|
| 85 | 51x |
if (is.na(.horodate)) {
|
| 86 | 50x |
.horodate = strptime(horodate, format = "%d-%m-%Y %H:%M") |
| 87 |
} |
|
| 88 | 51x |
if (is.na(.horodate)) {
|
| 89 | 49x |
.horodate = strptime(horodate, format = "%Y-%m-%d") |
| 90 |
} |
|
| 91 | 51x |
if (is.na(.horodate)) {
|
| 92 | ! |
.horodate = strptime(horodate, format = "%d-%m-%Y") |
| 93 |
} |
|
| 94 |
} else {
|
|
| 95 | 3x |
stop( |
| 96 | 3x |
"Formatting problem, the character vector you are trying to pass as horodate could not be parsed. Check example or documentation" |
| 97 |
) |
|
| 98 |
} |
|
| 99 |
|
|
| 100 | ! |
} else if (inherits(horodate, "Date")) {
|
| 101 | ! |
.horodate <- as.POSIXlt(horodate) |
| 102 | ! |
} else if (inherits(horodate[2] , "POSIXt")) {
|
| 103 | ! |
.horodate = horodate |
| 104 |
} |
|
| 105 | 54x |
if (is.na(.horodate)) |
| 106 | 1x |
stop( |
| 107 | 1x |
"Formatting problem, the character vector you are trying to pass as horodate could not be parsed. Check example or documentation" |
| 108 |
) |
|
| 109 | 53x |
object@horodate = .horodate |
| 110 | 53x |
validObject(object) |
| 111 | 53x |
assign(nomassign, object@horodate, envir_stacomi) |
| 112 | 53x |
if (!silent) |
| 113 | 5x |
funout(funoutlabel) |
| 114 | 53x |
return(object) |
| 115 |
} |
|
| 116 |
) |
| 1 |
#' Class 'ref_stage' |
|
| 2 |
#' |
|
| 3 |
#' Representation of a fish phase |
|
| 4 |
#' |
|
| 5 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 6 |
#' \code{new('ref_stage', data='data.frame')}. \describe{
|
|
| 7 |
#' \item{list('data')}{Object of class \code{'data.frame'} ~ The phases
|
|
| 8 |
#' available in the database}\item{:}{Object of class \code{'data.frame'} ~ The
|
|
| 9 |
#' phases available in the database} } |
|
| 10 |
#' @slot data A data frame containing data loaded from the database by either charge or charge_with_filter methods |
|
| 11 |
#' @slot stage_selected Contains the code \code{'tax_code'} of the stage selected by choice_c() method
|
|
| 12 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 13 |
#' @keywords classes |
|
| 14 |
#' @family referential objects |
|
| 15 |
setClass(Class = "ref_stage", representation = representation(data = "data.frame", stage_selected="character")) |
|
| 16 | ||
| 17 |
#' Loading method for ref_stage referential objects |
|
| 18 |
#' @param object An object of class \link{ref_stage-class}
|
|
| 19 |
#' @return An S4 object of class \link{ref_stage-class} with all stages available in the database
|
|
| 20 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 21 |
#' @examples |
|
| 22 |
#' \dontrun{
|
|
| 23 |
#' object=new('ref_stage')
|
|
| 24 |
#' charge(object) |
|
| 25 |
#' } |
|
| 26 |
setMethod("charge", signature = signature("ref_stage"), definition = function(object) {
|
|
| 27 | 2x |
req = new("RequeteDB")
|
| 28 | 2x |
req@sql = "SELECT std_code, std_libelle FROM ref.tr_stadedeveloppement_std ORDER BY std_code ;" |
| 29 | 2x |
req <- stacomirtools::query(req) |
| 30 | 2x |
object@data <- req@query |
| 31 | 2x |
return(object) |
| 32 |
}) |
|
| 33 | ||
| 34 | ||
| 35 |
#' Loading method for ref_stage referential objects searching only those stages existing for a DC and a Taxon |
|
| 36 |
#' @param object An object of class \link{ref_stage-class}
|
|
| 37 |
#' @param dc_selected The selected counting device |
|
| 38 |
#' @param taxa_selected The selected species |
|
| 39 |
#' @return An S4 object of class \link{ref_stage-class} listing all stages available for one DC and one taxon
|
|
| 40 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 41 |
#' @examples |
|
| 42 |
#' \dontrun{
|
|
| 43 |
#' dc_selected=6 |
|
| 44 |
#'taxa_selected=2038 |
|
| 45 |
#' object=new('ref_stage')
|
|
| 46 |
#' charge_with_filter(object,dc_selected,taxa_selected) |
|
| 47 |
#' } |
|
| 48 |
setMethod("charge_with_filter", signature = signature("ref_stage"), definition = function(object,
|
|
| 49 |
dc_selected, taxa_selected) {
|
|
| 50 | 54x |
requete = new("RequeteDBwhere")
|
| 51 | 54x |
requete@select = paste("SELECT DISTINCT ON (std_code) std_code, std_libelle",
|
| 52 | 54x |
" FROM ", get_schema(), "tg_dispositif_dis", " JOIN ", |
| 53 | 54x |
get_schema(), "t_dispositifcomptage_dic on dis_identifiant=dic_dis_identifiant", |
| 54 | 54x |
" JOIN ", get_schema(), "t_operation_ope on ope_dic_identifiant=dic_dis_identifiant", |
| 55 | 54x |
" JOIN ", get_schema(), "t_lot_lot on lot_ope_identifiant=ope_identifiant", |
| 56 | 54x |
" JOIN ref.tr_stadedeveloppement_std on lot_std_code=std_code", sep = "") |
| 57 | 54x |
requete@where = paste("where dis_identifiant in ", vector_to_listsql(dc_selected),
|
| 58 | 54x |
sep = "") |
| 59 | 54x |
requete@and = paste("and lot_tax_code in ", vector_to_listsql(taxa_selected),
|
| 60 | 54x |
sep = "") |
| 61 | 54x |
requete@order_by = "ORDER BY std_code" |
| 62 | 54x |
requete <- stacomirtools::query(requete) # appel de la methode connect de l'object requeteDB |
| 63 | 54x |
object@data <- requete@query |
| 64 | 54x |
if (nrow(object@data) == 0) |
| 65 | ! |
funout(gettext("No data for this counting device and this taxa\n", domain = "R-stacomiR"),
|
| 66 | ! |
arret = TRUE) |
| 67 | 54x |
return(object) |
| 68 |
}) |
|
| 69 | ||
| 70 | ||
| 71 |
#' choice_c method for ref_stage |
|
| 72 |
#' |
|
| 73 |
#' the choice_c method is intended to have the same behaviour as choice (which creates a |
|
| 74 |
#' widget in the graphical interface) but from the command line. The values passed to the choice_c method |
|
| 75 |
#' for stage is the code. Any numeric value will be discarded |
|
| 76 |
#' @param object An object of class \link{ref_stage-class}
|
|
| 77 |
#' @param stage the vector of stages chosen |
|
| 78 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 79 |
#' @return An S4 object of class \link{ref_stage-class} with the stage selected in the data slot
|
|
| 80 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 81 |
#' @examples |
|
| 82 |
#' \dontrun{
|
|
| 83 |
#'object=new('ref_stage')
|
|
| 84 |
#'object<-charge(object) |
|
| 85 |
#' } |
|
| 86 |
setMethod("choice_c", signature = signature("ref_stage"), definition = function(object,
|
|
| 87 |
stage, silent = FALSE) {
|
|
| 88 | 55x |
if (is.null(stage)) {
|
| 89 | ! |
funout(gettext("No value for argument stage\n", domain = "R-stacomiR"), arret = TRUE)
|
| 90 |
} |
|
| 91 | 55x |
missing_std_libelle <- stage[!stage %in% object@data$std_code] |
| 92 | 55x |
if (length(missing_std_libelle) > 0 & !silent) |
| 93 | ! |
funout(gettextf("No data for this counting device and this taxa\n %s", stringr::str_c( missing_std_libelle,
|
| 94 | ! |
collapse = ", "), domain = "R-stacomiR")) |
| 95 | 55x |
object@stage_selected <- object@data[object@data$std_code %in% stage,"std_code"] |
| 96 | 55x |
if (nrow(object@data) == 0) {
|
| 97 | ! |
funout(gettext("Stop there is no line in the taxa table (problem with the DB link ?)\n",
|
| 98 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 99 |
} |
|
| 100 | 55x |
assign("ref_stage", object, envir = envir_stacomi)
|
| 101 | 55x |
return(object) |
| 102 |
}) |
| 1 |
#' Function for report_mig graphs including numbers DF DC operations |
|
| 2 |
#' |
|
| 3 |
#' This graph is for species other than glass eel |
|
| 4 |
#' |
|
| 5 |
#' |
|
| 6 |
#' @param report_mig An object of class \code{\linkS4class{report_mig}}
|
|
| 7 |
#' @param tableau A data frame with the with the following columns : No.pas,debut_pas,fin_pas, |
|
| 8 |
#' ope_dic_identifiant,lot_tax_code,lot_std_code,type_de_quantite,MESURE,CALCULE, |
|
| 9 |
#' EXPERT,PONCTUEL,Effectif_total,taux_d_echappement,coe_valeur_coefficient |
|
| 10 |
#' @note this function is intended to be called from the plot method in report_mig_mult and report_mig |
|
| 11 |
#' @param time.sequence A vector POSIXt |
|
| 12 |
#' @param taxa The species |
|
| 13 |
#' @param stage The stage |
|
| 14 |
#' @param dc The DC |
|
| 15 |
#' @param silent Message displayed or not |
|
| 16 |
#' @param color Default NULL, a vector of color in the following order, working, stopped, 1...5 types of operation |
|
| 17 |
#' for the fishway or DC, measured, calculated, expert, direct observation. If null will be set to brewer.pal(12,"Paired")[c(8,10,4,6,1,2,3,5,7)] |
|
| 18 |
#' @param color_ope Default NULL, a vector of color for the operations. Default to brewer.pal(4,"Paired") |
|
| 19 |
#' @param ... additional parameters passed to matplot, main, ylab, ylim, lty, pch, bty, cex.main, |
|
| 20 |
#' it is currenly not a good idea to change xlim (numbers are wrong, the month plot covers all month, and legend placement is wrong |
|
| 21 |
#' @return No return value, called for side effects |
|
| 22 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 23 |
fungraph = function(report_mig, |
|
| 24 |
tableau, |
|
| 25 |
time.sequence, |
|
| 26 |
taxa, |
|
| 27 |
stage, |
|
| 28 |
dc = NULL, |
|
| 29 |
silent, |
|
| 30 |
color = NULL, |
|
| 31 |
color_ope = NULL, |
|
| 32 |
...) {
|
|
| 33 |
#mat <- matrix(1:6,3,2) |
|
| 34 |
#layout(mat) |
|
| 35 |
#browser() |
|
| 36 |
#cat("fungraph")
|
|
| 37 |
# color=null |
|
| 38 |
# color calculation |
|
| 39 |
|
|
| 40 | 5x |
oldpar <- par(no.readonly = TRUE) |
| 41 | 5x |
on.exit(par(oldpar)) |
| 42 | 5x |
if (is.null(color)) {
|
| 43 | 5x |
tp <- RColorBrewer::brewer.pal(12, "Paired") |
| 44 | 5x |
mypalette = c( |
| 45 | 5x |
"working" = tp[4], |
| 46 | 5x |
"stopped" = tp[6], |
| 47 | 5x |
"Fonc normal" = tp[1], |
| 48 | 5x |
"Arr ponctuel" = tp[2], |
| 49 | 5x |
"Arr maintenance" = tp[3], |
| 50 | 5x |
"Dysfonc" = tp[5], |
| 51 | 5x |
"Non connu" = tp[7], |
| 52 | 5x |
"ponctuel" = "indianred", |
| 53 | 5x |
"expert" = "chartreuse2", |
| 54 | 5x |
"calcule" = "deepskyblue", |
| 55 | 5x |
"mesure" = "black" |
| 56 |
) |
|
| 57 |
} else {
|
|
| 58 | ! |
if (length(color) != 11) |
| 59 | ! |
stop("The length of color must be 11")
|
| 60 | ! |
mypalette = c( |
| 61 | ! |
"working" = color[1], |
| 62 | ! |
"stopped" = color[2], |
| 63 | ! |
"Fonc normal" = color[3], |
| 64 | ! |
"Arr ponctuel" = color[4], |
| 65 | ! |
"Arr maintenance" = color[5], |
| 66 | ! |
"Dysfonc" = color[6], |
| 67 | ! |
"Non connu" = color[7], |
| 68 | ! |
"mesure" = color[8], |
| 69 | ! |
"calcule" = color[9], |
| 70 | ! |
"expert" = color[10], |
| 71 | ! |
"ponctuel" = color[11] |
| 72 |
) |
|
| 73 |
} |
|
| 74 |
|
|
| 75 | 5x |
if (is.null(color_ope)) {
|
| 76 | 5x |
if (stacomirtools::is.odd(dc)) |
| 77 | 1x |
brew = "Paired" |
| 78 |
else |
|
| 79 | 4x |
brew = "Accent" |
| 80 | 5x |
color_ope = RColorBrewer::brewer.pal(8, brew) |
| 81 |
} |
|
| 82 |
|
|
| 83 | 5x |
if (is.null(dc)) |
| 84 | ! |
dc = report_mig@dc@dc_selected[1] |
| 85 | 5x |
annee = unique(strftime(as.POSIXlt(time.sequence), "%Y"))[1] |
| 86 | 5x |
mois = months(time.sequence) |
| 87 | 5x |
jour = strftime(as.POSIXlt(time.sequence), "%j") |
| 88 | 5x |
jmois = strftime(as.POSIXlt(time.sequence), "%d") |
| 89 | 5x |
mois = unique(mois) |
| 90 | 5x |
mois = paste("15", substr(as.character(mois), 1, 3))
|
| 91 | 5x |
index = as.vector(tableau$No.pas[jmois == 15]) |
| 92 | 5x |
x = 1:nrow(tableau) |
| 93 | 5x |
debut = unclass(as.POSIXct((min(time.sequence))))[[1]] # attention arrondit e un jour de moins |
| 94 | 5x |
fin = unclass(as.POSIXct(max(time.sequence)))[[1]] |
| 95 | 5x |
dis_commentaire = as.character(report_mig@dc@data$dis_commentaires[report_mig@dc@data$dc %in% |
| 96 | 5x |
dc]) # commentaires sur le DC |
| 97 |
################################### |
|
| 98 |
# Definition du layout |
|
| 99 |
#################################### |
|
| 100 | 5x |
vec <- c(rep(1, 15), rep(2, 2), rep(3, 2), 4, rep(5, 6)) |
| 101 | 5x |
mat <- matrix(vec, length(vec), 1) |
| 102 | 5x |
layout(mat) |
| 103 |
|
|
| 104 |
#par("bg"=grDevices::gray(0.8))
|
|
| 105 | 5x |
graphics::par("mar" = c(3, 4, 3, 2) + 0.1)
|
| 106 |
################################### |
|
| 107 |
# Graph annuel couvrant sequence >0 |
|
| 108 |
#################################### |
|
| 109 | 5x |
dots <- list(...) |
| 110 | 5x |
if (!"main" %in% names(dots)) |
| 111 | 5x |
main = gettextf("Migration graph %s, %s, %s, %s",
|
| 112 | 5x |
dis_commentaire, |
| 113 | 5x |
taxa, |
| 114 | 5x |
stage, |
| 115 | 5x |
annee, |
| 116 | 5x |
domain = "R-stacomiR") |
| 117 |
else |
|
| 118 | ! |
main = dots[["main"]] |
| 119 | 5x |
if (!"ylab" %in% names(dots)) |
| 120 | 5x |
ylab = gettext("Number", domain = "R-stacomiR")
|
| 121 |
else |
|
| 122 | ! |
ylab = dots[["ylab"]] |
| 123 | 5x |
if (!"cex.main" %in% names(dots)) |
| 124 | 5x |
cex.main = 1 |
| 125 |
else |
|
| 126 | ! |
cex.main = dots[["cex.main"]] |
| 127 | 5x |
if (!"font.main" %in% names(dots)) |
| 128 | 5x |
font.main = 1 |
| 129 |
else |
|
| 130 | ! |
font.main = dots[["font.main"]] |
| 131 | 5x |
if (!"type" %in% names(dots)) |
| 132 | 5x |
type = "h" |
| 133 |
else |
|
| 134 | ! |
type = dots[["type"]] |
| 135 | 5x |
if (!"xlim" %in% names(dots)) |
| 136 | 5x |
xlim = c(debut, fin) |
| 137 |
else |
|
| 138 | ! |
xlim = c(debut, fin)#dots[["xlim"]] # currently this argument is ignored |
| 139 | 5x |
if (!"ylim" %in% names(dots)) |
| 140 | 5x |
ylim = NULL |
| 141 |
else |
|
| 142 | ! |
ylim = dots[["ylim"]] |
| 143 | 5x |
if (!"cex" %in% names(dots)) |
| 144 | 5x |
cex = 1 |
| 145 |
else |
|
| 146 | ! |
cex = dots[["cex"]] |
| 147 | 5x |
if (!"lty" %in% names(dots)) |
| 148 | 5x |
lty = 1 |
| 149 |
else |
|
| 150 | ! |
lty = dots[["lty"]] |
| 151 | 5x |
if (!"pch" %in% names(dots)) |
| 152 | 5x |
pch = 16 |
| 153 |
else |
|
| 154 | ! |
pch = dots[["pch"]] |
| 155 | 5x |
if (!"bty" %in% names(dots)) |
| 156 | 4x |
bty = "l" |
| 157 |
else |
|
| 158 | 1x |
bty = dots[["bty"]] |
| 159 | 5x |
matplot( |
| 160 | 5x |
time.sequence, |
| 161 | 5x |
cbind( |
| 162 | 5x |
tableau$MESURE + tableau$CALCULE + tableau$EXPERT + tableau$PONCTUEL, |
| 163 | 5x |
tableau$MESURE + tableau$CALCULE + tableau$EXPERT, |
| 164 | 5x |
tableau$MESURE + tableau$CALCULE, |
| 165 | 5x |
tableau$MESURE |
| 166 |
), |
|
| 167 | 5x |
col = mypalette[c("ponctuel", "expert", "calcule", "mesure")],
|
| 168 | 5x |
type = type, |
| 169 | 5x |
pch = pch, |
| 170 | 5x |
lty = lty, |
| 171 | 5x |
xaxt = "n", |
| 172 | 5x |
bty = bty, |
| 173 | 5x |
ylab = ylab, |
| 174 | 5x |
xlab = NULL, |
| 175 | 5x |
main = main, |
| 176 | 5x |
xlim = c(debut, fin), |
| 177 | 5x |
cex.main = cex.main, |
| 178 | 5x |
font.main = font.main |
| 179 |
) |
|
| 180 | 5x |
if (report_mig@timestep@step_duration == "86400") {
|
| 181 |
# pas de temps journalier |
|
| 182 | 5x |
index = as.vector(x[jmois == 15]) |
| 183 | 5x |
axis( |
| 184 | 5x |
side = 1, |
| 185 | 5x |
at = index, |
| 186 | 5x |
tick = TRUE, |
| 187 | 5x |
labels = mois |
| 188 |
) |
|
| 189 |
#axis(side=1,at=as.vector(x[jmois==1]),tick=TRUE,labels=FALSE) |
|
| 190 |
|
|
| 191 |
} else {
|
|
| 192 | ! |
axis(side = 1) |
| 193 |
} |
|
| 194 | 5x |
mtext( |
| 195 | 5x |
text = gettextf("Sum of numbers =%s",
|
| 196 | 5x |
round( |
| 197 | 5x |
sum( |
| 198 | 5x |
tableau$MESURE, |
| 199 | 5x |
tableau$CALCULE, |
| 200 | 5x |
tableau$EXPERT, |
| 201 | 5x |
tableau$PONCTUEL, |
| 202 | 5x |
na.rm = TRUE |
| 203 |
) |
|
| 204 | 5x |
), domain = "R-stacomiR"), |
| 205 | 5x |
side = 3, |
| 206 | 5x |
col = mypalette["expert"], |
| 207 | 5x |
cex = 0.8 |
| 208 |
) |
|
| 209 |
|
|
| 210 | 5x |
legend( |
| 211 | 5x |
x = 0, |
| 212 | 5x |
y = max( |
| 213 | 5x |
tableau$MESURE, |
| 214 | 5x |
tableau$CALCULE, |
| 215 | 5x |
tableau$EXPERT, |
| 216 | 5x |
tableau$PONCTUEL, |
| 217 | 5x |
na.rm = TRUE |
| 218 |
), |
|
| 219 | 5x |
legend = gettext("measured", "calculated", "expert", "direct", domain =
|
| 220 | 5x |
"R-stacomiR"), |
| 221 | 5x |
pch = c(16), |
| 222 | 5x |
col = mypalette[c("mesure", "calcule", "expert", "ponctuel")]
|
| 223 |
) |
|
| 224 | 5x |
report_ope <- get("report_ope", envir = envir_stacomi)
|
| 225 | 5x |
t_operation_ope <- |
| 226 | 5x |
report_ope@data[report_ope@data$ope_dic_identifiant == dc, ] |
| 227 | 5x |
dif = difftime(t_operation_ope$ope_date_fin, |
| 228 | 5x |
t_operation_ope$ope_date_debut, |
| 229 | 5x |
units = "days") |
| 230 |
|
|
| 231 | 5x |
if (!silent) {
|
| 232 | 1x |
funout(ngettext( |
| 233 | 1x |
nrow(t_operation_ope), |
| 234 | 1x |
"%d operation \n", |
| 235 | 1x |
"%d operations \n", |
| 236 | 1x |
domain = "R-stacomiR" |
| 237 |
)) |
|
| 238 | 1x |
funout(gettextf("average trapping time = %s days\n", round(mean(
|
| 239 | 1x |
as.numeric(dif) |
| 240 | 1x |
), 2), domain = "R-stacomiR")) |
| 241 | 1x |
funout(gettextf("maximum term = %s", round(max(
|
| 242 | 1x |
as.numeric(dif) |
| 243 | 1x |
), 2), domain = "R-stacomiR")) |
| 244 | 1x |
funout(gettextf("minimum term = %s", round(min(
|
| 245 | 1x |
as.numeric(dif) |
| 246 | 1x |
), 2), domain = "R-stacomiR")) |
| 247 |
} |
|
| 248 |
|
|
| 249 |
|
|
| 250 | 5x |
df <- report_mig@dc@data$df[report_mig@dc@data$dc == dc] |
| 251 | 5x |
report_df <- get("report_df", envir = envir_stacomi)
|
| 252 | 5x |
report_dc <- get("report_dc", envir = envir_stacomi)
|
| 253 | 5x |
report_df@data <- |
| 254 | 5x |
report_df@data[report_df@data$per_dis_identifiant == df, ] |
| 255 | 5x |
report_dc@data <- |
| 256 | 5x |
report_dc@data[report_dc@data$per_dis_identifiant == dc, ] |
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 | 5x |
graphdate <- function(vectordate) {
|
| 261 | 96x |
attributes(vectordate) <- NULL |
| 262 | 96x |
vectordate = unclass(vectordate) |
| 263 | 96x |
vectordate[vectordate < debut] <- debut |
| 264 | 96x |
vectordate[vectordate > fin] <- fin |
| 265 | 96x |
return(vectordate) |
| 266 |
} |
|
| 267 |
|
|
| 268 |
|
|
| 269 |
################################### |
|
| 270 |
# creation d'un graphique vide (2) |
|
| 271 |
################################### |
|
| 272 | 5x |
graphics::par("mar" = c(0, 4, 0, 2) + 0.1)
|
| 273 | 5x |
plot( |
| 274 | 5x |
as.POSIXct(time.sequence), |
| 275 | 5x |
seq(0, 3, length.out = nrow(tableau)), |
| 276 | 5x |
xlim = xlim, |
| 277 | 5x |
type = "n", |
| 278 | 5x |
xlab = "", |
| 279 | 5x |
xaxt = "n", |
| 280 | 5x |
yaxt = "n", |
| 281 | 5x |
ylab = gettext("Fishway", domain = "R-stacomiR"),
|
| 282 | 5x |
bty = "n", |
| 283 | 5x |
cex = cex + 0.2 |
| 284 |
) |
|
| 285 |
|
|
| 286 |
################################### |
|
| 287 |
# time for DF (fishway) operation |
|
| 288 |
################################### |
|
| 289 |
|
|
| 290 | 5x |
if (dim(report_df@data)[1] == 0) {
|
| 291 | 1x |
rect( |
| 292 | 1x |
xleft = debut, |
| 293 | 1x |
ybottom = 2.1, |
| 294 | 1x |
xright = fin, |
| 295 | 1x |
ytop = 3, |
| 296 | 1x |
col = "grey", |
| 297 | 1x |
border = NA, |
| 298 | 1x |
lwd = 1 |
| 299 |
) |
|
| 300 | 1x |
rect( |
| 301 | 1x |
xleft = debut, |
| 302 | 1x |
ybottom = 1.1, |
| 303 | 1x |
xright = fin, |
| 304 | 1x |
ytop = 2, |
| 305 | 1x |
col = "grey40", |
| 306 | 1x |
border = NA, |
| 307 | 1x |
lwd = 1 |
| 308 |
) |
|
| 309 | 1x |
legend( |
| 310 | 1x |
x = "bottom", |
| 311 | 1x |
legend = gettext("Unknown working", "Unknow operation type", domain =
|
| 312 | 1x |
"R-stacomiR"), |
| 313 | 1x |
pch = c(16, 16), |
| 314 | 1x |
col = c("grey", "grey40"),
|
| 315 | 1x |
horiz = TRUE, |
| 316 | 1x |
bty = "n" |
| 317 |
) |
|
| 318 |
|
|
| 319 |
|
|
| 320 |
} else {
|
|
| 321 |
# si il sort quelque chose |
|
| 322 | 4x |
if (sum(report_df@data$per_etat_fonctionnement == 1) > 0) {
|
| 323 | 4x |
rect( |
| 324 | 4x |
xleft = graphdate(as.POSIXct(report_df@data$per_date_debut[report_df@data$per_etat_fonctionnement == |
| 325 | 4x |
1])), |
| 326 | 4x |
ybottom = 2.1, |
| 327 | 4x |
xright = graphdate(as.POSIXct(report_df@data$per_date_fin[report_df@data$per_etat_fonctionnement == |
| 328 | 4x |
1])), |
| 329 | 4x |
ytop = 3, |
| 330 | 4x |
col = mypalette["working"], |
| 331 | 4x |
border = NA, |
| 332 | 4x |
lwd = 1 |
| 333 |
) |
|
| 334 |
} |
|
| 335 | 4x |
if (sum(report_df@data$per_etat_fonctionnement == 0) > 0) {
|
| 336 | 4x |
rect( |
| 337 | 4x |
xleft = graphdate(as.POSIXct(report_df@data$per_date_debut[report_df@data$per_etat_fonctionnement == |
| 338 | 4x |
0])), |
| 339 | 4x |
ybottom = 2.1, |
| 340 | 4x |
xright = graphdate(as.POSIXct(report_df@data$per_date_fin[report_df@data$per_etat_fonctionnement == |
| 341 | 4x |
0])), |
| 342 | 4x |
ytop = 3, |
| 343 | 4x |
col = mypalette["stopped"], |
| 344 | 4x |
border = NA, |
| 345 | 4x |
lwd = 1 |
| 346 |
) |
|
| 347 |
} |
|
| 348 |
#creation d'une liste par categorie d'arret contenant vecteurs dates |
|
| 349 | 4x |
listeperiode <- |
| 350 | 4x |
fun_table_per_dis( |
| 351 | 4x |
typeperiode = report_df@data$per_tar_code, |
| 352 | 4x |
tempsdebut = report_df@data$per_date_debut, |
| 353 | 4x |
tempsfin = report_df@data$per_date_fin, |
| 354 | 4x |
libelle = report_df@data$libelle, |
| 355 | 4x |
color= mypalette[report_df@data$libelle], |
| 356 | 4x |
date = FALSE |
| 357 |
) |
|
| 358 | 4x |
nomperiode <- vector() |
| 359 | 4x |
color_periodes <- |
| 360 | 4x |
vector() # a vector of colors, one per period type in listeperiode |
| 361 | 4x |
for (j in 1:length(listeperiode)) {
|
| 362 |
#recuperation du vecteur de noms (dans l'ordre) e partir de la liste |
|
| 363 | 11x |
nomperiode[j] <- substr(listeperiode[[j]]$nom, 1, 17) |
| 364 |
#ecriture pour chaque type de periode |
|
| 365 | 11x |
color_periode = listeperiode[[j]]$color |
| 366 | 11x |
rect( |
| 367 | 11x |
xleft = graphdate(listeperiode[[j]]$debut), |
| 368 | 11x |
ybottom = 1.1, |
| 369 | 11x |
xright = graphdate(listeperiode[[j]]$fin), |
| 370 | 11x |
ytop = 2, |
| 371 | 11x |
col = color_periode, |
| 372 | 11x |
border = NA, |
| 373 | 11x |
lwd = 1 |
| 374 |
) |
|
| 375 | 11x |
color_periodes <- c(color_periodes, color_periode) |
| 376 |
} |
|
| 377 |
|
|
| 378 | 4x |
legend ( |
| 379 | 4x |
x = debut, |
| 380 | 4x |
y = 1.2, |
| 381 | 4x |
legend = c(gettext("stop", domain = "R-stacomiR"), nomperiode),
|
| 382 | 4x |
pch = c(15, 15), |
| 383 | 4x |
col = c(mypalette["working"], mypalette["stopped"], color_periodes), |
| 384 | 4x |
bty = "n", |
| 385 | 4x |
ncol = 7, |
| 386 | 4x |
text.width = (fin - debut) / 10 |
| 387 |
) |
|
| 388 |
} |
|
| 389 |
|
|
| 390 |
################################### |
|
| 391 |
# creation d'un graphique vide (3) |
|
| 392 |
################################### |
|
| 393 |
|
|
| 394 | 5x |
graphics::par("mar" = c(0, 4, 0, 2) + 0.1)
|
| 395 | 5x |
plot( |
| 396 | 5x |
as.POSIXct(time.sequence), |
| 397 | 5x |
seq(0, 3, length.out = nrow(tableau)), |
| 398 | 5x |
xlim = xlim, |
| 399 | 5x |
type = "n", |
| 400 | 5x |
xlab = "", |
| 401 | 5x |
xaxt = "n", |
| 402 | 5x |
yaxt = "n", |
| 403 | 5x |
ylab = gettext("CD", domain = "R-stacomiR"),
|
| 404 | 5x |
bty = "n", |
| 405 | 5x |
cex = cex + 0.2 |
| 406 |
) |
|
| 407 |
################################### |
|
| 408 |
# time for DC (counting device) operation |
|
| 409 |
################################### |
|
| 410 |
|
|
| 411 |
|
|
| 412 | 5x |
if (dim(report_dc@data)[1] == 0) {
|
| 413 | ! |
rect( |
| 414 | ! |
xleft = debut, |
| 415 | ! |
ybottom = 2.1, |
| 416 | ! |
xright = fin, |
| 417 | ! |
ytop = 3, |
| 418 | ! |
col = "grey", |
| 419 | ! |
border = NA, |
| 420 | ! |
lwd = 1 |
| 421 |
) |
|
| 422 |
|
|
| 423 | ! |
rect( |
| 424 | ! |
xleft = debut, |
| 425 | ! |
ybottom = 1.1, |
| 426 | ! |
xright = fin, |
| 427 | ! |
ytop = 2, |
| 428 | ! |
col = "grey40", |
| 429 | ! |
border = NA, |
| 430 | ! |
lwd = 1 |
| 431 |
) |
|
| 432 | ! |
legend( |
| 433 | ! |
x = "bottom", |
| 434 | ! |
legend = gettext("Unknown working", "Unknow operation type", domain =
|
| 435 | ! |
"R-stacomiR"), |
| 436 | ! |
pch = c(16, 16), |
| 437 | ! |
col = c("grey", "grey40"),
|
| 438 |
#horiz=TRUE, |
|
| 439 | ! |
ncol = 5, |
| 440 | ! |
bty = "n" |
| 441 |
) |
|
| 442 |
|
|
| 443 |
|
|
| 444 |
} else {
|
|
| 445 | 5x |
if (sum(report_dc@data$per_etat_fonctionnement == 1) > 0) {
|
| 446 | 5x |
rect( |
| 447 | 5x |
xleft = graphdate(as.POSIXct(report_dc@data$per_date_debut[report_dc@data$per_etat_fonctionnement == |
| 448 | 5x |
1])), |
| 449 | 5x |
ybottom = 2.1, |
| 450 | 5x |
xright = graphdate(as.POSIXct(report_dc@data$per_date_fin[report_dc@data$per_etat_fonctionnement == |
| 451 | 5x |
1])), |
| 452 | 5x |
ytop = 3, |
| 453 | 5x |
col = mypalette["working"], |
| 454 | 5x |
border = NA, |
| 455 | 5x |
lwd = 1 |
| 456 |
) |
|
| 457 |
} |
|
| 458 | 5x |
if (sum(report_dc@data$per_etat_fonctionnement == 0) > 0) |
| 459 |
{
|
|
| 460 | 5x |
rect( |
| 461 | 5x |
xleft = graphdate(as.POSIXct(report_dc@data$per_date_debut[report_dc@data$per_etat_fonctionnement == |
| 462 | 5x |
0])), |
| 463 | 5x |
ybottom = 2.1, |
| 464 | 5x |
xright = graphdate(as.POSIXct(report_dc@data$per_date_fin[report_dc@data$per_etat_fonctionnement == |
| 465 | 5x |
0])), |
| 466 | 5x |
ytop = 3, |
| 467 | 5x |
col = mypalette["stopped"], |
| 468 | 5x |
border = NA, |
| 469 | 5x |
lwd = 1 |
| 470 |
) |
|
| 471 |
} |
|
| 472 | 5x |
listeperiode <- |
| 473 | 5x |
fun_table_per_dis( |
| 474 | 5x |
typeperiode = report_dc@data$per_tar_code, |
| 475 | 5x |
tempsdebut = report_dc@data$per_date_debut, |
| 476 | 5x |
tempsfin = report_dc@data$per_date_fin, |
| 477 | 5x |
libelle = report_dc@data$libelle, |
| 478 | 5x |
color= mypalette[report_df@data$libelle], |
| 479 | 5x |
date = FALSE |
| 480 |
) |
|
| 481 | 5x |
nomperiode <- vector() |
| 482 | 5x |
color_periodes <- vector() |
| 483 | 5x |
for (j in 1:length(listeperiode)) {
|
| 484 | 14x |
nomperiode[j] <- substr(listeperiode[[j]]$nom, 1, 17) |
| 485 | 14x |
color_periode <- listeperiode[[j]]$color |
| 486 | 14x |
rect( |
| 487 | 14x |
xleft = graphdate(listeperiode[[j]]$debut), |
| 488 | 14x |
ybottom = 1.1, |
| 489 | 14x |
xright = graphdate(listeperiode[[j]]$fin), |
| 490 | 14x |
ytop = 2, |
| 491 | 14x |
col = color_periode, |
| 492 | 14x |
border = NA, |
| 493 | 14x |
lwd = 1 |
| 494 |
) |
|
| 495 |
} |
|
| 496 |
|
|
| 497 | 5x |
legend ( |
| 498 | 5x |
x = debut, |
| 499 | 5x |
y = 1.2, |
| 500 | 5x |
legend = gettext("working", "stopped", nomperiode, domain = "R-stacomiR"),
|
| 501 | 5x |
pch = c(15, 15), |
| 502 | 5x |
col = c(mypalette["working"], mypalette["stopped"], color_periodes), |
| 503 | 5x |
bty = "n", |
| 504 | 5x |
ncol = length(listeperiode) + 2, |
| 505 | 5x |
text.width = (fin - debut) / 10 |
| 506 |
) |
|
| 507 |
} |
|
| 508 |
|
|
| 509 |
################################### |
|
| 510 |
# creation d'un graphique vide (4=op) |
|
| 511 |
################################### |
|
| 512 |
|
|
| 513 |
|
|
| 514 | 5x |
graphics::par("mar" = c(0, 4, 0, 2) + 0.1)
|
| 515 | 5x |
plot( |
| 516 | 5x |
as.POSIXct(time.sequence), |
| 517 | 5x |
seq(0, 1, length.out = nrow(tableau)), |
| 518 | 5x |
xlim = xlim, |
| 519 | 5x |
type = "n", |
| 520 | 5x |
xlab = "", |
| 521 | 5x |
xaxt = "n", |
| 522 | 5x |
yaxt = "n", |
| 523 | 5x |
ylab = gettext("Op", domain = "R-stacomiR"),
|
| 524 | 5x |
bty = "n", |
| 525 | 5x |
cex = cex + 0.2 |
| 526 |
) |
|
| 527 |
################################### |
|
| 528 |
# operations |
|
| 529 |
################################### |
|
| 530 |
|
|
| 531 | 5x |
rect( |
| 532 | 5x |
xleft = graphdate(as.POSIXct(t_operation_ope$ope_date_debut)), |
| 533 | 5x |
ybottom = 0, |
| 534 | 5x |
xright = graphdate(as.POSIXct(t_operation_ope$ope_date_fin)), |
| 535 | 5x |
ytop = 1, |
| 536 | 5x |
col = color_ope, |
| 537 | 5x |
border = NA, |
| 538 | 5x |
lwd = 1 |
| 539 |
) |
|
| 540 |
|
|
| 541 |
|
|
| 542 |
################################### |
|
| 543 |
# Graph mensuel |
|
| 544 |
#################################### |
|
| 545 | 5x |
graphics::par("mar" = c(4, 4, 1, 2) + 0.1)
|
| 546 | 5x |
tableau$mois = factor(months(tableau$debut_pas, abbreviate = TRUE), |
| 547 | 5x |
levels = unique(months(tableau$debut_pas, abbreviate = TRUE))) |
| 548 | 5x |
tableaum <- |
| 549 | 5x |
reshape2::melt( |
| 550 | 5x |
data = tableau[, c("MESURE", "CALCULE", "EXPERT", "PONCTUEL", "mois")],
|
| 551 | 5x |
id.vars = c("mois"),
|
| 552 | 5x |
measure.vars = c("MESURE", "CALCULE", "EXPERT", "PONCTUEL"),
|
| 553 | 5x |
variable.name = "type", |
| 554 | 5x |
value.name = "number" |
| 555 |
) |
|
| 556 | 5x |
levels(tableaum$type) <- |
| 557 | 5x |
gettext("measured", "calculated", "expert", "direct", domain = "R-stacomiR")
|
| 558 | 5x |
superpose.polygon <- lattice::trellis.par.get("plot.polygon")
|
| 559 | 5x |
superpose.polygon$col = mypalette[c("mesure", "calcule", "expert", "ponctuel")]
|
| 560 | 5x |
superpose.polygon$border = rep("transparent", 6)
|
| 561 | 5x |
lattice::trellis.par.set("superpose.polygon", superpose.polygon)
|
| 562 | 5x |
fontsize <- lattice::trellis.par.get("fontsize")
|
| 563 | 5x |
fontsize$text = 10 |
| 564 | 5x |
lattice::trellis.par.set("fontsize", fontsize)
|
| 565 | 5x |
par.main.text <- lattice::trellis.par.get("par.main.text")
|
| 566 | 5x |
par.main.text$cex = 1 |
| 567 | 5x |
par.main.text$font = 1 |
| 568 | 5x |
lattice::trellis.par.set("par.main.text", par.main.text)
|
| 569 |
# lattice::show.settings() |
|
| 570 |
|
|
| 571 | 5x |
par.ylab.text <- lattice::trellis.par.get("par.ylab.text")
|
| 572 | 5x |
par.ylab.text$cex = 0.8 |
| 573 | 5x |
lattice::trellis.par.set("par.ylab.text", par.ylab.text)
|
| 574 | 5x |
par.xlab.text <- lattice::trellis.par.get("par.xlab.text")
|
| 575 | 5x |
par.xlab.text$cex = 0.8 |
| 576 | 5x |
lattice::trellis.par.set("par.xlab.text", par.xlab.text)
|
| 577 |
|
|
| 578 | 5x |
bar <- lattice::barchart( |
| 579 | 5x |
number / 1000 ~ mois, |
| 580 | 5x |
groups = type, |
| 581 | 5x |
xlab = gettext("Month", domain = "R-stacomiR"),
|
| 582 | 5x |
ylab = gettext("Number (x1000)", domain = "R-stacomiR"),
|
| 583 | 5x |
data = tableaum, |
| 584 | 5x |
allow.multiple = FALSE, |
| 585 | 5x |
strip = FALSE, |
| 586 | 5x |
stack = TRUE, |
| 587 | 5x |
origin = 0 |
| 588 |
) |
|
| 589 | 5x |
print(bar, position = c(0, 0, 1, .25), newpage = FALSE) |
| 590 |
|
|
| 591 | 5x |
return(invisible(NULL)) |
| 592 |
} |
| 1 |
#' Class 'ref_taxa' |
|
| 2 |
#' |
|
| 3 |
#' Loading and selection of fish species. This class is a referential class, and it is |
|
| 4 |
#' integrated into refreport objects. |
|
| 5 |
#' |
|
| 6 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 7 |
#' \code{new('ref_taxa', ...)}.
|
|
| 8 |
#' @slot data A \code{'data.frame'} of species available in the database
|
|
| 9 |
#' @slot taxa_selected Contains the code \code{'tax_code'} of the taxa selected by choice_c() method
|
|
| 10 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 11 |
#' @family referential objects |
|
| 12 |
setClass(Class = "ref_taxa", representation = representation(data = "data.frame",taxa_selected = "character")) |
|
| 13 | ||
| 14 | ||
| 15 |
#' Loading method for ref_taxa referential objects |
|
| 16 |
#' |
|
| 17 |
#' @return An S4 object of class ref_taxa |
|
| 18 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 19 |
#' @param object An object of class \link{ref_taxa-class}
|
|
| 20 |
#' @return An S4 object of class \link{ref_taxa-class} with all taxa loaded from the database
|
|
| 21 |
#' @examples \dontrun{
|
|
| 22 |
#' object=new('ref_taxa')
|
|
| 23 |
#' charge(object)} |
|
| 24 |
setMethod("charge", signature = signature("ref_taxa"), definition = function(object) {
|
|
| 25 | 2x |
req = new("RequeteDB")
|
| 26 | 2x |
req@sql = "SELECT tax_code, tax_nom_latin, tax_nom_commun, tax_ntx_code, tax_tax_code FROM ref.tr_taxon_tax ORDER BY tax_rang ASC ;" |
| 27 | 2x |
req <- stacomirtools::query(req) |
| 28 | 2x |
object@data <- req@query |
| 29 | 2x |
return(object) |
| 30 |
}) |
|
| 31 | ||
| 32 |
#' Loading method for ref_taxa referential objects searching only taxa existing for a DC |
|
| 33 |
#' @param object An object of class \link{ref_taxa-class}
|
|
| 34 |
#' @param dc_selected A counting device selected, only taxa attached to this dc are selected |
|
| 35 |
#' @return An S4 object of class \link{ref_taxa-class} with all taxa present on a DC (counting device)
|
|
| 36 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 37 |
#' @examples \dontrun{
|
|
| 38 |
#' dc_selected=6 |
|
| 39 |
#' object=new('ref_taxa')
|
|
| 40 |
#' charge_with_filter(object,dc_selected=dc_selected)} |
|
| 41 |
setMethod("charge_with_filter", signature = signature("ref_taxa"), definition = function(object,
|
|
| 42 |
dc_selected) {
|
|
| 43 | 61x |
requete = new("RequeteDBwhere")
|
| 44 | 61x |
requete@select = paste("SELECT DISTINCT ON (tax_rang) tax_code, tax_nom_latin, tax_nom_commun, tax_ntx_code, tax_tax_code",
|
| 45 | 61x |
" FROM ", get_schema(), "tg_dispositif_dis", " JOIN ", |
| 46 | 61x |
get_schema(), "t_dispositifcomptage_dic on dis_identifiant=dic_dis_identifiant", |
| 47 | 61x |
" JOIN ", get_schema(), "t_operation_ope on ope_dic_identifiant=dic_dis_identifiant", |
| 48 | 61x |
" JOIN ", get_schema(), "t_lot_lot on lot_ope_identifiant=ope_identifiant", |
| 49 | 61x |
" JOIN ref.tr_taxon_tax on lot_tax_code=tax_code", sep = "") |
| 50 | 61x |
requete@where = paste("where dis_identifiant in", vector_to_listsql(dc_selected))
|
| 51 | 61x |
requete@order_by = "ORDER BY tax_rang ASC" |
| 52 | 61x |
requete <- stacomirtools::query(requete) |
| 53 | 61x |
object@data <- requete@query |
| 54 | 61x |
return(object) |
| 55 |
}) |
|
| 56 | ||
| 57 | ||
| 58 |
#' choice_c method for ref_taxa |
|
| 59 |
#' |
|
| 60 |
#' the choice_cc method is intended to have the same behaviour as choice (which creates a |
|
| 61 |
#' widget in the graphical interface) but from the command line. The values passed to the choice_c method |
|
| 62 |
#' for taxa can be either numeric (2038 = Anguilla anguilla) or character. |
|
| 63 |
#' @param object An object from the class ref_taxa |
|
| 64 |
#' @param taxa The vector of taxa, can be either code (numeric) or latin name |
|
| 65 |
#' @return An S4 object of class \link{ref_taxa-class} with data filtered according to the taxa
|
|
| 66 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 67 |
#' @examples |
|
| 68 |
#' \dontrun{
|
|
| 69 |
#' object=new('ref_taxa')
|
|
| 70 |
#' object<-charge(object) |
|
| 71 |
#' objectreport=new('report_mig_mult')
|
|
| 72 |
#' choice_c(object=object,'Anguilla anguilla') |
|
| 73 |
#' } |
|
| 74 |
setMethod("choice_c", signature = signature("ref_taxa"), definition = function(object,
|
|
| 75 |
taxa) {
|
|
| 76 | 58x |
if (is.null(taxa)) {
|
| 77 | ! |
funout(gettext("No value for argument taxa\n", domain = "R-stacomiR"), arret = TRUE)
|
| 78 | 58x |
} else if (inherits(taxa, "character") & suppressWarnings(all(is.na(as.numeric(taxa))))) {
|
| 79 |
# taxa is 'Anguilla anguilla' |
|
| 80 | 36x |
libellemanquants <- taxa[!taxa %in% object@data$tax_nom_latin] |
| 81 | 36x |
if (length(libellemanquants) > 0) |
| 82 | 1x |
warning(gettextf("Taxa not present :\n %s", stringr::str_c(libellemanquants,
|
| 83 | 1x |
collapse = ", "), domain = "R-stacomiR")) |
| 84 | 36x |
object@taxa_selected <- object@data[object@data$tax_nom_latin %in% taxa,"tax_code"] |
| 85 | 58x |
} else if (inherits(taxa, "numeric")){
|
| 86 |
# taxa is 2038 |
|
| 87 | 13x |
codemanquants <- taxa[!as.character(taxa) %in% object@data$tax_code] |
| 88 | 13x |
if (length(codemanquants) > 0) |
| 89 | 1x |
warning(gettextf("Taxa not present :\n %s", stringr::str_c(codemanquants,
|
| 90 | 1x |
collapse = ", "), domain = "R-stacomiR")) |
| 91 | 13x |
object@taxa_selected <- object@data[object@data$tax_code %in% as.character(taxa),"tax_code"] |
| 92 | 58x |
} else if (inherits(taxa, "character") & !suppressWarnings(all(is.na(as.numeric(taxa))))){
|
| 93 |
# taxa is "2038" |
|
| 94 | 9x |
codemanquants <- taxa[!taxa %in% object@data$tax_code] |
| 95 | 9x |
if (length(codemanquants) > 0) |
| 96 | 9x |
warning(gettextf("Taxa not present :\n %s", stringr::str_c(codemanquants,
|
| 97 | 9x |
collapse = ", "), domain = "R-stacomiR")) |
| 98 | 9x |
object@taxa_selected <- object@data[object@data$tax_code %in% taxa, "tax_code"] |
| 99 |
} |
|
| 100 | 58x |
if (nrow(object@data) == 0) {
|
| 101 | ! |
funout(gettext("Stop there is no line in the taxa table (problem with the DB link ?)\n",
|
| 102 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 103 |
} |
|
| 104 | 58x |
assign("ref_taxa", object, envir = envir_stacomi)
|
| 105 | 58x |
return(object) |
| 106 |
}) |
| 1 |
#' Class "report_sea_age" |
|
| 2 |
#' |
|
| 3 |
#' the report_sea_age class is used to dispatch adult salmons to age class according to |
|
| 4 |
#' their size and to basin dependent limits set by the user. Once checked with graphs and summary |
|
| 5 |
#' statistics, the results are to be written to the database. |
|
| 6 |
#' @include create_generic.R |
|
| 7 |
#' @include ref_textbox.R |
|
| 8 |
#' @include ref_dc.R |
|
| 9 |
#' @include ref_taxa.R |
|
| 10 |
#' @include ref_stage.R |
|
| 11 |
#' @include ref_horodate.R |
|
| 12 |
#' @include ref_par.R |
|
| 13 |
#' @note This class is displayed by interface_report_sea_age |
|
| 14 |
#' @slot data A data frame with data generated from the database |
|
| 15 |
#' @slot calcdata A list of dc with processed data. This lists consists of two elements |
|
| 16 |
#' \itemize{
|
|
| 17 |
#' \item (1) data A dataset with age set to be used by the plot and summary methods |
|
| 18 |
#' \item (2) tj_caracteristitiquelot_car A dataset to import into the database |
|
| 19 |
#' } |
|
| 20 |
#' @slot dc Object of class \link{ref_dc-class}: the control devices
|
|
| 21 |
#' @slot taxa Object of class \link{ref_taxa-class}: the species
|
|
| 22 |
#' @slot stage Object of class \link{ref_stage-class} : the stages of the fish
|
|
| 23 |
#' @slot par Object of class \link{ref_par-class}: the parameters used
|
|
| 24 |
#' @slot horodatedebut An object of class \code{ref_horodate-class}
|
|
| 25 |
#' @slot horodatefin An object of class \code{ref_horodate-class}
|
|
| 26 |
#' @slot limit1hm The size limit, in mm between 1 sea winter fishes and 2 sea winter fishes |
|
| 27 |
#' @slot limit2hm The size limit, in mm between 2 sea winter fishes and 3 sea winter fishes |
|
| 28 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 29 |
#' \code{new("report_sea_age", ...)}
|
|
| 30 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 31 |
#' @family report Objects |
|
| 32 |
#' @keywords classes |
|
| 33 |
#' @example inst/examples/report_sea_age-example.R |
|
| 34 |
#' @aliases report_sea_age |
|
| 35 |
#' @export |
|
| 36 |
setClass( |
|
| 37 |
Class = "report_sea_age", |
|
| 38 |
representation = representation( |
|
| 39 |
data = "data.frame", |
|
| 40 |
calcdata = "list", |
|
| 41 |
dc = "ref_dc", |
|
| 42 |
taxa = "ref_taxa", |
|
| 43 |
stage = "ref_stage", |
|
| 44 |
par = "ref_par", |
|
| 45 |
horodatedebut = "ref_horodate", |
|
| 46 |
horodatefin = "ref_horodate", |
|
| 47 |
limit1hm = "ref_textbox", |
|
| 48 |
limit2hm = "ref_textbox" |
|
| 49 |
), |
|
| 50 |
prototype = prototype( |
|
| 51 |
data = data.frame(), |
|
| 52 |
calcdata = list(), |
|
| 53 |
dc = new("ref_dc"),
|
|
| 54 |
taxa = new("ref_taxa"),
|
|
| 55 |
stage = new("ref_stage"),
|
|
| 56 |
par = new("ref_par"),
|
|
| 57 |
horodatedebut = new("ref_horodate"),
|
|
| 58 |
horodatefin = new("ref_horodate"),
|
|
| 59 |
limit1hm = new("ref_textbox"),
|
|
| 60 |
limit2hm = new("ref_textbox")
|
|
| 61 |
) |
|
| 62 |
) |
|
| 63 |
setValidity("report_sea_age", function(object)
|
|
| 64 |
{
|
|
| 65 |
rep1 = object@taxa@taxa_selected[1] == '2220' |
|
| 66 |
label1 <- |
|
| 67 |
'report_sea_age should only be for salmon (tax_code=2220)' |
|
| 68 |
rep2 = all(object@stage@stage_selected %in% c('5', '11', 'BEC', 'BER', 'IND'))
|
|
| 69 |
label2 <- |
|
| 70 |
'Only stages 5,11,BEC,BER,IND should be used in report_sea_age' |
|
| 71 |
return(ifelse(rep1 & |
|
| 72 |
rep2 , TRUE , c(label1, label2)[!c(rep1, rep2)])) |
|
| 73 |
}) |
|
| 74 |
#' connect method for report_sea_age |
|
| 75 |
#' |
|
| 76 |
#' @param object An object of class \link{report_sea_age-class}
|
|
| 77 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 78 |
#' @return An object of class \link{report_sea_age-class} with slot data \code{@data} filled
|
|
| 79 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 80 |
#' @aliases connect.report_sea_age |
|
| 81 |
setMethod( |
|
| 82 |
"connect", |
|
| 83 |
signature = signature("report_sea_age"),
|
|
| 84 |
definition = function(object, silent = FALSE) {
|
|
| 85 | 4x |
requete <- new("RequeteDBwheredate")
|
| 86 | 4x |
requete@select = paste("SELECT * FROM ",
|
| 87 | 4x |
get_schema(), |
| 88 | 4x |
"vue_lot_ope_car", |
| 89 | 4x |
sep = "") |
| 90 | 4x |
requete@colonnedebut = "ope_date_debut" |
| 91 | 4x |
requete@colonnefin = "ope_date_fin" |
| 92 | 4x |
requete@datedebut <- object@horodatedebut@horodate |
| 93 | 4x |
requete@datefin <- object@horodatefin@horodate |
| 94 | 4x |
requete@order_by = "ORDER BY ope_date_debut" |
| 95 | 4x |
requete@and = paste( |
| 96 | 4x |
" AND ope_dic_identifiant in ", |
| 97 | 4x |
vector_to_listsql(object@dc@dc_selected), |
| 98 | 4x |
" AND lot_tax_code in ", |
| 99 | 4x |
vector_to_listsql(object@taxa@taxa_selected), |
| 100 | 4x |
" AND lot_std_code in ", |
| 101 | 4x |
vector_to_listsql(object@stage@stage_selected), |
| 102 | 4x |
" AND car_par_code in ", |
| 103 | 4x |
vector_to_listsql(object@par@par_selected), |
| 104 | 4x |
sep = "" |
| 105 |
) |
|
| 106 | 4x |
requete <- stacomirtools::query(requete) |
| 107 | 4x |
object@data <- requete@query |
| 108 | 4x |
if (!silent) |
| 109 | 4x |
funout(gettext("Data loaded", domain = "R-stacomiR"))
|
| 110 | 4x |
return(object) |
| 111 |
} |
|
| 112 |
) |
|
| 113 | ||
| 114 | ||
| 115 |
#' Loads data and check that all choices in the graphical interface have been made. |
|
| 116 |
#' |
|
| 117 |
#' It is not necessary to run this method if the choice_c method has been run. |
|
| 118 |
#' This method verifies that boxes have been clicked in the user interface and gets the objects pasted in |
|
| 119 |
#' envir_stacomi |
|
| 120 |
#' @param object An object of class \link{report_sea_age-class}
|
|
| 121 |
#' @param h a handler |
|
| 122 |
#' @return An object of class \link{report_sea_age-class} with slots filled with user choice
|
|
| 123 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 124 |
#' @return An object of class \link{report_sea_age-class} with slots filled from values assigned in \code{envir_stacomi} environment
|
|
| 125 |
#' @aliases charge.report_sea_age |
|
| 126 |
#' @keywords internal |
|
| 127 |
setMethod( |
|
| 128 |
"charge", |
|
| 129 |
signature = signature("report_sea_age"),
|
|
| 130 |
definition = function(object, h) {
|
|
| 131 | 1x |
if (exists("ref_dc", envir_stacomi)) {
|
| 132 | 1x |
object@dc <- get("ref_dc", envir_stacomi)
|
| 133 |
} else {
|
|
| 134 | ! |
funout( |
| 135 | ! |
gettext( |
| 136 | ! |
"You need to choose a counting device, clic on validate\n", |
| 137 | ! |
domain = "R-stacomiR" |
| 138 |
), |
|
| 139 | ! |
arret = TRUE |
| 140 |
) |
|
| 141 |
} |
|
| 142 | 1x |
if (exists("ref_taxa", envir_stacomi)) {
|
| 143 | 1x |
object@taxa <- get("ref_taxa", envir_stacomi)
|
| 144 |
} else {
|
|
| 145 | ! |
funout( |
| 146 | ! |
gettext("You need to choose a taxa, clic on validate\n", domain = "R-stacomiR"),
|
| 147 | ! |
arret = TRUE |
| 148 |
) |
|
| 149 |
} |
|
| 150 | 1x |
if (exists("ref_stage", envir_stacomi)) {
|
| 151 | 1x |
object@stage <- get("ref_stage", envir_stacomi)
|
| 152 |
} else {
|
|
| 153 | ! |
funout( |
| 154 | ! |
gettext("You need to choose a stage, clic on validate\n", domain = "R-stacomiR"),
|
| 155 | ! |
arret = TRUE |
| 156 |
) |
|
| 157 |
} |
|
| 158 | 1x |
if (exists("ref_par", envir_stacomi)) {
|
| 159 | 1x |
object@par <- get("ref_par", envir_stacomi)
|
| 160 |
} else {
|
|
| 161 | ! |
funout( |
| 162 | ! |
gettext("You need to choose a parameter, clic on validate\n", domain = "R-stacomiR"),
|
| 163 | ! |
arret = TRUE |
| 164 |
) |
|
| 165 |
} |
|
| 166 |
# rem pas tres satisfaisant car ce nom est choisi dans l'interface |
|
| 167 | 1x |
if (exists("r_seaa_date_debut", envir_stacomi)) {
|
| 168 | 1x |
object@horodatedebut@horodate <- |
| 169 | 1x |
get("r_seaa_date_debut", envir_stacomi)
|
| 170 |
} else {
|
|
| 171 | ! |
funout(gettext("You need to choose the starting date\n", domain = "R-stacomiR"),
|
| 172 | ! |
arret = TRUE) |
| 173 |
} |
|
| 174 |
# rem id |
|
| 175 | 1x |
if (exists("r_seaa_date_fin", envir_stacomi)) {
|
| 176 | 1x |
object@horodatefin@horodate <- get("r_seaa_date_fin", envir_stacomi)
|
| 177 |
} else {
|
|
| 178 | ! |
funout(gettext("You need to choose the ending date\n", domain = "R-stacomiR"),
|
| 179 | ! |
arret = TRUE) |
| 180 |
} |
|
| 181 | 1x |
if (exists("limit1hm", envir_stacomi)) {
|
| 182 | 1x |
object@limit1hm <- get("limit1hm", envir_stacomi)
|
| 183 |
} else {
|
|
| 184 | ! |
funout(gettext("you need to choose a value for limit1hm", domain = "R-stacomiR"),
|
| 185 | ! |
arret = TRUE) |
| 186 |
} |
|
| 187 | 1x |
if (exists("limit2hm", envir_stacomi)) {
|
| 188 | 1x |
object@limit2hm <- get("limit2hm", envir_stacomi)
|
| 189 |
} else {
|
|
| 190 | ! |
funout(gettext("you need to choose a value for limit2hm", domain = "R-stacomiR"),
|
| 191 | ! |
arret = TRUE) |
| 192 |
} |
|
| 193 | 1x |
return(object) |
| 194 | ! |
validObject(object) |
| 195 | ! |
assign("r_seaa", object, envir_stacomi)
|
| 196 |
} |
|
| 197 |
) |
|
| 198 | ||
| 199 | ||
| 200 |
#' command line interface for report_sea_age class |
|
| 201 |
#' |
|
| 202 |
#' #' The choice_c method fills in the data slot for classes \link{ref_dc-class}, \link{ref_taxa-class}, \link{ref_stage-class}, \link{ref_par-class} and two slots of \link{ref_horodate-class} and then
|
|
| 203 |
#' uses the choice_c methods of these object to select the data. |
|
| 204 |
#' @param object An object of class \link{report_sea_age-class}
|
|
| 205 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 206 |
#' @param taxa '2220=Salmo salar', |
|
| 207 |
#' these should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 208 |
#' @param stage '5','11','BEC','BER','IND' |
|
| 209 |
#' @param par Parameters chosen for the report are measured body size (1786), measured fork length (1785),video size (C001) and number of year at sea (A124) |
|
| 210 |
#' @param horodatedebut The starting date as a character, formats like \code{\%Y-\%m-\%d} or \code{\%d-\%m-\%Y} can be used as input
|
|
| 211 |
#' @param horodatefin The finishing date of the report, for this class this will be used to calculate the number of daily steps. |
|
| 212 |
#' @param limit1hm Size limit of a salmon for an one sea winter fish |
|
| 213 |
#' @param limit2hm Size limit of a salmon for a two sea winter fish |
|
| 214 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 215 |
#' @return An object of class \link{report_sea_age-class}
|
|
| 216 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 217 |
#' @aliases choice_c.report_sea_age |
|
| 218 |
setMethod( |
|
| 219 |
"choice_c", |
|
| 220 |
signature = signature("report_sea_age"),
|
|
| 221 |
definition = function(object, |
|
| 222 |
dc, |
|
| 223 |
taxa = 2220, |
|
| 224 |
stage = c('5', '11', 'BEC', 'BER', 'IND'),
|
|
| 225 |
par = c('1786', '1785', 'C001', 'A124'),
|
|
| 226 |
horodatedebut, |
|
| 227 |
horodatefin, |
|
| 228 |
limit1hm, |
|
| 229 |
limit2hm, |
|
| 230 |
silent = FALSE) {
|
|
| 231 |
# code for debug using example |
|
| 232 |
#horodatedebut="2012-01-01";horodatefin="2013-12-31";dc=c(107,108,101); |
|
| 233 |
#taxa=2220; stage=c('5','11','BEC','BER','IND');par=c('1786','1785','C001');silent=FALSE
|
|
| 234 | 5x |
if (!(is.numeric(limit1hm) | |
| 235 | 5x |
is.integer(limit1hm))) |
| 236 | 5x |
funout(gettext("limit1hm should be numeric or integer", domain = "R-stacomiR"),
|
| 237 | 5x |
arret = TRUE) |
| 238 | 5x |
if (!(is.numeric(limit2hm) | |
| 239 | 5x |
is.integer(limit2hm))) |
| 240 | 5x |
funout(gettext("limit2hm should be numeric or integer", domain = "R-stacomiR"),
|
| 241 | 5x |
arret = TRUE) |
| 242 |
|
|
| 243 | 4x |
r_seaa <- object |
| 244 | 4x |
r_seaa@dc = charge(r_seaa@dc) |
| 245 |
# loads and verifies the dc |
|
| 246 |
# this will set dc_selected slot |
|
| 247 | 4x |
r_seaa@dc <- choice_c(object = r_seaa@dc, dc) |
| 248 |
# only taxa present in the report_mig are used |
|
| 249 | 4x |
r_seaa@taxa <- |
| 250 | 4x |
charge_with_filter(object = r_seaa@taxa, r_seaa@dc@dc_selected) |
| 251 | 4x |
r_seaa@taxa <- choice_c(r_seaa@taxa, taxa) |
| 252 | 4x |
r_seaa@stage <- |
| 253 | 4x |
charge_with_filter(object = r_seaa@stage, |
| 254 | 4x |
r_seaa@dc@dc_selected, |
| 255 | 4x |
r_seaa@taxa@taxa_selected) |
| 256 | 4x |
r_seaa@stage <- choice_c(r_seaa@stage, stage, silent = silent) |
| 257 | 4x |
r_seaa@par <- |
| 258 | 4x |
charge_with_filter( |
| 259 | 4x |
object = r_seaa@par, |
| 260 | 4x |
r_seaa@dc@dc_selected, |
| 261 | 4x |
r_seaa@taxa@taxa_selected, |
| 262 | 4x |
r_seaa@stage@stage_selected |
| 263 |
) |
|
| 264 | 4x |
r_seaa@par <- choice_c(r_seaa@par, par, silent = silent) |
| 265 | 4x |
r_seaa@horodatedebut <- choice_c( |
| 266 | 4x |
object = r_seaa@horodatedebut, |
| 267 | 4x |
nomassign = "r_seaa_date_debut", |
| 268 | 4x |
funoutlabel = gettext("Beginning date has been chosen\n", domain = "R-stacomiR"),
|
| 269 | 4x |
horodate = horodatedebut, |
| 270 | 4x |
silent = silent |
| 271 |
) |
|
| 272 | 4x |
r_seaa@horodatefin <- choice_c( |
| 273 | 4x |
r_seaa@horodatefin, |
| 274 | 4x |
nomassign = "r_seaa_date_fin", |
| 275 | 4x |
funoutlabel = gettext("Ending date has been chosen\n", domain = "R-stacomiR"),
|
| 276 | 4x |
horodate = horodatefin, |
| 277 | 4x |
silent = silent |
| 278 |
) |
|
| 279 | 4x |
r_seaa@limit1hm <- |
| 280 | 4x |
choice_c(r_seaa@limit1hm, as.character(limit1hm), "limit1hm") |
| 281 | 4x |
r_seaa@limit2hm <- |
| 282 | 4x |
choice_c(r_seaa@limit2hm, as.character(limit2hm), "limit2hm") |
| 283 | 4x |
validObject(r_seaa) |
| 284 | 4x |
return(r_seaa) |
| 285 |
} |
|
| 286 |
) |
|
| 287 | ||
| 288 |
#' Split data according to the limits |
|
| 289 |
#' set in the limit1hm, and limit2hm arguments of the \link{report_sea_age-class}.
|
|
| 290 |
#' |
|
| 291 |
#' If no value are provided in the limit1hm slot, an error is returned, if |
|
| 292 |
#' no value is provided in the limit2hm slot a default upper value for salmon |
|
| 293 |
#' size is taken to ensure all salmon are either of age 1 or 2, but no age 3 are |
|
| 294 |
#' returned |
|
| 295 |
#' @param object An object of class \code{\link{report_sea_age-class}}
|
|
| 296 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 297 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 298 |
#' @return An object of class \link{report_sea_age-class} with calculated data in slot calcdata
|
|
| 299 |
#' @aliases calcule.report_sea_age |
|
| 300 |
setMethod( |
|
| 301 |
"calcule", |
|
| 302 |
signature = signature("report_sea_age"),
|
|
| 303 |
definition = function(object, silent) {
|
|
| 304 |
#r_seaa<-r_sample_char |
|
| 305 | 2x |
r_seaa <- object |
| 306 | 2x |
if (nrow(r_seaa@data) == 0) {
|
| 307 | ! |
funout( |
| 308 | ! |
gettext("you have no line in the database for this period", domain = "R-stacomiR"),
|
| 309 | ! |
arret = TRUE |
| 310 |
) |
|
| 311 |
} |
|
| 312 | 2x |
adm = r_seaa@data # we get the data.frame |
| 313 |
# the age already present in the database don't interest us there |
|
| 314 | 2x |
adm = adm[adm$car_par_code != 'A124', ] |
| 315 | 2x |
if (is.na(as.numeric(r_seaa@limit1hm@label))) |
| 316 | 2x |
stop("internal error")
|
| 317 |
# if no value, a dummy value of 2m |
|
| 318 | 2x |
if (is.na(as.numeric(r_seaa@limit2hm@label))) |
| 319 | 2x |
r_seaa@limit2hm@label <- 2000 |
| 320 | 2x |
lescoupes <- |
| 321 | 2x |
c(0, |
| 322 | 2x |
as.numeric(r_seaa@limit1hm@label), |
| 323 | 2x |
as.numeric(r_seaa@limit2hm@label), |
| 324 | 2x |
2001) |
| 325 | 2x |
adm$age <- |
| 326 | 2x |
cut( |
| 327 | 2x |
x = adm$car_valeur_quantitatif, |
| 328 | 2x |
breaks = lescoupes, |
| 329 | 2x |
labels = FALSE |
| 330 |
) |
|
| 331 | 2x |
r_seaa@calcdata[["data"]] <- adm |
| 332 | 2x |
assign("r_seaa", r_seaa, envir_stacomi)
|
| 333 | 2x |
return(r_seaa) |
| 334 |
} |
|
| 335 |
) |
|
| 336 | ||
| 337 | ||
| 338 |
#' Plots of various type for report_sea_age |
|
| 339 |
#' |
|
| 340 |
#' @param x An object of class \link{report_sea_age-class}
|
|
| 341 |
#' @param plot.type Default "1" |
|
| 342 |
#' \itemize{
|
|
| 343 |
#' \item{plot.type="1"}{density plot by sea age}
|
|
| 344 |
#' \item{plot.type="2"}{Density plot by sea age and dc}
|
|
| 345 |
#' } |
|
| 346 |
#' @param silent Default FALSE, if TRUE the program should no display messages. |
|
| 347 |
#' @return Nothing, called for its side effect of plotting |
|
| 348 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 349 |
#' @aliases plot.report_sea_age |
|
| 350 |
#' @export |
|
| 351 |
setMethod( |
|
| 352 |
"plot", |
|
| 353 |
signature(x = "report_sea_age", y = "missing"), |
|
| 354 |
definition = function(x, |
|
| 355 |
plot.type = "1", |
|
| 356 |
silent = FALSE) {
|
|
| 357 |
#require(ggplot2);plot.type="1" |
|
| 358 |
#browser() |
|
| 359 | 5x |
r_seaa <- x |
| 360 | 5x |
plot.type <- as.character(plot.type)# to pass also characters |
| 361 | 5x |
if (!plot.type %in% c("1", "2"))
|
| 362 | 5x |
stop('plot.type must be 1,2')
|
| 363 | 5x |
if (nrow(r_seaa@calcdata[["data"]]) == 0) {
|
| 364 | ! |
if (!silent) |
| 365 | ! |
funout( |
| 366 | ! |
gettext("You need to launch computation first, clic on calc\n", domain =
|
| 367 | ! |
"R-stacomiR"), |
| 368 | ! |
arret = TRUE |
| 369 |
) |
|
| 370 |
} |
|
| 371 | 5x |
dat <- r_seaa@calcdata[["data"]] |
| 372 |
# cols are using viridis::inferno(6,alpha=0.9) |
|
| 373 | 5x |
les_coupes = as.numeric(c(r_seaa@limit1hm@label, r_seaa@limit2hm@label)) |
| 374 |
|
|
| 375 |
|
|
| 376 |
################################################# |
|
| 377 |
# plot.type =1 density plot |
|
| 378 |
################################################# |
|
| 379 |
|
|
| 380 | 5x |
if (plot.type == "1") {
|
| 381 | 2x |
p <- |
| 382 | 2x |
ggplot(dat) + geom_histogram( |
| 383 | 2x |
aes(x = car_valeur_quantitatif, fill = factor(age)), |
| 384 | 2x |
binwidth = 10, |
| 385 | 2x |
alpha = 0.8 |
| 386 |
) + |
|
| 387 | 2x |
geom_vline(xintercept = les_coupes, |
| 388 | 2x |
lty = 2, |
| 389 | 2x |
lwd = 1) + |
| 390 | 2x |
annotate( |
| 391 | 2x |
"text", |
| 392 | 2x |
x = les_coupes, |
| 393 | 2x |
y = 0, |
| 394 | 2x |
label = les_coupes, |
| 395 | 2x |
vjust = 1, |
| 396 | 2x |
hjust = -0.2 |
| 397 |
) + |
|
| 398 | 2x |
theme_minimal() + |
| 399 | 2x |
scale_fill_manual("Age",
|
| 400 | 2x |
values = c("1" = "#379ec6", "2" = "#173957", "3" = "#b09953")) +
|
| 401 | 2x |
xlab("Size in mm") +
|
| 402 | 2x |
ylab("Effectif")
|
| 403 | 2x |
print(p) |
| 404 | 2x |
assign("p", p, envir = envir_stacomi)
|
| 405 | 2x |
if (!silent){
|
| 406 | 1x |
funout( |
| 407 | 1x |
gettext( |
| 408 | 1x |
"The graphical object is written is env_stacomi, type p<-get('p',envir=envir_stacomi)",
|
| 409 | 1x |
domain = "R-stacomiR" |
| 410 |
) |
|
| 411 |
)} |
|
| 412 |
|
|
| 413 |
} |
|
| 414 |
###################################### |
|
| 415 |
# Migration according to stage, month and year |
|
| 416 |
###################################### |
|
| 417 |
# todo see of anotation is possible |
|
| 418 | 5x |
if (plot.type == "2") {
|
| 419 | 3x |
p <- |
| 420 | 3x |
ggplot(dat) + geom_histogram( |
| 421 | 3x |
aes(x = car_valeur_quantitatif, fill = factor(age)), |
| 422 | 3x |
binwidth = 10, |
| 423 | 3x |
alpha = 0.8 |
| 424 |
) + |
|
| 425 | 3x |
geom_vline(xintercept = les_coupes, |
| 426 | 3x |
lty = 2, |
| 427 | 3x |
lwd = 1) + |
| 428 | 3x |
theme_minimal() + |
| 429 | 3x |
scale_fill_manual("Age",
|
| 430 | 3x |
values = c("1" = "#379ec6", "2" = "#173957", "3" = "#b09953")) +
|
| 431 | 3x |
xlab("Size in mm") +
|
| 432 | 3x |
ylab("Effectif") +
|
| 433 | 3x |
facet_grid(ope_dic_identifiant ~ .) |
| 434 | 3x |
print(p) |
| 435 | 3x |
assign("p", p, envir = envir_stacomi)
|
| 436 | 3x |
if (!silent){
|
| 437 | 1x |
funout( |
| 438 | 1x |
gettext( |
| 439 | 1x |
"The graphical object is written is env_stacomi, type p<-get('p',envir=envir_stacomi)",
|
| 440 | 1x |
domain = "R-stacomiR" |
| 441 |
) |
|
| 442 |
) |
|
| 443 |
} |
|
| 444 |
} |
|
| 445 | 5x |
return(invisible(NULL)) |
| 446 |
} |
|
| 447 |
) |
|
| 448 | ||
| 449 |
#' summary for report_sea_age |
|
| 450 |
#' @param object An object of class \link{report_sea_age-class}
|
|
| 451 |
#' @param silent Default FALSE, if TRUE the program should no display messages. |
|
| 452 |
#' @param ... Additional parameters |
|
| 453 |
#' @return The summary |
|
| 454 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 455 |
#' @aliases summary.report_sea_age |
|
| 456 |
#' @export |
|
| 457 |
setMethod( |
|
| 458 |
"summary", |
|
| 459 |
signature = signature(object = "report_sea_age"), |
|
| 460 |
definition = function(object, silent = FALSE, ...) {
|
|
| 461 | 1x |
r_seaa <- object |
| 462 | 1x |
dat <- r_seaa@calcdata[["data"]] |
| 463 | 1x |
if (nrow(dat) == 0) {
|
| 464 | ! |
if (!silent) |
| 465 | ! |
funout( |
| 466 | ! |
gettext("You need to launch computation first, clic on calc\n", domain =
|
| 467 | ! |
"R-stacomiR"), |
| 468 | ! |
arret = TRUE |
| 469 |
) |
|
| 470 |
} |
|
| 471 | 1x |
ndc = unique(dat$ope_dic_identifiant) |
| 472 | 1x |
result <- list() |
| 473 | 1x |
for (i in 1:length(ndc)) {
|
| 474 | 3x |
datdc <- dat[dat$ope_dic_identifiant == ndc[i], ] |
| 475 | 3x |
dc_code <- r_seaa@dc@data$dc_code[r_seaa@dc@data$dc == ndc[i]] |
| 476 | 3x |
ouvrage <- |
| 477 | 3x |
gsub("[\r\n]", "", r_seaa@dc@data[r_seaa@dc@data$dc == r_seaa@dc@dc_selected[i], "ouv_libelle"])
|
| 478 | 3x |
dc <- as.character(unique(datdc$ope_dic_identifiant)) |
| 479 | 3x |
result[[dc]] <- list() |
| 480 | 3x |
result[[dc]][["ouvrage"]] <- ouvrage |
| 481 | 3x |
print(noquote( |
| 482 | 3x |
stringr::str_c("Age Statistics for dam : ", ouvrage, " CD=", dc_code)
|
| 483 |
)) |
|
| 484 | 3x |
print(noquote("========================"))
|
| 485 | 3x |
print(table(datdc$age)) |
| 486 | 3x |
result[[dc]][["age"]] <- table(datdc$age) |
| 487 |
|
|
| 488 |
} |
|
| 489 | 1x |
if (length(ndc) > 1) {
|
| 490 | 1x |
print(noquote(stringr::str_c("Age Statistics total")))
|
| 491 | 1x |
print(noquote("========================"))
|
| 492 | 1x |
print(table(dat$age)) |
| 493 |
|
|
| 494 |
} |
|
| 495 | 1x |
return(result) |
| 496 |
} |
|
| 497 |
) |
|
| 498 | ||
| 499 |
#' Command line method to write the characteristic "sea age" (car_par_code='A124') |
|
| 500 |
#' into the tj_caracteristiquelot_car table in the user's scheme |
|
| 501 |
#' |
|
| 502 |
#' The sea age characteristic is calculated from the measured or calculated size of salmon and with a size/age rule |
|
| 503 |
#' defined by the user. |
|
| 504 |
#' @param object an object of class \link{report_sea_age-class}
|
|
| 505 |
#' @param silent : Default FALSE, if TRUE the program should no display messages. |
|
| 506 |
#' @return Nothing, called for its side effect of writing data to the database |
|
| 507 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 508 |
#' @aliases write_database.report_sea_age |
|
| 509 |
#' @export |
|
| 510 |
setMethod( |
|
| 511 |
"write_database", |
|
| 512 |
signature = signature("report_sea_age"),
|
|
| 513 |
definition = function(object, silent = TRUE) {
|
|
| 514 |
# dbname="bd_contmig_nat" |
|
| 515 | 1x |
r_seaa <- object |
| 516 | 1x |
calcdata <- r_seaa@calcdata[["data"]] |
| 517 | 1x |
data_in_base <- r_seaa@data |
| 518 | 1x |
if (nrow(calcdata) == 0) {
|
| 519 | ! |
if (!silent) |
| 520 | ! |
funout( |
| 521 | ! |
gettext("You need to launch computation first, clic on calc\n", domain =
|
| 522 | ! |
"R-stacomiR"), |
| 523 | ! |
arret = TRUE |
| 524 |
) |
|
| 525 |
} |
|
| 526 | 1x |
if (!inherits(r_seaa, "report_sea_age")) |
| 527 | 1x |
stop("the r_seaa should be of class report_sea_age")
|
| 528 | 1x |
if (!inherits(silent, "logical")) |
| 529 | 1x |
stop("the silent argument should be a logical")
|
| 530 | 1x |
data_in_base <- data_in_base[data_in_base$car_par_code == 'A124', ] |
| 531 | 1x |
if (nrow(data_in_base) > 0) {
|
| 532 | 1x |
supprime(r_seaa, silent = silent) |
| 533 |
} |
|
| 534 |
#-------------- |
|
| 535 |
# creating the table to import |
|
| 536 |
#-------------- |
|
| 537 | 1x |
code_parametre_age = 'A124' |
| 538 | 1x |
code_methode_obtention = "CALCULE" |
| 539 | 1x |
comment = gettextf( |
| 540 | 1x |
"Age calculated from the size of fish compared to reference value %s for the limit between 1 sea winter and 2 sea winter fish, and %s for the limit between 2 sea winter fish and 3 sea winter fish", |
| 541 | 1x |
r_seaa@limit1hm@label, |
| 542 | 1x |
r_seaa@limit2hm@label |
| 543 |
) |
|
| 544 | 1x |
bam = data.frame( |
| 545 | 1x |
r_seaa@calcdata$data$lot_identifiant, |
| 546 | 1x |
code_parametre_age, |
| 547 | 1x |
code_methode_obtention, |
| 548 | 1x |
as.integer(NA), |
| 549 | 1x |
r_seaa@calcdata$data$age, |
| 550 | 1x |
as.integer(NA), |
| 551 | 1x |
comment, |
| 552 | 1x |
get_org() |
| 553 |
) |
|
| 554 |
#-------------- |
|
| 555 |
# writing the table in the database |
|
| 556 |
#-------------- |
|
| 557 | 1x |
con <- new("ConnectionDB")
|
| 558 | 1x |
con <- connect(con) |
| 559 | 1x |
on.exit(pool::poolClose(con@connection)) |
| 560 | 1x |
pool::dbWriteTable(con@connection, |
| 561 | 1x |
name = "bam", |
| 562 | 1x |
value=bam, |
| 563 | 1x |
temporary=TRUE) |
| 564 |
|
|
| 565 | 1x |
sql <- |
| 566 | 1x |
stringr::str_c( |
| 567 | 1x |
"INSERT INTO ", |
| 568 | 1x |
get_schema(), |
| 569 | 1x |
"tj_caracteristiquelot_car SELECT * FROM bam;" |
| 570 |
) |
|
| 571 |
|
|
| 572 | 1x |
pool::dbExecute(con@connection, statement = sql) |
| 573 |
|
|
| 574 | 1x |
if (!silent) {
|
| 575 | ! |
funout(gettextf("Writing %s age values in the database \n", nrow(bam)))
|
| 576 |
} |
|
| 577 | 1x |
return(invisible(NULL)) |
| 578 |
} |
|
| 579 |
) |
|
| 580 | ||
| 581 |
#' Method to print the command line of the object |
|
| 582 |
#' @param x An object of class report_sea_age |
|
| 583 |
#' @param ... Additional parameters passed to print |
|
| 584 |
#' @return NULL |
|
| 585 |
#' @author cedric.briand |
|
| 586 |
#' @aliases print.report_sea_age |
|
| 587 |
#' @export |
|
| 588 |
setMethod( |
|
| 589 |
"print", |
|
| 590 |
signature = signature("report_sea_age"),
|
|
| 591 |
definition = function(x, ...) {
|
|
| 592 | 1x |
sortie1 <- "r_seaa=new('report_sea_age')"
|
| 593 | 1x |
sortie2 <- stringr::str_c( |
| 594 | 1x |
"r_seaa=choice_c(r_seaa,", |
| 595 | 1x |
"dc=c(",
|
| 596 | 1x |
stringr::str_c(x@dc@dc_selected, collapse = ","), |
| 597 |
"),", |
|
| 598 | 1x |
"taxa=c(",
|
| 599 | 1x |
stringr::str_c(shQuote(x@taxa@data$tax_nom_latin), collapse = ","), |
| 600 |
"),", |
|
| 601 | 1x |
"stage=c(",
|
| 602 | 1x |
stringr::str_c(shQuote(x@stage@stage_selected), collapse = ","), |
| 603 |
"),", |
|
| 604 | 1x |
"par=c(",
|
| 605 | 1x |
stringr::str_c(shQuote(x@par@par_selected), collapse = ","), |
| 606 |
"),", |
|
| 607 | 1x |
"horodatedebut=", |
| 608 | 1x |
shQuote( |
| 609 | 1x |
strftime(x@horodatedebut@horodate, format = "%d/%m/%Y %H-%M-%S") |
| 610 |
), |
|
| 611 | 1x |
",horodatefin=", |
| 612 | 1x |
shQuote( |
| 613 | 1x |
strftime(x@horodatefin@horodate, format = "%d/%m/%Y %H-%M-%S") |
| 614 |
), |
|
| 615 |
")" |
|
| 616 |
) |
|
| 617 |
# removing backslashes |
|
| 618 | 1x |
funout(sortie1) |
| 619 | 1x |
funout(stringr::str_c(sortie2, ...)) |
| 620 | 1x |
return(invisible(NULL)) |
| 621 |
} |
|
| 622 |
) |
|
| 623 | ||
| 624 | ||
| 625 | ||
| 626 | ||
| 627 |
#' supprime method for report_mig_interannual class |
|
| 628 |
#' @param object An object of class \link{report_sea_age-class}
|
|
| 629 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 630 |
#' @return Nothing, called for its side effect of deleting data in the database |
|
| 631 |
#' @aliases supprime.report_sea_age |
|
| 632 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 633 |
#' @export |
|
| 634 |
setMethod( |
|
| 635 |
"supprime", |
|
| 636 |
signature = signature("report_sea_age"),
|
|
| 637 |
definition = function(object, silent = FALSE) |
|
| 638 |
{
|
|
| 639 | 2x |
r_seaa <- object |
| 640 | 2x |
data_in_base <- r_seaa@data |
| 641 | 2x |
data_in_base <- data_in_base[data_in_base$car_par_code == 'A124', ] |
| 642 | 2x |
if (nrow(data_in_base) == 0) {
|
| 643 | ! |
if (!silent) |
| 644 | ! |
funout(gettext("No data to remove"), arret = TRUE)
|
| 645 |
} |
|
| 646 | 2x |
con = new("ConnectionDB")
|
| 647 | 2x |
con <- connect(con) |
| 648 | 2x |
on.exit(pool::poolClose(con@connection)) |
| 649 | 2x |
sql = stringr::str_c("DELETE from ",
|
| 650 | 2x |
get_schema(), |
| 651 | 2x |
"tj_caracteristiquelot_car ", |
| 652 | 2x |
"WHERE car_lot_identifiant IN ", |
| 653 | 2x |
vector_to_listsql(data_in_base$lot_identifiant), |
| 654 | 2x |
" AND car_par_code='A124';" |
| 655 |
) |
|
| 656 | 2x |
pool::dbExecute(con@connection, statement = sql) |
| 657 | 2x |
return(invisible(NULL)) |
| 658 |
} |
|
| 659 | ||
| 660 |
) |
| 1 |
#' Validity check for ref_year |
|
| 2 |
#' |
|
| 3 |
#' validity_year tests for validity within the class |
|
| 4 |
#' |
|
| 5 |
#' |
|
| 6 |
#' @param object An object of class \code{\linkS4class{ref_year}}
|
|
| 7 |
#' @return boolean The test for the object refannee |
|
| 8 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 9 |
#' @keywords internal |
|
| 10 |
validity_year = function(object) |
|
| 11 |
{
|
|
| 12 | ! |
rep1 = inherits(object@data, "data.frame") |
| 13 | ! |
rep2 = inherits(object@year_selected, "numeric") |
| 14 |
|
|
| 15 | ! |
return(ifelse(rep1 & rep2, TRUE, FALSE)) |
| 16 |
} |
|
| 17 |
#definition de la classe |
|
| 18 | ||
| 19 |
#' Year reference class |
|
| 20 |
#' |
|
| 21 |
#' Class used to select one or several years |
|
| 22 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 23 |
#' \code{new("ref_year", data=data.frame(), year_selected=numeric())}.
|
|
| 24 |
#' @include create_generic.R |
|
| 25 |
#' @slot data A \code{data.frame} with the list of possible years selected as numerics
|
|
| 26 |
#' @slot year_selected A numeric vector |
|
| 27 |
#' @keywords classes |
|
| 28 |
#' @family referential objects |
|
| 29 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 30 |
setClass( |
|
| 31 |
Class = "ref_year", |
|
| 32 |
representation = |
|
| 33 |
representation(data = "data.frame", year_selected = "numeric"), |
|
| 34 |
validity = validity_year, |
|
| 35 |
prototype = prototype(data = data.frame(), year_selected = numeric()) |
|
| 36 |
) |
|
| 37 | ||
| 38 |
#' Loading method for ref_year referential objects |
|
| 39 |
#' |
|
| 40 |
#' Selects year available either in the bjo table if report_object==report_mig_interannual) or in the t_operation_ope table |
|
| 41 |
#' @param object An object of class \link{ref_year-class}
|
|
| 42 |
#' @param objectreport The object report, default \code{report_ge_weight} other possible value report_mig_interannual
|
|
| 43 |
#' @return object An object of class \link{ref_year-class} with slot data filled with the available years for the corresponding report
|
|
| 44 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 45 |
#' @examples |
|
| 46 |
#' \dontrun{
|
|
| 47 |
#' object=new("ref_year")
|
|
| 48 |
#' charge(object) |
|
| 49 |
#' validObject(annee) |
|
| 50 |
#' showMethods("charge")
|
|
| 51 |
#' } |
|
| 52 |
setMethod( |
|
| 53 |
"charge", |
|
| 54 |
signature = signature("ref_year"),
|
|
| 55 |
definition = function(object, objectreport = "report_ge_weight") {
|
|
| 56 | 25x |
requete = new("RequeteDB")
|
| 57 | 25x |
if (objectreport == "report_mig_interannual") {
|
| 58 | 10x |
if (exists("ref_dc", envir_stacomi)) {
|
| 59 | 10x |
dc <- get("ref_dc", envir_stacomi)
|
| 60 | 10x |
and1 <- paste(" AND bjo_dis_identifiant in", vector_to_listsql(dc@dc_selected))
|
| 61 |
} else {
|
|
| 62 | ! |
and1 <- "" |
| 63 |
} |
|
| 64 | 10x |
if (exists("ref_taxa", envir_stacomi)) {
|
| 65 | 10x |
taxa <- get("ref_taxa", envir_stacomi)
|
| 66 | 10x |
and2 <- |
| 67 | 10x |
stringr::str_c(" AND bjo_tax_code ='", taxa@taxa_selected, "'")
|
| 68 |
} else {
|
|
| 69 | ! |
and2 <- "" |
| 70 |
} |
|
| 71 | 10x |
if (exists("ref_stage", envir_stacomi)) {
|
| 72 | 10x |
stage <- get("ref_stage", envir_stacomi)
|
| 73 | 10x |
and3 <- |
| 74 | 10x |
stringr::str_c(" AND bjo_std_code ='", stage@stage_selected, "'")
|
| 75 |
} else |
|
| 76 |
{
|
|
| 77 | ! |
and3 = "" |
| 78 |
} |
|
| 79 | 10x |
requete@sql = paste( |
| 80 | 10x |
"select DISTINCT ON (bjo_annee) bjo_annee from ", |
| 81 | 10x |
get_schema(), |
| 82 | 10x |
"t_bilanmigrationjournalier_bjo where bjo_identifiant>0 ", |
| 83 |
# I want and statements to not have to choose the order |
|
| 84 |
# the where statement is always verified |
|
| 85 | 10x |
and1, |
| 86 | 10x |
and2, |
| 87 | 10x |
and3, |
| 88 | 10x |
sep = "" |
| 89 |
) |
|
| 90 | 25x |
} else if (objectreport == "report_ge_weight") {
|
| 91 | 4x |
requete@sql = paste( |
| 92 | 4x |
"select DISTINCT ON (year) year from( select date_part('year', ope_date_debut) as year from ",
|
| 93 | 4x |
get_schema(), |
| 94 | 4x |
"t_operation_ope) as tabletemp", |
| 95 | 4x |
sep = "" |
| 96 |
) |
|
| 97 | 25x |
} else if (objectreport == "report_annual" | |
| 98 | 25x |
objectreport == "report_species") {
|
| 99 | 11x |
if (exists("ref_dc", envir_stacomi)) {
|
| 100 | 11x |
dc <- get("ref_dc", envir_stacomi)
|
| 101 | 11x |
and1 <- |
| 102 | 11x |
paste(" AND ope_dic_identifiant in ",
|
| 103 | 11x |
vector_to_listsql(dc@dc_selected)) |
| 104 |
} else {
|
|
| 105 | ! |
and1 <- "" |
| 106 |
} |
|
| 107 | 11x |
if (exists("ref_taxa", envir_stacomi)) {
|
| 108 | 11x |
taxa <- get("ref_taxa", envir_stacomi)
|
| 109 | 11x |
if (!length(taxa@taxa_selected)==0){
|
| 110 | 11x |
and2 <- |
| 111 | 11x |
stringr::str_c(" AND lot_tax_code in ",
|
| 112 | 11x |
vector_to_listsql(taxa@taxa_selected)) |
| 113 |
} else {
|
|
| 114 | ! |
and2 <- "" |
| 115 |
} |
|
| 116 |
} else {
|
|
| 117 | ! |
and2 <- "" |
| 118 |
} |
|
| 119 | 11x |
if (exists("ref_stage", envir_stacomi)) {
|
| 120 | 4x |
stage <- get("ref_stage", envir_stacomi)
|
| 121 | 4x |
if (!length(stage@stage_selected)==0){
|
| 122 | 4x |
and3 <- |
| 123 | 4x |
stringr::str_c(" AND lot_std_code in ",
|
| 124 | 4x |
vector_to_listsql(stage@stage_selected)) |
| 125 |
} else |
|
| 126 |
{
|
|
| 127 | ! |
and3 = "" |
| 128 |
} |
|
| 129 |
} else |
|
| 130 |
{
|
|
| 131 | 7x |
and3 = "" |
| 132 |
} |
|
| 133 | 11x |
requete@sql = paste( |
| 134 | 11x |
"select DISTINCT ON (year) year from (select date_part('year', ope_date_debut) as year from ",
|
| 135 | 11x |
get_schema(), |
| 136 | 11x |
"t_operation_ope JOIN ", |
| 137 | 11x |
get_schema(), |
| 138 | 11x |
"t_lot_lot on lot_ope_identifiant=ope_identifiant", |
| 139 | 11x |
" WHERE lot_lot_identifiant is null", |
| 140 | 11x |
and1, |
| 141 | 11x |
and2, |
| 142 | 11x |
and3, |
| 143 | 11x |
") as tabletemp", |
| 144 | 11x |
sep = "" |
| 145 |
) |
|
| 146 |
} else {
|
|
| 147 | ! |
funout(gettextf("Not implemented for objectreport = %s", objectreport),
|
| 148 | ! |
arret = TRUE) |
| 149 |
} |
|
| 150 | 25x |
requete <- |
| 151 | 25x |
stacomirtools::query(requete) |
| 152 | 25x |
object@data <- stacomirtools::getquery(requete) |
| 153 | 25x |
return(object) |
| 154 |
} |
|
| 155 |
) |
|
| 156 | ||
| 157 | ||
| 158 |
#' choice_c method for ref_year referential from the command line |
|
| 159 |
#' |
|
| 160 |
#' The choice_c method will issue a warning if the year is not present in the database |
|
| 161 |
#' Allows the selection of year and the assignment in environment envir_stacomi |
|
| 162 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 163 |
#' @param object An object of class \link{ref_year-class}
|
|
| 164 |
#' @param annee The year to select, either as a character or as a numeric |
|
| 165 |
#' @param nomassign The name to be assigned in envir_stacomi |
|
| 166 |
#' @param funoutlabel The label that appears in funout |
|
| 167 |
#' @param silent Stops messages from being displayed if silent=TRUE, default FALSE |
|
| 168 |
#' @return object An object of class \link{ref_year-class} with year selected
|
|
| 169 |
#' @examples |
|
| 170 |
#' \dontrun{
|
|
| 171 |
#' object=new("ref_year")
|
|
| 172 |
#' object<-charge(object) |
|
| 173 |
#' win=gwindow(title="test ref_year") |
|
| 174 |
#' group=ggroup(container=win,horizontal=FALSE) |
|
| 175 |
#' choice(object,nomassign="ref_year",funoutlabel="essai",titleFrame="essai ref_year",preselect=1) |
|
| 176 |
#' dispose(win) |
|
| 177 |
#' } |
|
| 178 |
setMethod( |
|
| 179 |
"choice_c", |
|
| 180 |
signature = signature("ref_year"),
|
|
| 181 |
definition = function(object, |
|
| 182 |
annee, |
|
| 183 |
nomassign = "ref_year", |
|
| 184 |
funoutlabel = gettext("Year selected\n", domain = "R-stacomiR"),
|
|
| 185 |
silent = FALSE) {
|
|
| 186 | 49x |
if (length(annee) > 1) |
| 187 | 49x |
stop("horodate should be a vector of length 1")
|
| 188 | 49x |
if (inherits (annee, "character")) |
| 189 | 49x |
annee <- as.numeric(annee) |
| 190 |
# the charge method must be performed before |
|
| 191 | 49x |
gettext("no year", domain = "R-stacomiR")
|
| 192 | 49x |
if (!annee %in% object@data[, 1]) {
|
| 193 | ! |
warning( |
| 194 | ! |
stringr::str_c( |
| 195 | ! |
"Attention, year ", |
| 196 | ! |
annee, |
| 197 | ! |
" is not available in the database, available years :", |
| 198 | ! |
ifelse( |
| 199 | ! |
length(object@data$bjo_annee) == 0, |
| 200 | ! |
gettext(" none", domain = "R-stacomiR"),
|
| 201 | ! |
stringr::str_c(object@data$bjo_annee, collapse = ",") |
| 202 |
) |
|
| 203 |
) |
|
| 204 |
) |
|
| 205 |
} |
|
| 206 | 48x |
object@year_selected <- annee |
| 207 |
|
|
| 208 | 48x |
assign(nomassign, object, envir_stacomi) |
| 209 | 48x |
if (!silent) |
| 210 | 48x |
funout(funoutlabel) |
| 211 | 48x |
return(object) |
| 212 |
} |
|
| 213 |
) |
| 1 |
#' Class 'report_sample_char' |
|
| 2 |
#' |
|
| 3 |
#' The report_sample_char class is used to load and display sample characteristics, which can be either |
|
| 4 |
#' continuous or discrete variable, for instance, it can be used to analyze size or sex structure during |
|
| 5 |
#' a given period. |
|
| 6 |
#' |
|
| 7 |
#' @note This class is displayed by interface_report_sample_char, in the database, the class calls the content |
|
| 8 |
#' of the view vue_lot_ope_car |
|
| 9 |
#' @slot data A data frame |
|
| 10 |
#' @slot dc An object of class \link{ref_dc-class}: the control devices
|
|
| 11 |
#' @slot taxa An object of class \link{ref_taxa-class}: the species
|
|
| 12 |
#' @slot stage An object of class \link{ref_stage-class} : the stages of the fish
|
|
| 13 |
#' @slot par An object of class \link{ref_par-class}: the parameters used
|
|
| 14 |
#' @slot horodatedebut An object of class \link{ref_horodate-class}
|
|
| 15 |
#' @slot horodatefin An object of class \link{ref_horodate-class}
|
|
| 16 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 17 |
#' \code{new('report_sample_char', ...)}
|
|
| 18 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 19 |
#' @family report Objects |
|
| 20 |
#' @keywords classes |
|
| 21 |
#' @example inst/examples/report_sample_char-example.R |
|
| 22 |
#' @aliases report_sample_char |
|
| 23 |
#' @export |
|
| 24 |
setClass(Class = "report_sample_char", representation = representation(data = "ANY", |
|
| 25 |
dc = "ref_dc", taxa = "ref_taxa", stage = "ref_stage", par = "ref_par", horodatedebut = "ref_horodate", |
|
| 26 |
horodatefin = "ref_horodate"), prototype = prototype(data = data.frame(), dc = new("ref_dc"),
|
|
| 27 |
taxa = new("ref_taxa"), stage = new("ref_stage"), par = new("ref_par"), horodatedebut = new("ref_horodate"),
|
|
| 28 |
horodatefin = new("ref_horodate")))
|
|
| 29 | ||
| 30 |
#' connect method for report_sample_char |
|
| 31 |
#' |
|
| 32 |
#' @param object An object of class \link{report_sample_char-class}
|
|
| 33 |
#' @param silent Boolean if TRUE messages are not displayed |
|
| 34 |
#' @return An object of class \link{report_sample_char-class} with slot data \code{@data} filled
|
|
| 35 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 36 |
#' @aliases connect.report_sample_char |
|
| 37 |
setMethod("connect", signature = signature("report_sample_char"), definition = function(object,
|
|
| 38 |
silent = FALSE) {
|
|
| 39 | 3x |
requete <- new("RequeteDBwheredate")
|
| 40 | 3x |
requete@select = paste("SELECT * FROM ", get_schema(),
|
| 41 | 3x |
"vue_lot_ope_car", sep = "") |
| 42 | 3x |
requete@colonnedebut = "ope_date_debut" |
| 43 | 3x |
requete@colonnefin = "ope_date_fin" |
| 44 | 3x |
requete@datedebut <- object@horodatedebut@horodate |
| 45 | 3x |
requete@datefin <- object@horodatefin@horodate |
| 46 | 3x |
requete@order_by = "ORDER BY ope_date_debut" |
| 47 | 3x |
requete@and = paste(" AND ope_dic_identifiant in ", vector_to_listsql(object@dc@dc_selected),
|
| 48 | 3x |
" AND lot_tax_code in ", vector_to_listsql(object@taxa@taxa_selected), " AND lot_std_code in ", |
| 49 | 3x |
vector_to_listsql(object@stage@stage_selected), " AND car_par_code in ", vector_to_listsql(object@par@par_selected), |
| 50 | 3x |
sep = "") |
| 51 | 3x |
requete <- stacomirtools::query(requete) |
| 52 | 3x |
object@data <- requete@query |
| 53 | 3x |
if (!silent) |
| 54 | ! |
funout(gettext("Sample characteristics have been loaded from the database\n",
|
| 55 | ! |
domain = "R-stacomiR")) |
| 56 | 3x |
return(object) |
| 57 |
}) |
|
| 58 | ||
| 59 | ||
| 60 |
#' charge method for report_sample_char class |
|
| 61 |
#' |
|
| 62 |
#' this method verifies that boxes have been clicked in the user interface and gets the objects pasted in |
|
| 63 |
#' envir_stacomi |
|
| 64 |
#' @param object An object of class \link{report_sample_char-class}
|
|
| 65 |
#' @return An object of class \link{report_sample_char-class} with slots filled with user choice
|
|
| 66 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 67 |
#' @return An object of the class \link{report_sample_char-class} with slots filled from values assigned in \code{envir_stacomi} environment
|
|
| 68 |
#' @aliases charge.report_sample_char |
|
| 69 |
#' @keywords internal |
|
| 70 |
setMethod("charge", signature = signature("report_sample_char"), definition = function(object) {
|
|
| 71 | 2x |
if (exists("ref_dc", envir_stacomi)) {
|
| 72 | 2x |
object@dc <- get("ref_dc", envir_stacomi)
|
| 73 |
} else {
|
|
| 74 | ! |
funout(gettext("You need to choose a counting device, clic on validate\n",
|
| 75 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 76 |
} |
|
| 77 | 2x |
if (exists("ref_taxa", envir_stacomi)) {
|
| 78 | 2x |
object@taxa <- get("ref_taxa", envir_stacomi)
|
| 79 |
} else {
|
|
| 80 | ! |
funout(gettext("You need to choose a taxa, clic on validate\n", domain = "R-stacomiR"),
|
| 81 | ! |
arret = TRUE) |
| 82 |
} |
|
| 83 | 2x |
if (exists("ref_stage", envir_stacomi)) {
|
| 84 | 2x |
object@stage <- get("ref_stage", envir_stacomi)
|
| 85 |
} else {
|
|
| 86 | ! |
funout(gettext("You need to choose a stage, clic on validate\n", domain = "R-stacomiR"),
|
| 87 | ! |
arret = TRUE) |
| 88 |
} |
|
| 89 | 2x |
if (exists("ref_par", envir_stacomi)) {
|
| 90 | 2x |
object@par <- get("ref_par", envir_stacomi)
|
| 91 |
} else {
|
|
| 92 | ! |
funout(gettext("You need to choose a parameter, clic on validate\n", domain = "R-stacomiR"),
|
| 93 | ! |
arret = TRUE) |
| 94 |
} |
|
| 95 |
# rem pas tres satisfaisant car ce nom est choisi dans l'interface |
|
| 96 | 2x |
if (exists("report_sample_char_date_debut", envir_stacomi)) {
|
| 97 | 2x |
object@horodatedebut@horodate <- get("report_sample_char_date_debut", envir_stacomi)
|
| 98 |
} else {
|
|
| 99 | ! |
funout(gettext("You need to choose the starting date\n", domain = "R-stacomiR"),
|
| 100 | ! |
arret = TRUE) |
| 101 |
} |
|
| 102 |
# rem id |
|
| 103 | 2x |
if (exists("report_sample_char_date_fin", envir_stacomi)) {
|
| 104 | 2x |
object@horodatefin@horodate <- get("report_sample_char_date_fin", envir_stacomi)
|
| 105 |
} else {
|
|
| 106 | ! |
funout(gettext("You need to choose the ending date\n", domain = "R-stacomiR"),
|
| 107 | ! |
arret = TRUE) |
| 108 |
} |
|
| 109 | 2x |
assign("report_sample_char", object, envir_stacomi)
|
| 110 | 2x |
return(object) |
| 111 |
}) |
|
| 112 | ||
| 113 | ||
| 114 |
#' command line interface for report_sample_char class |
|
| 115 |
#' |
|
| 116 |
#' #' The choice_c method fills in the data slot for classes \link{ref_dc-class}, \link{ref_taxa-class}, \link{ref_stage-class}, \link{ref_par-class} and two slots of \link{ref_horodate-class} and then
|
|
| 117 |
#' uses the choice_c methods of these object to select the data. |
|
| 118 |
#' @param object An object of class \link{report_sample_char-class}
|
|
| 119 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 120 |
#' @param taxa Either a species name in latin or the SANDRE code for species (ie 2038=Anguilla anguilla), |
|
| 121 |
#' these should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 122 |
#' @param stage A stage code matching the ref.tr_stadedeveloppement_std table in the stacomi database, see \link{choice_c,ref_stage-method}
|
|
| 123 |
#' @param par A parameter matching th ref.tg_parametre_par table in the stacomi database, see \link{choice_c,ref_par-method}
|
|
| 124 |
#' @param horodatedebut The starting date as a character, formats like \code{\%Y-\%m-\%d} or \code{\%d-\%m-\%Y} can be used as input
|
|
| 125 |
#' @param horodatefin The finishing date of the report, for this class this will be used to calculate the number of daily steps. |
|
| 126 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 127 |
#' @return An object of class \link{report_mig-class} with data selected
|
|
| 128 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 129 |
#' @aliases choice_c.report_sample_char |
|
| 130 |
setMethod("choice_c", signature = signature("report_sample_char"), definition = function(object,
|
|
| 131 |
dc, taxa, stage, par, horodatedebut, horodatefin, silent = FALSE) {
|
|
| 132 |
# code for debug using example |
|
| 133 |
# report_sample_char<-r_sample_char;dc=c(5,6);taxa='Anguilla anguilla' |
|
| 134 |
# stage=c('CIV','AGJ');par=c(1785,1786,1787,'C001');horodatedebut='2010-01-01';horodatefin='2015-12-31'
|
|
| 135 | 4x |
report_sample_char <- object |
| 136 | 4x |
report_sample_char@dc = charge(report_sample_char@dc) |
| 137 |
# loads and verifies the dc this will set dc_selected slot |
|
| 138 | 4x |
report_sample_char@dc <- choice_c(object = report_sample_char@dc, dc) |
| 139 |
# only taxa present in the report_mig are used |
|
| 140 | 4x |
report_sample_char@taxa <- charge_with_filter(object = report_sample_char@taxa, |
| 141 | 4x |
report_sample_char@dc@dc_selected) |
| 142 | 4x |
report_sample_char@taxa <- choice_c(report_sample_char@taxa, taxa) |
| 143 | 4x |
report_sample_char@stage <- charge_with_filter(object = report_sample_char@stage, |
| 144 | 4x |
report_sample_char@dc@dc_selected, report_sample_char@taxa@taxa_selected) |
| 145 | 4x |
report_sample_char@stage <- choice_c(report_sample_char@stage, stage) |
| 146 | 4x |
report_sample_char@par <- charge_with_filter(object = report_sample_char@par, |
| 147 | 4x |
report_sample_char@dc@dc_selected, report_sample_char@taxa@taxa_selected, |
| 148 | 4x |
report_sample_char@stage@stage_selected) |
| 149 | 4x |
report_sample_char@par <- choice_c(report_sample_char@par, par, silent = silent) |
| 150 | 4x |
report_sample_char@horodatedebut <- choice_c(object = report_sample_char@horodatedebut, |
| 151 | 4x |
nomassign = "report_sample_char_date_debut", funoutlabel = gettext("Beginning date has been chosen\n",
|
| 152 | 4x |
domain = "R-stacomiR"), horodate = horodatedebut, silent = silent) |
| 153 | 4x |
report_sample_char@horodatefin <- choice_c(report_sample_char@horodatefin, nomassign = "report_sample_char_date_fin", |
| 154 | 4x |
funoutlabel = gettext("Ending date has been chosen\n", domain = "R-stacomiR"),
|
| 155 | 4x |
horodate = horodatefin, silent = silent) |
| 156 | 4x |
return(report_sample_char) |
| 157 |
}) |
|
| 158 | ||
| 159 |
#' Calculation for report_sample_char |
|
| 160 |
#' |
|
| 161 |
#' In that class, most treatments are done in the query, this method checks that data are available and fills information for year, month, two weeks, week, doy |
|
| 162 |
#' @param object An object of class \code{\link{report_sample_char-class}}
|
|
| 163 |
#' @param silent Boolean, if TRUE, information messages are not displayed, only warnings and errors |
|
| 164 |
#' @return An object of class \code{\link{report_sample_char-class}} with slot \code{@data} filled
|
|
| 165 |
#' @aliases calcule.report_sample_char |
|
| 166 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 167 |
setMethod("calcule", signature = signature("report_sample_char"), definition = function(object,
|
|
| 168 |
silent = FALSE) {
|
|
| 169 |
# report_sample_char<-r_sample_char |
|
| 170 | 3x |
report_sample_char <- object |
| 171 | 3x |
if (nrow(report_sample_char@data) == 0) {
|
| 172 | ! |
funout(gettext("No information for these samples during the selected period\n",
|
| 173 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 174 |
} |
|
| 175 | 3x |
vue_ope_lot = report_sample_char@data # on recupere le data.frame |
| 176 | 3x |
nom_variable = report_sample_char@par@data$par_nom[report_sample_char@par@data$par_code %in% |
| 177 | 3x |
report_sample_char@par@par_selected] |
| 178 |
# stopifnot(length(nom_variable)==1) |
|
| 179 | 3x |
vue_ope_lot$ope_dic_identifiant = as.factor(vue_ope_lot$ope_dic_identifiant) |
| 180 | 3x |
vue_ope_lot$dev_code = as.factor(vue_ope_lot$dev_code) |
| 181 | 3x |
vue_ope_lot$car_val_identifiant = as.factor(vue_ope_lot$car_val_identifiant) |
| 182 | 3x |
vue_ope_lot$car_par_code = as.factor(vue_ope_lot$car_par_code) |
| 183 | 3x |
vue_ope_lot$ope_identifiant = as.factor(vue_ope_lot$ope_identifiant) |
| 184 | 3x |
vue_ope_lot$lot_pere = as.factor(vue_ope_lot$lot_pere) |
| 185 | 3x |
vue_ope_lot$val_libelle = as.factor(vue_ope_lot$val_libelle) |
| 186 | 3x |
vue_ope_lot$lot_tax_code = as.factor(vue_ope_lot$lot_tax_code) |
| 187 | 3x |
vue_ope_lot <- fun_date_extraction(data = vue_ope_lot, nom_coldt = "ope_date_debut", |
| 188 | 3x |
annee = TRUE, mois = TRUE, quinzaine = TRUE, semaine = TRUE, jour_an = TRUE, |
| 189 | 3x |
jour_mois = FALSE, heure = FALSE) |
| 190 |
# vue_ope_lot=stacomirtools::chnames(vue_ope_lot, |
|
| 191 |
# c('ope_identifiant','lot_identifiant','ope_dic_identifiant','lot_pere',
|
|
| 192 |
# 'ope_date_debut','ope_date_fin','lot_effectif','lot_quantite','lot_tax_code','lot_std_code','tax_nom_latin','std_libelle','dev_code','dev_libelle','par_nom','car_par_code','car_methode_obtention','car_val_identifiant', |
|
| 193 |
# 'car_valeur_quantitatif','val_libelle', |
|
| 194 |
# 'annee','mois','quinzaine','semaine','jour_365'), |
|
| 195 |
# c('ope','lot','dic','lot_pere',
|
|
| 196 |
# 'date','date_fin','effectif','quantite','lot_tax_code','lot_std_code','tax','std','dev_code','dev','par','car_par_code','meth','val','val_quant','val_libelle', |
|
| 197 |
# 'annee','mois','quinzaine','semaine','jour')) |
|
| 198 |
# vue_ope_lot=vue_ope_lot[,c('ope','lot','dic','lot_pere','date','effectif','quantite','tax','std','dev','par','meth','val','val_quant','val_libelle',
|
|
| 199 |
# 'annee','mois','quinzaine','semaine','jour')] |
|
| 200 | 3x |
report_sample_char@data <- vue_ope_lot |
| 201 | 3x |
assign("report_sample_char", report_sample_char, envir_stacomi) #assign('report_sample_char',vue_ope_lot,envir_stacomi)
|
| 202 | 3x |
if (!silent) |
| 203 | ! |
funout(gettext("To obtain the table, type : report_sample_char=get('report_sample_char',envir_stacomi)\n",
|
| 204 | ! |
domain = "R-stacomiR")) |
| 205 | 3x |
return(report_sample_char) |
| 206 |
}) |
|
| 207 | ||
| 208 | ||
| 209 |
#' Plots of various type for reportcarlot |
|
| 210 |
#' @param x An object of class report_sample_char |
|
| 211 |
#' @param plot.type One of '1','violin plot'. Defaut to \code{1} , can also be \code{2} boxplot or
|
|
| 212 |
#' \code{3} points.
|
|
| 213 |
#' @param silent Stops displaying the messages |
|
| 214 |
#' @return Nothing, called for its side effect, plotting |
|
| 215 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 216 |
#' @aliases plot.report_sample_char |
|
| 217 |
#' @export |
|
| 218 |
setMethod("plot", signature(x = "report_sample_char", y = "missing"), definition = function(x,
|
|
| 219 |
plot.type = "1", silent = FALSE) {
|
|
| 220 |
# report_sample_char<-r_sample_char;require(ggplot2);plot.type='1' |
|
| 221 |
# browser() |
|
| 222 | 6x |
report_sample_char <- x |
| 223 | 6x |
plot.type <- as.character(plot.type) # to pass also characters |
| 224 | 6x |
if (!plot.type %in% c("1", "2", "3"))
|
| 225 | ! |
stop("plot.type must be 1,2,3")
|
| 226 | 6x |
if (exists("report_sample_char", envir_stacomi)) {
|
| 227 | 3x |
report_sample_char <- get("report_sample_char", envir_stacomi)
|
| 228 |
} else {
|
|
| 229 | 3x |
if (!silent) |
| 230 | ! |
funout(gettext("You need to launch computation first, clic on calc\n",
|
| 231 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 232 |
} |
|
| 233 | 6x |
name_param <- report_sample_char |
| 234 | 6x |
if (plot.type == 1) {
|
| 235 | 2x |
g <- ggplot(report_sample_char@data, aes(x = car_valeur_quantitatif)) |
| 236 | 2x |
g <- g + stat_density(aes(ymax = ..density.., ymin = -..density..), fill = "grey50", |
| 237 | 2x |
colour = "grey10", geom = "ribbon", position = "identity") + facet_grid(. ~ |
| 238 | 2x |
annee) + coord_flip() |
| 239 | 2x |
print(g) |
| 240 | 2x |
assign("g", g, envir_stacomi)
|
| 241 | 2x |
if (!silent) |
| 242 | ! |
funout(gettext("To obtain the graphical object, type : g<-get(\"g\",envir_stacomi)\n",
|
| 243 | ! |
domain = "R-stacomiR")) |
| 244 | 4x |
} else if (plot.type == 2) {
|
| 245 | 2x |
g <- ggplot(report_sample_char@data) |
| 246 | 2x |
g <- g + geom_boxplot(aes(x = mois, y = car_valeur_quantitatif, fill = std_libelle)) + |
| 247 | 2x |
facet_grid(annee ~ .) |
| 248 | 2x |
print(g) |
| 249 | 2x |
assign("g", g, envir_stacomi)
|
| 250 | 2x |
if (!silent) |
| 251 | ! |
funout(gettext("To obtain the graphical object, type : g<-get(\"g\",envir_stacomi)\n",
|
| 252 | ! |
domain = "R-stacomiR")) |
| 253 | ||
| 254 | 2x |
} else if (plot.type == 3) {
|
| 255 | 2x |
g <- ggplot(report_sample_char@data) |
| 256 | 2x |
g <- g + geom_point(aes(x = ope_date_debut, y = car_valeur_quantitatif)) |
| 257 | 2x |
print(g) |
| 258 | 2x |
assign("g", g, envir_stacomi)
|
| 259 | 2x |
if (!silent) |
| 260 | ! |
funout(gettext("To obtain the graphical object, type : g<-get(\"g\",envir_stacomi)\n",
|
| 261 | ! |
domain = "R-stacomiR")) |
| 262 |
} |
|
| 263 | 6x |
return(invisible(NULL)) |
| 264 |
}) |
|
| 265 | ||
| 266 |
#' summary for report_sample_char |
|
| 267 |
#' |
|
| 268 |
#' @param object An object of class \code{\link{report_sample_char-class}}
|
|
| 269 |
#' @param silent Should the program stay silent or display messages, default FALSE |
|
| 270 |
#' @param ... Additional parameters |
|
| 271 |
#' @return Nothing, called for its side effect of printing a summary |
|
| 272 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 273 |
#' @aliases summary.report_sample_char |
|
| 274 |
#' @export |
|
| 275 |
setMethod("summary", signature = signature(object = "report_sample_char"), definition = function(object,
|
|
| 276 |
silent = FALSE, ...) {
|
|
| 277 | ! |
Hmisc::describe(object@data) |
| 278 | ! |
return(invisible(NULL)) |
| 279 |
}) |
|
| 280 | ||
| 281 |
#' Method to print the command line of the object |
|
| 282 |
#' @param x An object of class report_sample_char |
|
| 283 |
#' @param ... Additional parameters passed to print |
|
| 284 |
#' @return NULL |
|
| 285 |
#' @author cedric.briand |
|
| 286 |
#' @aliases print.report_sample_char |
|
| 287 |
#' @export |
|
| 288 |
setMethod("print", signature = signature("report_sample_char"), definition = function(x,
|
|
| 289 |
...) {
|
|
| 290 | 1x |
sortie1 <- "report_sample_char=new('report_sample_char')"
|
| 291 | 1x |
sortie2 <- stringr::str_c("report_sample_char <- choice_c(report_sample_char,",
|
| 292 | 1x |
"dc=c(", stringr::str_c(x@dc@dc_selected, collapse = ","), "),", "taxa=c(",
|
| 293 | 1x |
stringr::str_c(shQuote(x@taxa@data$tax_nom_latin), collapse = ","), "),", |
| 294 | 1x |
"stage=c(", stringr::str_c(shQuote(x@stage@stage_selected), collapse = ","),
|
| 295 | 1x |
"),", "par=c(", stringr::str_c(shQuote(x@par@par_selected), collapse = ","),
|
| 296 | 1x |
"),", "horodatedebut=", shQuote(strftime(x@horodatedebut@horodate, format = "%d/%m/%Y %H-%M-%S")), |
| 297 | 1x |
",horodatefin=", shQuote(strftime(x@horodatefin@horodate, format = "%d/%m/%Y %H-%M-%S")), |
| 298 |
")") |
|
| 299 |
# removing backslashes |
|
| 300 | 1x |
funout(sortie1) |
| 301 | 1x |
funout(stringr::str_c(sortie2, ...)) |
| 302 | 1x |
return(invisible(NULL)) |
| 303 |
}) |
|
| 304 |
| 1 |
#' Counts of number per taxa/stages |
|
| 2 |
#' |
|
| 3 |
#' This class is used to make the assessment of all species, and their number. It is intended |
|
| 4 |
#' as a simple way to check what fishes are present (taxa + development stage). It was altered to include ref_taxa, |
|
| 5 |
#' to allow excluding some of the most numerous taxa from reports. The taxa is reported unless |
|
| 6 |
#' a taxa has several stages, in which case the different stages for the taxa will be reported |
|
| 7 |
#' Using the split arguments |
|
| 8 |
#' the calc method of the class will count numbers, subsamples are not accounted for in the Overview. |
|
| 9 |
#' The split argument currently takes values year or month. The class is intended to be used over long periods |
|
| 10 |
#' e.g years. The plot method writes either an histogram or a pie chart of number per |
|
| 11 |
#' year/week/month. |
|
| 12 |
#' @slot dc an object of class \link{ref_dc-class}
|
|
| 13 |
#' @slot taxa Object of class \link{ref_taxa-class}: the species
|
|
| 14 |
#' @slot start_year Object of class \code{\link{ref_year-class}}
|
|
| 15 |
#' @slot end_year Object of class \code{\link{ref_year-class}}
|
|
| 16 |
#' @slot data \code{data.frame}
|
|
| 17 |
#' @slot calcdata \code{data.frame} with data processed by the calc method
|
|
| 18 |
#' @slot split Object of class \code{\link{ref_list-class}} ref_list referential class choose within a list
|
|
| 19 |
#' @include ref_taxa.R |
|
| 20 |
#' @include ref_dc.R |
|
| 21 |
#' @include ref_list.R |
|
| 22 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 23 |
#' @family report Objects |
|
| 24 |
#' @aliases report_species |
|
| 25 |
#' @example inst/examples/report_species-example.R |
|
| 26 |
#' @keywords classes |
|
| 27 |
#' @export |
|
| 28 |
setClass(Class = "report_species", representation = representation(dc = "ref_dc", taxa = "ref_taxa", |
|
| 29 |
start_year = "ref_year", end_year = "ref_year", data = "data.frame", calcdata = "data.frame", |
|
| 30 |
split = "ref_list"), prototype = prototype(dc = new("ref_dc"), taxa = new("ref_taxa"), start_year = new("ref_year"),
|
|
| 31 |
end_year = new("ref_year"), data = data.frame(), calcdata = data.frame(), split = new("ref_list")))
|
|
| 32 | ||
| 33 | ||
| 34 |
#' connect method for report_species |
|
| 35 |
#' @param object An object of class report_species |
|
| 36 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 37 |
#' @return An object of class \link{report_species-class} with data slot filled with slot data \code{@data} filled
|
|
| 38 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 39 |
#' @aliases connect.report_species |
|
| 40 |
setMethod("connect", signature = signature("report_species"), definition = function(object,
|
|
| 41 |
silent = FALSE) {
|
|
| 42 | 7x |
bilesp <- object |
| 43 | 7x |
requete = new("RequeteDB")
|
| 44 | 7x |
start_year = bilesp@start_year@year_selected |
| 45 | 7x |
end_year = bilesp@end_year@year_selected |
| 46 | 7x |
requete@sql = paste("SELECT lot_identifiant, ope_date_debut, ope_date_fin,",
|
| 47 | 7x |
" lot_effectif, lot_tax_code, lot_std_code, tax_nom_latin, std_libelle,", |
| 48 | 7x |
" date_part('year', ope_date_debut) as annee,", " date_part('month',ope_date_debut) as mois,",
|
| 49 | 7x |
" date_part('week',ope_date_debut) as semaine", " FROM ",
|
| 50 | 7x |
get_schema(), |
| 51 | 7x |
"t_operation_ope", " INNER JOIN ", |
| 52 | 7x |
get_schema(), |
| 53 | 7x |
"t_lot_lot ON ope_identifiant=lot_ope_identifiant", " INNER JOIN ref.tr_taxon_tax on tax_code=lot_tax_code", |
| 54 | 7x |
" INNER JOIN ref.tr_stadedeveloppement_std on std_code=lot_std_code", " WHERE extract(year from ope_date_debut)>=", |
| 55 | 7x |
start_year, " AND extract(year from ope_date_debut)<=", end_year, " AND ope_dic_identifiant in", |
| 56 | 7x |
vector_to_listsql(bilesp@dc@dc_selected), "AND lot_tax_code in", vector_to_listsql(bilesp@taxa@taxa_selected), |
| 57 | 7x |
" AND lot_lot_identifiant IS NULL", |
| 58 | 7x |
" AND lot_effectif IS NOT NULL", sep = "") |
| 59 | 7x |
requete <- stacomirtools::query(requete) |
| 60 | 7x |
if (requete@status != "success") |
| 61 | 7x |
funout(gettext("Query failed for for report species \n", domain = "R-stacomiR"),
|
| 62 | 7x |
arret = TRUE) |
| 63 | 7x |
bilesp@data <- requete@query |
| 64 | 7x |
if (!silent) |
| 65 | 7x |
funout(gettext("data loaded from the database for report_species"))
|
| 66 | 7x |
assign("bilesp", bilesp, envir = envir_stacomi)
|
| 67 | 7x |
return(bilesp) |
| 68 |
}) |
|
| 69 | ||
| 70 | ||
| 71 |
#' command line interface for \link{report_species-class}
|
|
| 72 |
#' |
|
| 73 |
#' @param object An object of class \link{report_species-class}
|
|
| 74 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 75 |
#' @param taxa Either 'all' (default) or a species name in latin or the SANDRE code for species (ie 2038=Anguilla anguilla), |
|
| 76 |
#' it should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 77 |
#' @param start_year The starting the first year, passed as character or integer |
|
| 78 |
#' @param end_year the finishing year |
|
| 79 |
#' @param split one of c('none','week','month','year')
|
|
| 80 |
#' @param silent Boolean, if TRUE, information messages are not displayed |
|
| 81 |
#' @return An object of class \link{report_species-class} with data selected
|
|
| 82 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 83 |
#' @aliases choice_c.report_species |
|
| 84 |
setMethod("choice_c", signature = signature("report_species"),
|
|
| 85 |
definition = function(object, |
|
| 86 |
dc, |
|
| 87 |
taxa="all", |
|
| 88 |
split = "none", |
|
| 89 |
start_year, |
|
| 90 |
end_year, |
|
| 91 |
silent = FALSE) {
|
|
| 92 |
# code for debug using example |
|
| 93 |
# dc=c(5,6);taxa = c(start_year='1996';end_year='2016';split='none';silent=TRUE |
|
| 94 | 7x |
bilesp <- object |
| 95 | 7x |
bilesp@dc = charge(bilesp@dc) |
| 96 |
# loads and verifies the dc this will set dc_selected slot |
|
| 97 | 7x |
bilesp@dc <- choice_c(object = bilesp@dc, dc) |
| 98 |
|
|
| 99 | 7x |
bilesp@taxa <- |
| 100 | 7x |
charge_with_filter(object = bilesp@taxa, bilesp@dc@dc_selected) |
| 101 |
|
|
| 102 | 7x |
if (any(taxa=="all")) {
|
| 103 |
# taxa selected correspond to all loaded taxa for the dc |
|
| 104 | 6x |
bilesp@taxa@taxa_selected <- bilesp@taxa@data$tax_code |
| 105 |
# Here we are not using the standard choice_c which assigns in envir_stacomi... |
|
| 106 | 6x |
assign("ref_taxa", bilesp@taxa, envir=envir_stacomi)
|
| 107 |
} else {
|
|
| 108 | 1x |
bilesp@taxa <- choice_c(bilesp@taxa, taxa) |
| 109 |
} |
|
| 110 | 7x |
bilesp@split = charge(object = bilesp@split, listechoice = c("none", "week",
|
| 111 | 7x |
"month", "year"), label = gettext("choice of number in sample (one, several,all)",
|
| 112 | 7x |
domain = "R-stacomiR")) # choix de la categorie d'effectif) |
| 113 | 7x |
bilesp@split <- choice_c(bilesp@split, selectedvalue = split) |
| 114 |
# by default choice_c returns reflist but usefull to mimic gr.interface |
|
| 115 | 7x |
assign("refliste", bilesp@split, envir_stacomi)
|
| 116 | 7x |
bilesp@start_year <- charge(object = bilesp@start_year, objectreport = "report_species") |
| 117 | 7x |
bilesp@start_year <- choice_c(object = bilesp@start_year, nomassign = "start_year", |
| 118 | 7x |
annee = start_year, silent = silent) |
| 119 | 7x |
bilesp@end_year@data <- bilesp@start_year@data |
| 120 | 7x |
bilesp@end_year <- choice_c(object = bilesp@end_year, nomassign = "end_year", |
| 121 | 7x |
annee = end_year, silent = silent) |
| 122 | 7x |
assign("bilesp", bilesp, envir = envir_stacomi)
|
| 123 | 7x |
return(bilesp) |
| 124 |
}) |
|
| 125 | ||
| 126 | ||
| 127 |
#' charge method for report_species |
|
| 128 |
#' |
|
| 129 |
#' Verifies the content of objects when the graphical interface is used, it is not necessary |
|
| 130 |
#' to call the charge method if the choice_c method has been used |
|
| 131 |
#' @param object An object of class \link{report_species-class}
|
|
| 132 |
#' @param silent Stops displaying the messages. |
|
| 133 |
#' @return report_species with slots filled by user choice |
|
| 134 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 135 |
#' @return An object of class \link{report_species-class} with slots filled from values assigned in \code{envir_stacomi} environment
|
|
| 136 |
#' @aliases charge.report_species |
|
| 137 |
#' @keywords internal |
|
| 138 |
setMethod("charge", signature = signature("report_species"), definition = function(object,
|
|
| 139 |
silent = FALSE) {
|
|
| 140 | 6x |
if (!silent) |
| 141 | 6x |
funout(gettext("Checking objects and launching query\n", domain = "R-stacomiR"))
|
| 142 | 6x |
bilesp <- object |
| 143 | 6x |
if (exists("ref_dc", envir_stacomi)) {
|
| 144 | 6x |
bilesp@dc <- get("ref_dc", envir_stacomi)
|
| 145 |
} else {
|
|
| 146 | ! |
funout(gettext("You need to choose a counting device, clic on validate\n",
|
| 147 | ! |
domain = "R-stacomiR"), arret = TRUE) |
| 148 |
} |
|
| 149 | 6x |
if (exists("ref_taxa", envir_stacomi)) {
|
| 150 | 6x |
bilesp@taxa <- get("ref_taxa", envir_stacomi)
|
| 151 |
} else {
|
|
| 152 | ! |
funout( |
| 153 | ! |
gettext("You need to choose a taxa, clic on validate\n", domain = "R-stacomiR"),
|
| 154 | ! |
arret = TRUE |
| 155 |
) |
|
| 156 |
} |
|
| 157 | 6x |
if (exists("start_year", envir_stacomi)) {
|
| 158 | 6x |
bilesp@start_year <- get("start_year", envir_stacomi)
|
| 159 |
} else {
|
|
| 160 | ! |
funout(gettext("You need to choose the starting year\n", domain = "R-stacomiR"),
|
| 161 | ! |
arret = TRUE) |
| 162 |
} |
|
| 163 | 6x |
if (exists("end_year", envir_stacomi)) {
|
| 164 | 6x |
bilesp@end_year <- get("end_year", envir_stacomi)
|
| 165 |
} else {
|
|
| 166 | ! |
funout(gettext("You need to choose the ending year\n", domain = "R-stacomiR"),
|
| 167 | ! |
arret = TRUE) |
| 168 |
} |
|
| 169 |
|
|
| 170 | 6x |
if (exists("refliste", envir_stacomi)) {
|
| 171 | 6x |
bilesp@split <- get("refliste", envir_stacomi)
|
| 172 |
} else {
|
|
| 173 | ! |
funout(gettext("You need to choose a size class\n", domain = "R-stacomiR"),
|
| 174 | ! |
arret = TRUE) |
| 175 |
} |
|
| 176 | 6x |
assign("bilesp", bilesp, envir_stacomi)
|
| 177 | 6x |
if (!silent) |
| 178 | 6x |
funout(gettext("A report_species object was stored into envir_stacomi environment : write bilesp=get('bilesp',envir_stacomi)",
|
| 179 | 6x |
domain = "R-stacomiR")) |
| 180 | 6x |
return(bilesp) |
| 181 |
}) |
|
| 182 | ||
| 183 | ||
| 184 | ||
| 185 |
#' calcule method for report_species |
|
| 186 |
#' |
|
| 187 |
#' The number will be split according to the split argument passed to the class, e.g. |
|
| 188 |
#' per year or month or week. Data from different DC will be grouped. Counts are given per taxa, |
|
| 189 |
#' unless there are several stages, in which case the counts correspond to taxa + stage. |
|
| 190 |
#' @param object An object of class \link{report_species-class}
|
|
| 191 |
#' @param silent Boolean, if TRUE, information messages are not displayed, only warnings and errors |
|
| 192 |
#' @return An object of class \link{report_species-class} with calcdata slot filled.
|
|
| 193 |
#' @aliases calcule.report_species |
|
| 194 |
setMethod("calcule", signature = signature("report_species"), definition = function(object,
|
|
| 195 |
silent = FALSE) {
|
|
| 196 | 5x |
bilesp <- object |
| 197 | 5x |
DC = as.numeric(bilesp@dc@dc_selected) |
| 198 |
# update of refliste which does not need calcul button pushed |
|
| 199 | 5x |
tableEspeces = bilesp@data |
| 200 | 5x |
if (nrow(tableEspeces) == 0) |
| 201 | 5x |
funout(gettext("No fish in the database for this period\n", domain = "R-stacomiR"),
|
| 202 | 5x |
arret = TRUE) |
| 203 | 5x |
tableEspeces$taxa_stage = paste(tableEspeces$tax_nom_latin, tableEspeces$std_libelle, |
| 204 | 5x |
sep = "_") |
| 205 |
# only keeping taxa stage for species with several stages |
|
| 206 | 5x |
nbstage = tapply(tableEspeces$tax_nom_latin, tableEspeces$taxa_stage, function(X) (length(unique(X)))) |
| 207 |
# we currently have taxa+stage, below this is replaces with taxa unless |
|
| 208 |
# there are more than one stage per species |
|
| 209 | 5x |
if (length(nbstage[nbstage > 1]) > 0) {
|
| 210 | ! |
les_multiples = names(nbstage[nbstage > 1]) |
| 211 | ! |
tableEspeces[!tableEspeces$taxa_stage %in% les_multiples, "taxa_stage"] <- tableEspeces$tax_nom_latin[!tableEspeces$taxa_stage %in% |
| 212 | ! |
les_multiples] |
| 213 | 5x |
} else tableEspeces$taxa_stage <- tableEspeces$tax_nom_latin |
| 214 | 5x |
if (min(tableEspeces$lot_effectif) < 0) {
|
| 215 | 5x |
if (!silent) |
| 216 | 5x |
funout(gettext("Some negative counts are transformed into positive ones\n",
|
| 217 | 5x |
domain = "R-stacomiR")) |
| 218 | 5x |
tableEspeces$lot_effectif = abs(tableEspeces$lot_effectif) |
| 219 |
} |
|
| 220 | 5x |
sumEspeces = switch(bilesp@split@selectedvalue, year = as.data.frame(xtabs(lot_effectif ~ |
| 221 | 5x |
taxa_stage + annee, data = tableEspeces)), month = as.data.frame(xtabs(lot_effectif ~ |
| 222 | 5x |
taxa_stage + mois, data = tableEspeces)), week = as.data.frame(xtabs(lot_effectif ~ |
| 223 | 5x |
taxa_stage + semaine, data = tableEspeces)), none = as.data.frame(xtabs(lot_effectif ~ |
| 224 | 5x |
taxa_stage, data = tableEspeces))) |
| 225 | 5x |
colnames(sumEspeces)[colnames(sumEspeces) == "Freq"] <- "Effectif" # pas forcement le m nb de colonnes |
| 226 | 5x |
if (bilesp@split@selectedvalue != "none") {
|
| 227 | 5x |
colnames(sumEspeces)[2] <- bilesp@split@selectedvalue |
| 228 |
} |
|
| 229 | 5x |
bilesp@calcdata <- sumEspeces |
| 230 | 5x |
assign("bilesp", bilesp, envir_stacomi)
|
| 231 | 5x |
return(bilesp) |
| 232 |
}) |
|
| 233 | ||
| 234 |
#' Plot method for report_species |
|
| 235 |
#' |
|
| 236 |
#' @param x An object of class \link{report_species-class}
|
|
| 237 |
#' @param plot.type Default pie |
|
| 238 |
#' #' \itemize{
|
|
| 239 |
#' \item{plot.type='pie': A pie}'
|
|
| 240 |
#' \item{plot.type='barchart' : A barchart}
|
|
| 241 |
#' } |
|
| 242 |
#' @param color Default NULL, a vector of colors of length corresponding to the number of taxa-stage |
|
| 243 |
#' different values, use unique(bilesp@calcdata$taxa_stage) to get that number. The color applies to both |
|
| 244 |
#' pie and barchart plots |
|
| 245 |
#' @param silent Stops displaying the messages |
|
| 246 |
#' @return Nothing, called for producing plots |
|
| 247 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 248 |
#' @aliases plot.reportreport_species |
|
| 249 |
#' @export |
|
| 250 |
setMethod("plot", signature(x = "report_species", y = "missing"), definition = function(x,
|
|
| 251 |
plot.type = "pie", color = NULL, silent = FALSE) {
|
|
| 252 | 3x |
bilesp <- x |
| 253 | 3x |
if (nrow(bilesp@calcdata) == 0) |
| 254 | 3x |
stop("No data in the calcdata slot, did you forget to run calculations ?")
|
| 255 | 3x |
nb = length(unique(bilesp@calcdata$taxa_stage)) |
| 256 | 3x |
g <- ggplot(bilesp@calcdata) |
| 257 | 3x |
g <- g + geom_col(aes(x = "", y = Effectif, fill = taxa_stage)) + ggtitle(paste("report Especes, DC",
|
| 258 | 3x |
str_c(bilesp@dc@dc_selected, collapse = "+"), bilesp@start_year@year_selected, |
| 259 | 3x |
"=>", bilesp@end_year@year_selected)) + xlab("") + ylab(gettext("Number",
|
| 260 | 3x |
domain = "R-stacomiR")) |
| 261 |
# theme(axis.line.x=element_line('none'))+theme(axis.title.x=
|
|
| 262 |
# element_text('none'))
|
|
| 263 | 3x |
if (bilesp@split@selectedvalue != "none") {
|
| 264 | 3x |
facet <- switch(bilesp@split@selectedvalue, year = as.formula(~year), month = as.formula(~month), |
| 265 | 3x |
week = as.formula(~week)) |
| 266 | 3x |
g <- g + facet_wrap(facet, scales = "fixed") |
| 267 |
} |
|
| 268 | 3x |
if (is.null(color)) {
|
| 269 | 2x |
if (nb <= 8) {
|
| 270 | ! |
g <- g + scale_fill_brewer(palette = "Accent", name = gettext("Taxa-stage",
|
| 271 | ! |
domain = "R-stacomiR")) |
| 272 | 2x |
} else if (nb <= 12) {
|
| 273 | ! |
p <- g + scale_fill_brewer(palette = "Set3", name = gettext("Taxa-stage",
|
| 274 | ! |
domain = "R-stacomiR")) |
| 275 |
} else {
|
|
| 276 | 2x |
g <- g + scale_fill_manual(values = grDevices::rainbow(nb), name = gettext("Taxa-stage",
|
| 277 | 2x |
domain = "R-stacomiR")) |
| 278 |
} |
|
| 279 |
} else {
|
|
| 280 |
# color is not null |
|
| 281 | 1x |
if (length(color) != nb) |
| 282 | 1x |
stop(gettextf("The vector of color should be of length %s", domain = "R-stacomiR",
|
| 283 | 1x |
nb)) |
| 284 | 1x |
g <- g + scale_fill_manual(values = color, gettext("Taxa-stage", domain = "R-stacomiR"))
|
| 285 |
} |
|
| 286 | 3x |
if (plot.type == "barplot") {
|
| 287 | 2x |
print(g) |
| 288 | 2x |
assign("g", g, envir = envir_stacomi)
|
| 289 | 3x |
} else if (plot.type == "pie") {
|
| 290 | 1x |
g <- g + coord_polar(theta = "y", start = pi) + xlab("") + ylab("")
|
| 291 | 1x |
print(g) |
| 292 | 1x |
assign("g", g, envir = envir_stacomi)
|
| 293 |
} else {
|
|
| 294 | ! |
funout(gettext("plot.type should be one of barplot or pie", domain = "R-stacomiR"),
|
| 295 | ! |
arret = TRUE) |
| 296 |
} |
|
| 297 | 3x |
if (!silent) |
| 298 | 3x |
funout(gettext("the object g has been assigned to envir_stacomi", domain = "R-stacomiR"))
|
| 299 |
|
|
| 300 | 3x |
return(invisible(NULL)) |
| 301 |
}) |
|
| 302 | ||
| 303 | ||
| 304 | ||
| 305 |
#' summary for report_species |
|
| 306 |
#' |
|
| 307 |
#' generate csv and html output in the user data directory |
|
| 308 |
#' @param object An object of class \code{\link{report_species-class}}
|
|
| 309 |
#' @param silent Should the program stay silent or display messages, default FALSE |
|
| 310 |
#' @return nothing, but writes summary in \code{get("datawd", envir = envir_stacomi)}, and prints output
|
|
| 311 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 312 |
#' @aliases summary.report_species |
|
| 313 |
#' @export |
|
| 314 |
setMethod("summary", signature = signature(object = "report_species"), definition = function(object,
|
|
| 315 |
silent = FALSE) {
|
|
| 316 | 1x |
bilesp <- object |
| 317 | 1x |
if (nrow(bilesp@calcdata) == 0) |
| 318 | 1x |
stop("No data in the calcdata slot, did you forget to run calculations ?")
|
| 319 | 1x |
loc <- str_c(str_c(bilesp@dc@dc_selected, collapse = "+"), bilesp@start_year@year_selected, |
| 320 | 1x |
bilesp@end_year@year_selected, sep = "_") |
| 321 |
|
|
| 322 | 1x |
path = file.path(normalizePath(path.expand(get("datawd", envir = envir_stacomi))),
|
| 323 | 1x |
paste("tableEspece", loc, ".csv", sep = ""), fsep = "\\")
|
| 324 | 1x |
res <- tryCatch( |
| 325 | 1x |
write.table(bilesp@calcdata, path, row.names = TRUE, col.names = TRUE, sep = ";", |
| 326 | 1x |
append = FALSE), |
| 327 | 1x |
error = function(e) e, |
| 328 | 1x |
finally = |
| 329 | 1x |
if (!silent) {
|
| 330 | 1x |
funout(gettextf("writing of %s \n", path))
|
| 331 | 1x |
funout(gettextf("attention, negative numbers were transformed into positive numbers"))
|
| 332 |
}) |
|
| 333 | 1x |
if (inherits(res, "simpleError")) {
|
| 334 | ! |
warnings("The table could not be reported, please change the working directory datawd with options(stacomiR.path='path/to/directory'")
|
| 335 |
} |
|
| 336 |
|
|
| 337 |
}) |
|
| 338 |
| 1 |
#' Class 'ref_parqual' |
|
| 2 |
#' |
|
| 3 |
#' Class enabling to load the list of qualitative parameters and to select one |
|
| 4 |
#' of them. It inherits from 'ref_par', uses its 'choice' method |
|
| 5 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 6 |
#' @slot valqual='data.frame' the list of qualitative parameters |
|
| 7 |
#' @include ref_par.R |
|
| 8 |
#' @family referential objects |
|
| 9 |
setClass(Class = "ref_parqual", representation = representation(valqual = "data.frame"), |
|
| 10 |
contains = "ref_par") |
|
| 11 | ||
| 12 |
#' Loading method for Reparqual referential objects |
|
| 13 |
#' @param object An object of class \link{ref_parqual-class}
|
|
| 14 |
#' @return An S4 object of class ref_parqual |
|
| 15 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 16 |
#' @examples |
|
| 17 |
#' \dontrun{
|
|
| 18 |
#' object=new('ref_parqual')
|
|
| 19 |
#' charge(object) |
|
| 20 |
#' } |
|
| 21 |
setMethod("charge", signature = signature("ref_parqual"), definition = function(object) {
|
|
| 22 | 1x |
requete = new("RequeteDB")
|
| 23 | 1x |
requete@sql = "select par_code, par_nom, par_unite, par_nature, par_definition, qal_valeurs_possibles from ref.tg_parametre_par |
| 24 | 1x |
INNER JOIN ref.tr_parametrequalitatif_qal ON tr_parametrequalitatif_qal.qal_par_code::text = tg_parametre_par.par_code::text" |
| 25 | 1x |
requete <- stacomirtools::query(requete) |
| 26 |
# funout(gettext('The query to load parameters is done
|
|
| 27 |
# \n',domain='R-stacomiR')) |
|
| 28 | 1x |
object@data <- requete@query |
| 29 | 1x |
return(object) |
| 30 |
}) |
|
| 31 | ||
| 32 |
#' Loading method for Reparqual referential objects searching only those parameters existing for a DC, a Taxon, and a stage |
|
| 33 |
#' @param object An object of class \link{ref_parqual-class}
|
|
| 34 |
#' @param dc_selected The dc set in the report object |
|
| 35 |
#' @param taxa_selected The taxa set in the report object |
|
| 36 |
#' @param stage_selected The stage set in the report object |
|
| 37 |
#' @return An S4 object of class \link{ref_parqual-class}
|
|
| 38 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 39 |
#' @examples |
|
| 40 |
#' \dontrun{
|
|
| 41 |
#' dc_selected=6 |
|
| 42 |
#' taxa_selected=2038 |
|
| 43 |
#' stage_selected='AGJ' |
|
| 44 |
#' object=new('ref_parqual')
|
|
| 45 |
#' charge_with_filter(object,dc_selected,taxa_selected,stage_selected) |
|
| 46 |
#' } |
|
| 47 |
setMethod("charge_with_filter", signature = signature("ref_parqual"), definition = function(object,
|
|
| 48 |
dc_selected, taxa_selected, stage_selected) {
|
|
| 49 | 8x |
requete = new("RequeteDBwhere")
|
| 50 | 8x |
requete@select = paste("SELECT DISTINCT ON (par_code) par_code, par_nom, par_unite, par_nature, par_definition, qal_valeurs_possibles", " FROM ",
|
| 51 | 8x |
get_schema(), "tg_dispositif_dis", " JOIN ", get_schema(), "t_dispositifcomptage_dic on dis_identifiant=dic_dis_identifiant", |
| 52 | 8x |
" JOIN ", get_schema(), "t_operation_ope on ope_dic_identifiant=dic_dis_identifiant", |
| 53 | 8x |
" JOIN ", get_schema(), "t_lot_lot on lot_ope_identifiant=ope_identifiant", |
| 54 | 8x |
" JOIN ", get_schema(), "tj_caracteristiquelot_car on car_lot_identifiant=lot_identifiant", |
| 55 | 8x |
" JOIN ref.tg_parametre_par on par_code=car_par_code", " JOIN ref.tr_parametrequalitatif_qal ON tr_parametrequalitatif_qal.qal_par_code::text = tg_parametre_par.par_code::text", |
| 56 | 8x |
sep = "") |
| 57 | 8x |
requete@where = paste("where dis_identifiant in ", vector_to_listsql(dc_selected))
|
| 58 | 8x |
requete@and = paste("and lot_tax_code in ", vector_to_listsql(taxa_selected),
|
| 59 | 8x |
" and lot_std_code in ", vector_to_listsql(stage_selected), sep = "") |
| 60 | 8x |
requete@order_by = "ORDER BY par_code" |
| 61 | 8x |
requete <- stacomirtools::query(requete) |
| 62 | 8x |
object@data <- requete@query |
| 63 | 8x |
return(object) |
| 64 |
}) |
|
| 65 | ||
| 66 |
#' Loads an additional dataset |
|
| 67 |
#' this method is loaded to obtain the possible values of a qualitative parameter. Here data only contains one line |
|
| 68 |
#' @param object An object of class \link{ref_parqual-class}
|
|
| 69 |
#' @return An S4 object of class \link{ref_parqual-class} with the valqual slot filled
|
|
| 70 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 71 |
#' @examples |
|
| 72 |
#' \dontrun{
|
|
| 73 |
#' dc_selected=6 |
|
| 74 |
#'taxa_selected=2038 |
|
| 75 |
#' stage_selected='AGJ' |
|
| 76 |
#' object=new('ref_parqual')
|
|
| 77 |
#' object<-charge(object) |
|
| 78 |
#' charge_complement(object) |
|
| 79 |
#' } |
|
| 80 |
setMethod("charge_complement", signature = signature("ref_parqual"), definition = function(object) {
|
|
| 81 | 2x |
requete = new("RequeteDB")
|
| 82 | 2x |
requete@sql = paste("select * from ref.tr_valeurparametrequalitatif_val", " WHERE val_qal_code in ",
|
| 83 | 2x |
vector_to_listsql(object@par_selected), " ORDER BY val_rang", sep = "") |
| 84 | 1x |
requete <- stacomirtools::query(requete) |
| 85 |
# funout(gettext('The query to load parameters is done
|
|
| 86 |
# \n',domain='R-stacomiR')) |
|
| 87 | 1x |
object@valqual <- requete@query |
| 88 | 1x |
return(object) |
| 89 |
}) |
|
| 90 | ||
| 91 |
| 1 |
#' function to print and save statistics in .csv and .html formats for report_mig and report_mig_mult class |
|
| 2 |
#' @param tableau A table with the following columns : No.pas,debut_pas,fin_pas, |
|
| 3 |
#' ope_dic_identifiant,lot_tax_code,lot_std_code,type_de_quantite,MESURE,CALCULE, |
|
| 4 |
#' EXPERT,PONCTUEL,Effectif_total,taux_d_echappement,coe_valeur_coefficient |
|
| 5 |
#' @note this function is intended to be called from within the summary method |
|
| 6 |
#' @param time.sequence Passed from report_mig or report_mig_mult |
|
| 7 |
#' @param taxa Taxa |
|
| 8 |
#' @param stage The Stage |
|
| 9 |
#' @param DC The counting device |
|
| 10 |
#' @param resum A summary table generated by funstat |
|
| 11 |
#' @param silent If TRUE, all messages turned off (except warnings and errors) |
|
| 12 |
#' @return No return value, called for side effects |
|
| 13 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 14 |
funtable = function(tableau, time.sequence, taxa, stage, DC, resum, silent) {
|
|
| 15 | 8x |
annee = paste(unique(strftime(as.POSIXlt(time.sequence), "%Y")), collapse = ",") |
| 16 | 8x |
tableau$debut_pas <- as.character(tableau$debut_pas) |
| 17 | 8x |
path1 = file.path(path.expand(get("datawd", envir = envir_stacomi)), paste(DC,
|
| 18 | 8x |
"_", taxa, "_", stage, "_", annee, ".csv", sep = ""), fsep = "/") |
| 19 | 8x |
path1html = file.path(path.expand(get("datawd", envir = envir_stacomi)), paste(DC,
|
| 20 | 8x |
"_", taxa, "_", stage, "_", annee, ".html", sep = ""), fsep = "/") |
| 21 | 8x |
res <- tryCatch( |
| 22 | 8x |
write.table(tableau, file = path1, row.names = FALSE, col.names = TRUE, sep = ";") |
| 23 | 8x |
, error = function(e) e, |
| 24 | 8x |
finally = |
| 25 | 8x |
if (!silent) funout(gettext("Writing of %s \n", path1, domain = "R-stacomiR"))
|
| 26 |
|
|
| 27 |
) |
|
| 28 |
|
|
| 29 |
|
|
| 30 | ||
| 31 | 8x |
res <- tryCatch( |
| 32 | 8x |
suppressWarnings(funhtml(data = tableau, caption = paste(DC, "_", taxa, "_", |
| 33 | 8x |
stage, "_", annee, ".csv", sep = ""), top = TRUE, outfile = path1html, clipboard = FALSE, |
| 34 | 8x |
append = FALSE, digits = 2)), |
| 35 | 8x |
error = function(e) e, |
| 36 | 8x |
finally = |
| 37 | 8x |
if (!silent) funout(gettextf("writing of %s\n", path1html))
|
| 38 |
|
|
| 39 |
) |
|
| 40 | 8x |
if (!is.null(resum)) {
|
| 41 | 6x |
path2 = file.path(path.expand(get("datawd", envir = envir_stacomi)), paste("res",
|
| 42 | 6x |
DC, "_", taxa, "_", stage, "_", annee, ".csv", sep = ""), fsep = "/") |
| 43 | 6x |
resum1 <- resum |
| 44 | 6x |
resum$id = rownames(resum) |
| 45 | 6x |
path2html = file.path(path.expand(get("datawd", envir = envir_stacomi)),
|
| 46 | 6x |
paste("res", annee, ".html", sep = ""), fsep = "/")
|
| 47 | 6x |
res <- tryCatch({
|
| 48 | 6x |
write.table(resum1, path2, row.names = FALSE, col.names = TRUE, sep = ";") |
| 49 | 6x |
suppressWarnings(funhtml(data = resum, caption = paste("Sommes mensuelles",
|
| 50 | 6x |
annee), top = TRUE, outfile = path2html, clipboard = FALSE, append = TRUE, |
| 51 | 6x |
digits = 2)) |
| 52 | 6x |
}, error = function(e) e, |
| 53 | 6x |
finally ={
|
| 54 | 6x |
if (!silent) funout(gettextf("writing of %s\n", path2))
|
| 55 | 6x |
if (!silent) funout(gettextf("writing of %s\n", path2html))
|
| 56 |
}) |
|
| 57 | 6x |
if (inherits(res, "simpleError")) {
|
| 58 | ! |
warnings("The table could not be reported, please change the working directory datawd with options(stacomiR.path='path/to/directory'")
|
| 59 |
} |
|
| 60 |
} |
|
| 61 | 8x |
return(invisible(NULL)) |
| 62 |
} |
|
| 63 | ||
| 64 |
| 1 |
#' ref_textbox referencial class |
|
| 2 |
#' |
|
| 3 |
#' allows to a put a value within a glabel |
|
| 4 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 5 |
#' @slot title='character' the title of the box giving the possible choices |
|
| 6 |
#' @slot labels the logical parameters choice |
|
| 7 |
#' @slot checked a vector |
|
| 8 |
setClass(Class = "ref_textbox", representation = representation(title = "character", |
|
| 9 |
label = "character")) |
|
| 10 | ||
| 11 |
#' Loading method for ref_textbox referential objects |
|
| 12 |
#' @param object An object of class \link{ref_textbox-class}
|
|
| 13 |
#' @param title A title for the frame |
|
| 14 |
#' @param label A label for the TextBox |
|
| 15 |
#' @return An S4 object of class \link{ref_textbox-class}
|
|
| 16 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 17 |
#' @examples |
|
| 18 |
#' \dontrun{
|
|
| 19 |
#' object=new('ref_textbox')
|
|
| 20 |
#' charge(object,title='un titre',label='20') |
|
| 21 |
#' } |
|
| 22 |
setMethod("charge", signature = signature("ref_textbox"), definition = function(object,
|
|
| 23 |
title, label) {
|
|
| 24 | ! |
object@title = title |
| 25 | ! |
object@label = label |
| 26 | ! |
return(object) |
| 27 |
}) |
|
| 28 | ||
| 29 | ||
| 30 |
#' Choice_c method for ref_textbox referential objects |
|
| 31 |
#' |
|
| 32 |
#' @param object An object of class \link{ref_textbox-class}
|
|
| 33 |
#' @param value The value to set |
|
| 34 |
#' @param nomassign The name with which the object will be assigned in envir_stacomi |
|
| 35 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 36 |
#' @return An S4 object of class \link{ref_textbox-class} label selected
|
|
| 37 |
setMethod("choice_c", signature = signature("ref_textbox"), definition = function(object,
|
|
| 38 |
value, nomassign = "ref_textbox") {
|
|
| 39 | 8x |
object@label <- value |
| 40 | 8x |
assign(nomassign, object, envir_stacomi) |
| 41 | 8x |
return(object) |
| 42 |
}) |
| 1 |
#' Migration report for one DC, one species and one stage |
|
| 2 |
#' |
|
| 3 |
#' This class performs a migration summary. A migration monitoring operation can correspond to a single |
|
| 4 |
#' horodate (in the case of some video monitoring operation) or comprise a period which does not necessarily |
|
| 5 |
#' span a full day. The daily migration is calculated by splitting the operation between days, and the migration is either |
|
| 6 |
#' grouped or split according to the lenth of the different time spans. |
|
| 7 |
#' @include ref_taxa.R |
|
| 8 |
#' @include ref_stage.R |
|
| 9 |
#' @include ref_timestep_daily.R |
|
| 10 |
#' @include report_df.R |
|
| 11 |
#' @include report_dc.R |
|
| 12 |
#' @include report_ope.R |
|
| 13 |
#' @slot dc Object of class \link{ref_dc-class}: the control device
|
|
| 14 |
#' @slot taxa Object of class \link{ref_taxa-class}: the species
|
|
| 15 |
#' @slot stage Object of class \link{ref_stage-class} : the stage of the fish
|
|
| 16 |
#' @slot timestep Object of class \link{ref_timestep_daily-class} : the time step
|
|
| 17 |
#' constrained to daily value and 365 days |
|
| 18 |
#' @slot data Object of class \code{data.frame} with data filled in from the connect method
|
|
| 19 |
#' @slot calcdata A "list" of calculated daily data, one per dc, filled in by the calcule method |
|
| 20 |
#' @slot coef_conversion A data.frame of daily weight to number conversion coefficients, filled in by the connect |
|
| 21 |
#' method if any weight are found in the data slot. |
|
| 22 |
#' @slot time.sequence Object of class \code{POSIXct} : a time sequence of days generated by the calcule method
|
|
| 23 |
#' @note In practise, the report_mig class uses methods (calcule, connect...) from the more elaborate \link{report_mig_mult-class}
|
|
| 24 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 25 |
#' @family report Objects |
|
| 26 |
#' @keywords classes |
|
| 27 |
#' @aliases report_mig |
|
| 28 |
#' @example inst/examples/report_mig-example.R |
|
| 29 |
#' @export |
|
| 30 |
setClass( |
|
| 31 |
Class = "report_mig", |
|
| 32 |
representation = |
|
| 33 |
representation( |
|
| 34 |
dc = "ref_dc", |
|
| 35 |
taxa = "ref_taxa", |
|
| 36 |
stage = "ref_stage", |
|
| 37 |
timestep = "ref_timestep_daily", |
|
| 38 |
data = "data.frame", |
|
| 39 |
calcdata = "list", |
|
| 40 |
coef_conversion = "data.frame", |
|
| 41 |
time.sequence = "POSIXct" |
|
| 42 |
), |
|
| 43 |
prototype = prototype( |
|
| 44 |
dc = new("ref_dc"),
|
|
| 45 |
taxa = new("ref_taxa"),
|
|
| 46 |
stage = new("ref_stage"),
|
|
| 47 |
timestep = new("ref_timestep_daily"),
|
|
| 48 |
data = data.frame(), |
|
| 49 |
calcdata = list(), |
|
| 50 |
coef_conversion = data.frame(), |
|
| 51 |
time.sequence = as.POSIXct(Sys.time()) |
|
| 52 |
) |
|
| 53 |
) |
|
| 54 |
# report_mig= new("report_mig")
|
|
| 55 | ||
| 56 |
setValidity("report_mig", function(object)
|
|
| 57 |
{
|
|
| 58 |
rep1 = length(object@dc) == 1 |
|
| 59 |
rep2 = length(object@taxa) == 1 |
|
| 60 |
rep3 = length(object@stage) == 1 |
|
| 61 |
rep3 = length(object@timestep) == 1 |
|
| 62 |
rep4 = (object@timestep@nb_step == 365 | |
|
| 63 |
object@timestep@nb_step == 366) # constraint 365 to 366 days |
|
| 64 |
rep5 = as.numeric(strftime(object@timestep@dateDebut, '%d')) == 1 # contrainte : depart = 1er janvier |
|
| 65 |
rep6 = as.numeric(strftime(object@timestep@dateDebut, '%m')) == 1 |
|
| 66 |
return(ifelse(rep1 & |
|
| 67 |
rep2 & |
|
| 68 |
rep3 & |
|
| 69 |
rep4 & |
|
| 70 |
rep5 & |
|
| 71 |
rep6 , TRUE , c(1:6)[!c(rep1, rep2, rep3, rep4, rep5, rep6)])) |
|
| 72 |
}) |
|
| 73 | ||
| 74 |
#deprecated0.6 |
|
| 75 |
##' handler for calculations report_mig |
|
| 76 |
##' |
|
| 77 |
##' internal use |
|
| 78 |
##' @param h handler |
|
| 79 |
##' @param ... additional parameters |
|
| 80 |
##' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 81 |
##' @keywords internal |
|
| 82 |
#h_report_migcalc=function(h,...){
|
|
| 83 |
# if (exists("report_mig",envir_stacomi)) {
|
|
| 84 |
# report_mig<-get("report_mig",envir_stacomi)
|
|
| 85 |
# } else {
|
|
| 86 |
# funout(gettext("You need to launch computation first, clic on calc\n",domain="R-stacomiR"),arret=TRUE)
|
|
| 87 |
# } |
|
| 88 |
# report_mig<-charge(report_mig) |
|
| 89 |
# report_mig<-connect(report_mig) |
|
| 90 |
# report_mig<-calcule(report_mig) |
|
| 91 |
#} |
|
| 92 | ||
| 93 |
#' connect method for report_mig |
|
| 94 |
#' |
|
| 95 |
#' |
|
| 96 |
#' uses the report_mig_mult method |
|
| 97 |
#' @param object An object of class \link{report_mig-class}
|
|
| 98 |
#' @param silent Boolean default FALSE, if TRUE information messages not displayed |
|
| 99 |
#' @return report_mig with slot \code{@data} filled from the database
|
|
| 100 |
#' @aliases connect.report_mig |
|
| 101 |
setMethod( |
|
| 102 |
"connect", |
|
| 103 |
signature = signature("report_mig"),
|
|
| 104 |
definition = function(object, silent = FALSE) {
|
|
| 105 | 11x |
report_mig <- object |
| 106 | 11x |
report_mig_mult <- as(report_mig, "report_mig_mult") |
| 107 | 11x |
report_mig_mult <- connect(report_mig_mult, silent = silent) |
| 108 | 11x |
report_mig@data <- report_mig_mult@data |
| 109 | 11x |
report_mig@coef_conversion <- report_mig_mult@coef_conversion |
| 110 | 11x |
return(report_mig) |
| 111 |
} |
|
| 112 |
) |
|
| 113 |
#' command line interface for report_mig class |
|
| 114 |
#' |
|
| 115 |
#' The choice_c method fills in the data slot for ref_dc, ref_taxa, ref_stage, and refref_timestep_daily and then |
|
| 116 |
#' uses the choice_c methods of these object to select the data. |
|
| 117 |
#' @param object An object of class \link{report_mig-class}
|
|
| 118 |
#' @param dc A numeric or integer, the code of the dc, coerced to integer,see \link{choice_c,ref_dc-method}
|
|
| 119 |
#' @param taxa Either a species name in latin or the SANDRE code for species (ie 2038=Anguilla anguilla), |
|
| 120 |
#' these should match the ref.tr_taxon_tax referential table in the stacomi database, see \link{choice_c,ref_taxa-method}
|
|
| 121 |
#' @param stage A stage code matching the ref.tr_stadedeveloppement_std table in the stacomi database see \link{choice_c,ref_stage-method}
|
|
| 122 |
#' @param datedebut The starting date as a character, formats like \code{\%Y-\%m-\%d} or \code{\%d-\%m-\%Y} can be used as input
|
|
| 123 |
#' @param datefin The finishing date of the report, for this class this will be used to calculate the number of daily steps. |
|
| 124 |
#' @return An object of class \link{report_mig-class} with data selected
|
|
| 125 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 126 |
#' @aliases choice_c.report_mig |
|
| 127 |
setMethod( |
|
| 128 |
"choice_c", |
|
| 129 |
signature = signature("report_mig"),
|
|
| 130 |
definition = function(object, dc, taxa, stage, datedebut, datefin) {
|
|
| 131 |
# code for debug using r_mig example |
|
| 132 |
#report_mig<-r_mig;dc=5;taxa="Liza ramada";stage="IND";datedebut="2015-01-01";datefin="2015-12-31" |
|
| 133 | 12x |
report_mig <- object |
| 134 | 12x |
report_mig@dc = charge(report_mig@dc) |
| 135 |
# loads and verifies the dc |
|
| 136 |
# this will set dc_selected slot |
|
| 137 | 12x |
report_mig@dc <- choice_c(object = report_mig@dc, dc) |
| 138 |
# only taxa present in the report_mig are used |
|
| 139 | 12x |
report_mig@taxa <- |
| 140 | 12x |
charge_with_filter(object = report_mig@taxa, report_mig@dc@dc_selected) |
| 141 | 12x |
report_mig@taxa <- choice_c(report_mig@taxa, taxa) |
| 142 | 12x |
report_mig@stage <- |
| 143 | 12x |
charge_with_filter(object = report_mig@stage, |
| 144 | 12x |
report_mig@dc@dc_selected, |
| 145 | 12x |
report_mig@taxa@taxa_selected) |
| 146 | 12x |
report_mig@stage <- choice_c(report_mig@stage, stage) |
| 147 | 12x |
report_mig@timestep <- |
| 148 | 12x |
choice_c(report_mig@timestep, datedebut, datefin) |
| 149 | 12x |
return(report_mig) |
| 150 |
} |
|
| 151 |
) |
|
| 152 | ||
| 153 |
#' Loads additional data on migration control operations, df (fishway) dc (counting device). |
|
| 154 |
#' |
|
| 155 |
#' this method creates additional classes in envir_stacomi for later use in plot (operations, |
|
| 156 |
#' DF operation, DC operation). So unlike in most report classes where the charge method is only |
|
| 157 |
#' used by the graphical interface, it is necessary to run charge for report_mig. |
|
| 158 |
#' @param object An object of class \code{\link{report_mig-class}}
|
|
| 159 |
#' @param silent Should the program be returning messages |
|
| 160 |
#' @return An object of class \link{report_mig-class} with slots filled from values assigned in \code{envir_stacomi} environment
|
|
| 161 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 162 |
#' @aliases charge.report_mig |
|
| 163 |
setMethod( |
|
| 164 |
"charge", |
|
| 165 |
signature = signature("report_mig"),
|
|
| 166 |
definition = function(object, silent = FALSE) {
|
|
| 167 | 11x |
report_mig <- object |
| 168 |
#report_mig<-r_mig |
|
| 169 |
#pour l'instant ne lancer que si les fenetre sont fermees |
|
| 170 |
# funout(gettext("launching updateplot \n",domain="R-stacomiR"))
|
|
| 171 | 11x |
if (exists("ref_dc", envir_stacomi)) {
|
| 172 | 11x |
report_mig@dc <- get("ref_dc", envir_stacomi)
|
| 173 | 11x |
dc <- report_mig@dc@dc_selected |
| 174 | 11x |
df <- report_mig@dc@data$df[report_mig@dc@data$dc %in% dc] |
| 175 |
} else {
|
|
| 176 | ! |
funout( |
| 177 | ! |
gettext( |
| 178 | ! |
"You need to choose a counting device, clic on validate\n", |
| 179 | ! |
domain = "R-stacomiR" |
| 180 |
), |
|
| 181 | ! |
arret = TRUE |
| 182 |
) |
|
| 183 |
} |
|
| 184 | 11x |
if (exists("ref_taxa", envir_stacomi)) {
|
| 185 | 11x |
report_mig@taxa <- get("ref_taxa", envir_stacomi)
|
| 186 |
} else {
|
|
| 187 | ! |
funout( |
| 188 | ! |
gettext("You need to choose a taxa, clic on validate\n", domain = "R-stacomiR"),
|
| 189 | ! |
arret = TRUE |
| 190 |
) |
|
| 191 |
} |
|
| 192 | 11x |
if (exists("ref_stage", envir_stacomi)) {
|
| 193 | 11x |
report_mig@stage <- get("ref_stage", envir_stacomi)
|
| 194 |
} else |
|
| 195 |
{
|
|
| 196 | ! |
funout( |
| 197 | ! |
gettext("You need to choose a stage, clic on validate\n", domain = "R-stacomiR"),
|
| 198 | ! |
arret = TRUE |
| 199 |
) |
|
| 200 |
} |
|
| 201 | 11x |
if (exists("timestep", envir_stacomi)) {
|
| 202 | 11x |
report_mig@timestep <- get("timestep", envir_stacomi)
|
| 203 |
} else {
|
|
| 204 | ! |
funout( |
| 205 | ! |
gettext( |
| 206 | ! |
"Attention, no time step selected, compunting with default value\n", |
| 207 | ! |
domain = "R-stacomiR" |
| 208 |
), |
|
| 209 | ! |
arret = FALSE |
| 210 |
) |
|
| 211 | ! |
warning( |
| 212 | ! |
gettext( |
| 213 | ! |
"Attention, no time step selected, compunting with default value\n", |
| 214 | ! |
domain = "R-stacomiR" |
| 215 |
) |
|
| 216 |
) |
|
| 217 |
} |
|
| 218 |
|
|
| 219 |
################################# |
|
| 220 |
# loading data for other classes associated with report_mig_mult |
|
| 221 |
################################# |
|
| 222 | 11x |
report_df = new("report_df")
|
| 223 | 11x |
report_dc = new("report_dc")
|
| 224 | 11x |
report_ope = new("report_ope")
|
| 225 | 11x |
assign( |
| 226 | 11x |
"report_dc_date_debut", |
| 227 | 11x |
get("timestep", envir_stacomi)@"dateDebut",
|
| 228 | 11x |
envir_stacomi |
| 229 |
) |
|
| 230 | 11x |
assign("report_dc_date_fin", as.POSIXlt(end_date(get(
|
| 231 | 11x |
"timestep", envir_stacomi |
| 232 | 11x |
))), envir_stacomi) |
| 233 | 11x |
assign( |
| 234 | 11x |
"report_df_date_debut", |
| 235 | 11x |
get("timestep", envir_stacomi)@"dateDebut",
|
| 236 | 11x |
envir_stacomi |
| 237 |
) |
|
| 238 | 11x |
assign("report_df_date_fin", as.POSIXlt(end_date(get(
|
| 239 | 11x |
"timestep", envir_stacomi |
| 240 | 11x |
))), envir_stacomi) |
| 241 | 11x |
assign( |
| 242 | 11x |
"report_ope_date_debut", |
| 243 | 11x |
get("timestep", envir_stacomi)@"dateDebut",
|
| 244 | 11x |
envir_stacomi |
| 245 |
) |
|
| 246 | 11x |
assign("report_ope_date_fin",
|
| 247 | 11x |
as.POSIXlt(end_date(get( |
| 248 | 11x |
"timestep", envir_stacomi |
| 249 |
))), |
|
| 250 | 11x |
envir_stacomi) |
| 251 |
|
|
| 252 | 11x |
report_ope <- charge(report_ope) |
| 253 |
# charge will search for ref_dc (possible multiple choice), report_ope_date_debut |
|
| 254 |
# and report_ope_date_fin in envir_stacomi |
|
| 255 |
# charge will search for ref_dc (possible multiple choice), report_dc_date_debut |
|
| 256 |
# and report_dc_date_fin in envir_stacomi |
|
| 257 | 11x |
report_dc <- charge(report_dc) |
| 258 | 11x |
ref_df = new("ref_df")
|
| 259 | 11x |
ref_df <- charge(ref_df) |
| 260 | 11x |
ref_df <- choice_c(ref_df, df) |
| 261 |
|
|
| 262 | 11x |
assign("ref_df", ref_df, envir = envir_stacomi)
|
| 263 |
|
|
| 264 |
# charge will search for ref_df (possible multiple choice), report_df_date_debut |
|
| 265 |
# and report_df_date_fin in envir_stacomi |
|
| 266 | 11x |
report_df <- charge(report_df) |
| 267 |
# the object are assigned to the envir_stacomi for later use by the connect method |
|
| 268 | 11x |
assign("report_df", report_df, envir = envir_stacomi)
|
| 269 | 11x |
assign("report_dc", report_dc, envir = envir_stacomi)
|
| 270 | 11x |
assign("report_ope", report_ope, envir = envir_stacomi)
|
| 271 | 11x |
return(report_mig) |
| 272 |
} |
|
| 273 |
) |
|
| 274 | ||
| 275 | ||
| 276 |
#' Transforms migration per period to daily migrations, and performs the conversion from weights to number is data |
|
| 277 |
#' are stored as weights (glass eel). |
|
| 278 |
#' |
|
| 279 |
#' The calculation must be launched once data are filled by the connect method. Currently the negative argument |
|
| 280 |
#' has no effect. |
|
| 281 |
#' @param object An object of class \code{\link{report_mig-class}}
|
|
| 282 |
#' @param negative a boolean indicating if a separate sum must be done for positive and negative values, if true, positive and negative counts return |
|
| 283 |
#' different rows |
|
| 284 |
#' @param silent Boolean, if TRUE, information messages are not displayed, only warnings and errors |
|
| 285 |
#' @note The class report_mig does not handle escapement rates nor |
|
| 286 |
#' 'devenir' i.e. the destination of the fishes. |
|
| 287 |
#' @return report_mig with calcdata slot filled. It is a list with one element per counting device containing |
|
| 288 |
#' \describe{
|
|
| 289 |
#' \item{method}{In the case of instantaneous periods (video counting) the sum of daily values is done by the \link{fun_report_mig_mult} method and the value indicated in method is "sum".
|
|
| 290 |
#' If any migration monitoring period is longer than a day, then the migration is split using the \link{fun_report_mig_mult_overlaps} function and the value indicated in the
|
|
| 291 |
#' method is "overlaps" as the latter method uses the overlap package to split migration period.} |
|
| 292 |
#' \item{data}{the calculated data.}
|
|
| 293 |
#' \item{contient_poids}{A boolean which indicates, in the case of glass eel, that the function \link{fun_weight_conversion} has been run to convert the weights to numbers using the weight
|
|
| 294 |
#' to number coefficients in the database (see link{report_ge_weight}).}
|
|
| 295 |
#' \item{negative}{A parameter indicating if negative migration (downstream in the case of upstream migration devices) have been converted to positive numbers,
|
|
| 296 |
#' not developed yet}} |
|
| 297 |
#' @aliases calcule.report_mig |
|
| 298 |
setMethod( |
|
| 299 |
"calcule", |
|
| 300 |
signature = signature("report_mig"),
|
|
| 301 |
definition = function(object, |
|
| 302 |
negative = FALSE, |
|
| 303 |
silent = FALSE) {
|
|
| 304 |
#report_mig<-r_mig |
|
| 305 |
#report_mig<-bM_Arzal_civ |
|
| 306 |
#negative=FALSE;silent=FALSE |
|
| 307 | 13x |
if (!silent) {
|
| 308 | ! |
funout(gettext("Starting migration summary ... be patient\n", domain = "R-stacomiR"))
|
| 309 |
} |
|
| 310 | 13x |
report_mig <- object |
| 311 |
|
|
| 312 | 13x |
if (nrow(report_mig@data) > 0) {
|
| 313 |
# report_mig@data$time.sequence=difftime(report_mig@data$ope_date_fin, |
|
| 314 |
# report_mig@data$ope_date_debut, |
|
| 315 |
# units="days") |
|
| 316 | 11x |
debut = report_mig@timestep@dateDebut |
| 317 | 11x |
fin = end_date(report_mig@timestep) |
| 318 | 11x |
time.sequence <- seq.POSIXt( |
| 319 | 11x |
from = debut, |
| 320 | 11x |
to = fin, |
| 321 | 11x |
by = as.numeric(report_mig@timestep@step_duration) |
| 322 |
) |
|
| 323 | 11x |
report_mig@time.sequence <- time.sequence |
| 324 | 11x |
lestableaux <- list() |
| 325 | 11x |
datasub <- report_mig@data |
| 326 | 11x |
dic <- unique(report_mig@data$ope_dic_identifiant) |
| 327 | 11x |
stopifnot(length(dic) == 1) |
| 328 | 11x |
datasub$duree = difftime(datasub$ope_date_fin, datasub$ope_date_debut, units = |
| 329 | 11x |
"days") |
| 330 | 11x |
if (any(datasub$duree > (report_mig@timestep@step_duration / 86400))) {
|
| 331 |
#---------------------- |
|
| 332 |
# reports with overlaps |
|
| 333 |
#---------------------- |
|
| 334 | 4x |
data <- |
| 335 | 4x |
fun_report_mig_mult_overlaps( |
| 336 | 4x |
time.sequence = time.sequence, |
| 337 | 4x |
datasub = datasub, |
| 338 | 4x |
negative = negative |
| 339 |
) |
|
| 340 |
# to remain compatible with report_mig |
|
| 341 | 4x |
data$taux_d_echappement = -1 |
| 342 | 4x |
lestableaux[[stringr::str_c("dc_", dic)]][["data"]] <- data
|
| 343 | 4x |
lestableaux[[stringr::str_c("dc_", dic)]][["method"]] <-
|
| 344 | 4x |
"overlaps" |
| 345 | 4x |
contient_poids <- "poids" %in% datasub$type_de_quantite |
| 346 | 4x |
lestableaux[[stringr::str_c("dc_", dic)]][["contient_poids"]] <-
|
| 347 | 4x |
contient_poids |
| 348 | 4x |
lestableaux[[stringr::str_c("dc_", dic)]][["negative"]] <-
|
| 349 | 4x |
negative |
| 350 | 4x |
if (contient_poids) {
|
| 351 | 1x |
coe <- |
| 352 | 1x |
report_mig@coef_conversion[, c("coe_date_debut", "coe_valeur_coefficient")]
|
| 353 | 1x |
data$coe_date_debut <- as.Date(data$debut_pas) |
| 354 | 1x |
data <- merge(data, coe, by = "coe_date_debut") |
| 355 | 1x |
data <- data[, -1] # removing coe_date_debut |
| 356 | 1x |
data <- |
| 357 | 1x |
fun_weight_conversion(tableau = data, |
| 358 | 1x |
time.sequence = report_mig@time.sequence, |
| 359 | 1x |
silent) |
| 360 |
} |
|
| 361 |
|
|
| 362 | 4x |
lestableaux[[stringr::str_c("dc_", dic)]][["data"]] <- data
|
| 363 |
|
|
| 364 |
} else {
|
|
| 365 |
#---------------------- |
|
| 366 |
#report simple |
|
| 367 |
#---------------------- |
|
| 368 | 7x |
data <- |
| 369 | 7x |
fun_report_mig_mult( |
| 370 | 7x |
time.sequence = time.sequence, |
| 371 | 7x |
datasub = datasub, |
| 372 | 7x |
negative = negative |
| 373 |
) |
|
| 374 | 7x |
data$taux_d_echappement = -1 |
| 375 | 7x |
contient_poids <- "poids" %in% datasub$type_de_quantite |
| 376 | 7x |
if (contient_poids) {
|
| 377 | ! |
coe <- |
| 378 | ! |
report_mig@coef_conversion[, c("coe_date_debut", "coe_valeur_coefficient")]
|
| 379 | ! |
data$coe_date_debut <- as.Date(data$debut_pas) |
| 380 | ! |
data <- merge(data, coe, by = "coe_date_debut") |
| 381 | ! |
data <- data[, -1] # removing coe_date_debut |
| 382 | ! |
data <- |
| 383 | ! |
fun_weight_conversion(tableau = data, |
| 384 | ! |
time.sequence = report_mig@time.sequence, |
| 385 | ! |
silent) |
| 386 |
} else {
|
|
| 387 | 7x |
data$coe_valeur_coefficient = NA |
| 388 |
} |
|
| 389 | 7x |
lestableaux[[stringr::str_c("dc_", dic)]][["data"]] <- data
|
| 390 | 7x |
lestableaux[[stringr::str_c("dc_", dic)]][["method"]] <- "sum"
|
| 391 | 7x |
lestableaux[[stringr::str_c("dc_", dic)]][["contient_poids"]] <-
|
| 392 | 7x |
contient_poids |
| 393 | 7x |
lestableaux[[stringr::str_c("dc_", dic)]][["negative"]] <-
|
| 394 | 7x |
negative |
| 395 |
} |
|
| 396 |
# TODO developper une methode pour sumneg |
|
| 397 | 11x |
report_mig@calcdata <- lestableaux |
| 398 | 11x |
assign("report_mig", report_mig, envir_stacomi)
|
| 399 | 11x |
if (!silent) {
|
| 400 | ! |
funout( |
| 401 | ! |
gettext( |
| 402 | ! |
"Summary object is stocked into envir_stacomi environment : write report_mig=get('report_mig',envir_stacomi) \n",
|
| 403 | ! |
domain = "R-stacomiR" |
| 404 |
) |
|
| 405 |
) |
|
| 406 | ! |
funout( |
| 407 | ! |
gettext( |
| 408 | ! |
"To access calculated data, type report_mig@calcdata\n", |
| 409 | ! |
domain = "R-stacomiR" |
| 410 |
) |
|
| 411 |
) |
|
| 412 |
} |
|
| 413 |
|
|
| 414 |
|
|
| 415 |
|
|
| 416 |
} else {
|
|
| 417 |
# no fish... |
|
| 418 | 2x |
funout( |
| 419 | 2x |
gettext( |
| 420 | 2x |
"There are no values for the taxa, stage and selected period\n", |
| 421 | 2x |
domain = "R-stacomiR" |
| 422 |
) |
|
| 423 |
) |
|
| 424 |
} |
|
| 425 | 13x |
return(report_mig) |
| 426 |
} |
|
| 427 |
) |
|
| 428 | ||
| 429 | ||
| 430 |
#deprecated0.6 |
|
| 431 |
##' handler to print the command line |
|
| 432 |
##' @param h a handler |
|
| 433 |
##' @param ... Additional parameters |
|
| 434 |
##' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 435 |
##' @keywords internal |
|
| 436 |
#houtreport_mig=function(h=null,...) {
|
|
| 437 |
# if (exists("ref_stage",envir_stacomi)) {
|
|
| 438 |
# report_mig<-get("report_mig",envir_stacomi)
|
|
| 439 |
# print(report_mig) |
|
| 440 |
# } |
|
| 441 |
# else |
|
| 442 |
# {
|
|
| 443 |
# funout(gettext("Please select DC, taxa, and stages for a complete command\n",domain="R-stacomiR"),arret=TRUE)
|
|
| 444 |
# } |
|
| 445 |
#} |
|
| 446 | ||
| 447 |
#' Method to print the command line of the object |
|
| 448 |
#' @param x An object of class report_mig |
|
| 449 |
#' @param ... Additional parameters passed to print |
|
| 450 |
#' @return NULL |
|
| 451 |
#' @author cedric.briand |
|
| 452 |
#' @aliases print.report_mig |
|
| 453 |
#' @export |
|
| 454 |
setMethod( |
|
| 455 |
"print", |
|
| 456 |
signature = signature("report_mig"),
|
|
| 457 |
definition = function(x, ...) {
|
|
| 458 | 1x |
sortie1 <- "report_mig=new('report_mig');"
|
| 459 | 1x |
sortie2 <- stringr::str_c( |
| 460 | 1x |
"report_mig=choice_c(report_mig,", |
| 461 | 1x |
"dc=c(",
|
| 462 | 1x |
stringr::str_c(x@dc@dc_selected, collapse = ","), |
| 463 |
"),", |
|
| 464 | 1x |
"taxa=c(",
|
| 465 | 1x |
stringr::str_c(shQuote(x@taxa@data$tax_nom_latin), collapse = ","), |
| 466 |
"),", |
|
| 467 | 1x |
"stage=c(",
|
| 468 | 1x |
stringr::str_c(shQuote(x@stage@stage_selected), collapse = ","), |
| 469 |
"),", |
|
| 470 | 1x |
"datedebut=", |
| 471 | 1x |
shQuote(strftime(x@timestep@dateDebut, format = "%d/%m/%Y")), |
| 472 | 1x |
",datefin=", |
| 473 | 1x |
shQuote(strftime(end_date(x@timestep), format = "%d/%m/%Y")), |
| 474 |
")" |
|
| 475 |
) |
|
| 476 |
# removing backslashes |
|
| 477 | 1x |
funout(stringr::str_c(sortie1, sortie2), ...) |
| 478 | 1x |
return(invisible(NULL)) |
| 479 |
} |
|
| 480 |
) |
|
| 481 | ||
| 482 | ||
| 483 | ||
| 484 | ||
| 485 |
#' Plots of various type for report_mig. |
|
| 486 |
#' |
|
| 487 |
#' \itemize{
|
|
| 488 |
#' \item{plot.type="standard"}{calls \code{\link{fungraph}} and \code{\link{fungraph_glasseel}} functions to plot as many "report_mig"
|
|
| 489 |
#' as needed, the function will test for the existence of data for one dc, one taxa, and one stage} |
|
| 490 |
#' \item{plot.type="step"}{creates Cumulated graphs for report_mig_mult. Data are summed per day for different dc taxa and stages}
|
|
| 491 |
#' \item{plot.type="multiple"}{Method to overlay graphs for report_mig_mult (multiple dc/taxa/stage in the same plot)}
|
|
| 492 |
#' } |
|
| 493 |
#' @param x An object of class report_mig |
|
| 494 |
#' @param y From the formals but missing |
|
| 495 |
#' @param plot.type One of "standard","step". Defaut to \code{standard} the standard report_mig with dc and operation displayed, can also be \code{step} or
|
|
| 496 |
#' \code{multiple}
|
|
| 497 |
#' @param silent Stops displaying the messages. |
|
| 498 |
#' @param color Default NULL, argument passed for the plot.type="standard" method. A vector of color in the following order : (1) working, (2) stopped, (3:7) 1...5 types of operation, |
|
| 499 |
#' (8:11) numbers, weight, NULL, NULL (if glass eel), (8:11) measured, calculated, expert, direct observation for other taxa. If null will be set to brewer.pal(12,"Paired")[c(8,10,4,6,1,2,3,5,7)] |
|
| 500 |
#' @param color_ope Default NULL, argument passed for the plot.type="standard" method. A vector of color for the operations. Default to brewer.pal(4,"Paired") |
|
| 501 |
#' @param ... Additional arguments passed to matplot or plot if plot.type="standard", see ... in \link{fungraph_glasseel} and \link{fungraph}
|
|
| 502 |
#' @return Nothing, called for its side effect |
|
| 503 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 504 |
#' @aliases plot.report_mig |
|
| 505 |
#' @export |
|
| 506 |
setMethod( |
|
| 507 |
"plot", |
|
| 508 |
signature(x = "report_mig", y = "ANY"), |
|
| 509 |
definition = function(x, |
|
| 510 |
y, |
|
| 511 |
plot.type = "standard", |
|
| 512 |
color = NULL, |
|
| 513 |
color_ope = NULL, |
|
| 514 |
silent = FALSE, |
|
| 515 |
...) {
|
|
| 516 |
#report_mig<-r_mig |
|
| 517 | 6x |
report_mig <- x |
| 518 |
################################################################ |
|
| 519 |
# standard plot |
|
| 520 |
################################################################ |
|
| 521 | 6x |
if (plot.type == "standard") {
|
| 522 | 4x |
if (!silent) |
| 523 | 4x |
print("plot type standard")
|
| 524 | 4x |
if (!silent) |
| 525 | 4x |
funout(gettext("Statistics about migration :\n", domain = "R-stacomiR"))
|
| 526 | 4x |
taxa = report_mig@taxa@data[1, "tax_nom_latin"] |
| 527 | 4x |
stage = report_mig@stage@data[1, "std_libelle"] |
| 528 | 4x |
dc = as.numeric(report_mig@dc@dc_selected)[1] |
| 529 | 4x |
data <- report_mig@calcdata[[stringr::str_c("dc_", dc)]][["data"]]
|
| 530 | 4x |
if (!is.null(data)) {
|
| 531 | 4x |
if (nrow(data) > 0) {
|
| 532 | 4x |
if (!silent) {
|
| 533 | 1x |
funout(paste( |
| 534 | 1x |
"dc=", |
| 535 | 1x |
dc, |
| 536 | 1x |
"taxa" = taxa, |
| 537 | 1x |
"stage" = stage, |
| 538 | 1x |
"\n" |
| 539 |
)) |
|
| 540 | 1x |
funout("---------------------\n")
|
| 541 |
} |
|
| 542 | 4x |
if (any(duplicated(data$No.pas))) |
| 543 | 4x |
stop("duplicated values in No.pas")
|
| 544 | 4x |
data_without_hole <- merge( |
| 545 | 4x |
data.frame( |
| 546 | 4x |
No.pas = as.numeric(strftime( |
| 547 | 4x |
report_mig@time.sequence, format = "%j" |
| 548 | 4x |
)) - 1, |
| 549 | 4x |
debut_pas = report_mig@time.sequence |
| 550 |
), |
|
| 551 | 4x |
data, |
| 552 | 4x |
by = c("No.pas", "debut_pas"),
|
| 553 | 4x |
all.x = TRUE |
| 554 |
) |
|
| 555 | 4x |
data_without_hole$CALCULE[is.na(data_without_hole$CALCULE)] <- 0 |
| 556 | 4x |
data_without_hole$MESURE[is.na(data_without_hole$MESURE)] <- 0 |
| 557 | 4x |
data_without_hole$EXPERT[is.na(data_without_hole$EXPERT)] <- 0 |
| 558 | 4x |
data_without_hole$PONCTUEL[is.na(data_without_hole$PONCTUEL)] <- 0 |
| 559 | 4x |
if (report_mig@calcdata[[stringr::str_c("dc_", dc)]][["contient_poids"]] &
|
| 560 | 4x |
taxa == "Anguilla anguilla" & |
| 561 | 4x |
(stage == "civelle" | stage == "Anguille jaune")) {
|
| 562 |
#---------------------------------- |
|
| 563 |
# report migration with weights (glass eel) |
|
| 564 |
#----------------------------------------- |
|
| 565 |
|
|
| 566 | ! |
fungraph_glasseel( |
| 567 | ! |
report_mig = report_mig, |
| 568 | ! |
table = data_without_hole, |
| 569 | ! |
time.sequence = report_mig@time.sequence, |
| 570 | ! |
taxa = taxa, |
| 571 | ! |
stage = stage, |
| 572 | ! |
dc = dc, |
| 573 | ! |
silent, |
| 574 | ! |
color = color, |
| 575 | ! |
color_ope = color_ope, |
| 576 |
... |
|
| 577 |
) |
|
| 578 |
} else {
|
|
| 579 |
#---------------------------------- |
|
| 580 |
# report migration standard |
|
| 581 |
#----------------------------------------- |
|
| 582 |
#silent=TRUE |
|
| 583 | 4x |
fungraph( |
| 584 | 4x |
report_mig = report_mig, |
| 585 | 4x |
tableau = data_without_hole, |
| 586 | 4x |
time.sequence = report_mig@time.sequence, |
| 587 | 4x |
taxa, |
| 588 | 4x |
stage, |
| 589 | 4x |
dc, |
| 590 | 4x |
color = color, |
| 591 | 4x |
color_ope = color_ope, |
| 592 | 4x |
silent, |
| 593 |
... |
|
| 594 |
) |
|
| 595 |
} |
|
| 596 | 4x |
} # end nrow(data)>0 |
| 597 | 4x |
} # end is.null(data) |
| 598 |
|
|
| 599 |
################################################################ |
|
| 600 |
# step plot |
|
| 601 |
################################################################ |
|
| 602 |
# FIXME problem with negative numbers |
|
| 603 | 6x |
} else if (plot.type == "step") {
|
| 604 | 2x |
taxa <- as.character(report_mig@taxa@data$tax_nom_latin) |
| 605 | 2x |
stage <- as.character(report_mig@stage@data$std_libelle) |
| 606 | 2x |
dc <- as.numeric(report_mig@dc@dc_selected) |
| 607 | 2x |
if (report_mig@timestep@step_duration == 86400 & |
| 608 | 2x |
report_mig@timestep@step_duration == 86400) {
|
| 609 | 2x |
grdata <- report_mig@calcdata[[stringr::str_c("dc_", dc)]][["data"]]
|
| 610 | 2x |
grdata <- fun_date_extraction( |
| 611 | 2x |
grdata, |
| 612 | 2x |
nom_coldt = "debut_pas", |
| 613 | 2x |
annee = FALSE, |
| 614 | 2x |
mois = TRUE, |
| 615 | 2x |
quinzaine = TRUE, |
| 616 | 2x |
semaine = TRUE, |
| 617 | 2x |
jour_an = TRUE, |
| 618 | 2x |
jour_mois = FALSE, |
| 619 | 2x |
heure = FALSE |
| 620 |
) |
|
| 621 | 2x |
grdata$Cumsum <- cumsum(grdata$Effectif_total) |
| 622 |
# pour sauvegarder sous excel |
|
| 623 | 2x |
annee <- |
| 624 | 2x |
unique(strftime(as.POSIXlt(report_mig@time.sequence), "%Y"))[1] |
| 625 | 2x |
dis_commentaire <- |
| 626 | 2x |
as.character(report_mig@dc@data$dis_commentaires[report_mig@dc@data$dc %in% |
| 627 | 2x |
report_mig@dc@dc_selected]) |
| 628 | 2x |
update_geom_defaults("line", aes(size = 2))
|
| 629 |
|
|
| 630 | 2x |
p <- ggplot(grdata) + |
| 631 | 2x |
geom_line(aes( |
| 632 | 2x |
x = debut_pas, |
| 633 | 2x |
y = Cumsum, |
| 634 | 2x |
colour = mois |
| 635 |
)) + |
|
| 636 | 2x |
ylab(gettext("Cumulative migration", domain = "R-stacomiR")) +
|
| 637 | 2x |
ggtitle(gettextf( |
| 638 | 2x |
"Cumulative count %s, %s, %s, %s", |
| 639 | 2x |
dis_commentaire, |
| 640 | 2x |
taxa, |
| 641 | 2x |
stage, |
| 642 | 2x |
annee |
| 643 |
)) + |
|
| 644 | 2x |
theme(plot.title = element_text(size = 10, colour = "navy")) + |
| 645 | 2x |
scale_colour_manual( |
| 646 | 2x |
values = c( |
| 647 | 2x |
"01" = "#092360", |
| 648 | 2x |
"02" = "#1369A2", |
| 649 | 2x |
"03" = "#0099A9", |
| 650 | 2x |
"04" = "#009780", |
| 651 | 2x |
"05" = "#67B784", |
| 652 | 2x |
"06" = "#CBDF7C", |
| 653 | 2x |
"07" = "#FFE200", |
| 654 | 2x |
"08" = "#DB9815", |
| 655 | 2x |
"09" = "#E57B25", |
| 656 | 2x |
"10" = "#F0522D", |
| 657 | 2x |
"11" = "#912E0F", |
| 658 | 2x |
"12" = "#33004B" |
| 659 |
) |
|
| 660 |
) |
|
| 661 | 2x |
print(p) |
| 662 |
} else {
|
|
| 663 | ! |
funout( |
| 664 | ! |
gettext( |
| 665 | ! |
"Warning, this function applies for annual summaries\n", |
| 666 | ! |
domain = "R-stacomiR" |
| 667 |
) |
|
| 668 |
) |
|
| 669 |
} |
|
| 670 |
} else {
|
|
| 671 | ! |
stop("unrecognised plot.type argument, plot.type should either be standard or step")
|
| 672 |
} |
|
| 673 | 6x |
return(invisible(NULL)) |
| 674 |
} |
|
| 675 | ||
| 676 | ||
| 677 |
) |
|
| 678 | ||
| 679 | ||
| 680 |
#' summary for report_mig |
|
| 681 |
#' calls functions funstat and funtable to create migration overviews |
|
| 682 |
#' and generate csv and html output in the user data directory |
|
| 683 |
#' @param object An object of class \code{\link{report_mig-class}}
|
|
| 684 |
#' @param silent Should the program stay silent or display messages, default FALSE |
|
| 685 |
#' @param ... Additional parameters (not used there) |
|
| 686 |
#' @return Nothing, calls the \link{summary.report_mig_mult} method
|
|
| 687 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 688 |
#' @aliases summary.report_mig |
|
| 689 |
#' @export |
|
| 690 |
setMethod( |
|
| 691 |
"summary", |
|
| 692 |
signature = signature(object = "report_mig"), |
|
| 693 |
definition = function(object, silent = FALSE, ...) {
|
|
| 694 | 3x |
report_mig_mult <- as(object, "report_mig_mult") |
| 695 | 3x |
summary(report_mig_mult, silent = silent) |
| 696 | 3x |
return(invisible(NULL)) |
| 697 |
} |
|
| 698 | ||
| 699 |
) |
|
| 700 | ||
| 701 | ||
| 702 | ||
| 703 | ||
| 704 | ||
| 705 |
#' Command line method to write the daily and monthly counts to the t_bilanmigrationjournalier_bjo table |
|
| 706 |
#' |
|
| 707 |
#' Daily values are needed to compare migrations from year to year, by the class \link{report_mig_interannual-class}. They are added by
|
|
| 708 |
#' by this function. |
|
| 709 |
#' @param object an object of class \linkS4class{report_mig}
|
|
| 710 |
#' @param silent : TRUE to avoid messages |
|
| 711 |
#' @param check_for_bjo : do you want to check if data are already present in the bjo table, and delete them, |
|
| 712 |
#' this param was added otherwise connect method when called from report_mig_interannual runs in loops |
|
| 713 |
#' @note the user is asked whether or not he wants to overwrite data only when silent is FALSE, if no |
|
| 714 |
#' data are present in the database, the import is done anyway. |
|
| 715 |
#' @return Nothing, just writes data into the database |
|
| 716 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 717 |
#' @examples |
|
| 718 |
#' \dontrun{
|
|
| 719 |
#' stacomi(database_expected=FALSE) |
|
| 720 |
#' data("r_mig")
|
|
| 721 |
#' r_mig<-calcule(r_mig) |
|
| 722 |
#' write_database(report_mig=r_mig,silent=FALSE) |
|
| 723 |
#' } |
|
| 724 |
#' @aliases write_database.report_mig |
|
| 725 |
#' @export |
|
| 726 |
setMethod( |
|
| 727 |
"write_database", |
|
| 728 |
signature = signature("report_mig"),
|
|
| 729 |
definition = function(object, |
|
| 730 |
silent = TRUE, |
|
| 731 |
check_for_bjo = TRUE) {
|
|
| 732 |
# object=bM |
|
| 733 | 5x |
report_mig <- object |
| 734 | 5x |
if (!inherits(report_mig, "report_mig")) |
| 735 | 5x |
stop("the report_mig should be of class report_mig")
|
| 736 | 5x |
if (!inherits(silent, "logical")) |
| 737 | 5x |
stop("the silent argument should be a logical")
|
| 738 | 5x |
dc = as.numeric(report_mig@dc@dc_selected)[1] |
| 739 | 5x |
data = report_mig@calcdata[[stringr::str_c("dc_", dc)]][["data"]]
|
| 740 |
# keep one line if there is one species in one day with as much up as down... |
|
| 741 | 5x |
if (nrow(data) > 1) |
| 742 | 5x |
data = data[data$Effectif_total != 0, ] |
| 743 | 5x |
jour_dans_lannee_non_nuls = data$debut_pas |
| 744 | 5x |
col_a_retirer = match(c("No.pas", "type_de_quantite", "debut_pas", "fin_pas"),
|
| 745 | 5x |
colnames(data)) |
| 746 | 5x |
col_a_retirer = col_a_retirer[!is.na(col_a_retirer)] # as in the case of glass eel and weight |
| 747 |
# the columns are not the same |
|
| 748 | 5x |
data = data[, -col_a_retirer] |
| 749 |
|
|
| 750 |
# below again the taux_d_echappement not there if glass eel and weights |
|
| 751 | 5x |
if (is.null(data$taux_d_echappement)) |
| 752 | 5x |
data$taux_d_echappement <- NA |
| 753 | 5x |
data$taux_d_echappement[data$taux_d_echappement == -1] <- NA |
| 754 | 5x |
if (!is.null(data$coe_valeur_coefficient)) {
|
| 755 | 5x |
data$coe_valeur_coefficient[data$"coe_valeur_coefficient" == 1] <- NA |
| 756 |
} else {
|
|
| 757 | ! |
data$coe_valeur_coefficient <- NA |
| 758 |
} |
|
| 759 | 5x |
cannotbenull = match(c("taux_d_echappement", "coe_valeur_coefficient"),
|
| 760 | 5x |
colnames(data)) |
| 761 |
|
|
| 762 | 5x |
if (nrow(data) > 1) |
| 763 | 5x |
data[, -cannotbenull][data[, -cannotbenull] == 0] <- NA |
| 764 | 5x |
annee <- |
| 765 | 5x |
as.numeric(unique(strftime( |
| 766 | 5x |
as.POSIXlt(report_mig@time.sequence), "%Y" |
| 767 | 5x |
))[1]) |
| 768 |
|
|
| 769 | 5x |
if ("Poids_total" %in% colnames(data)) {
|
| 770 | ! |
aat_reportmigrationjournalier_bjo = cbind( |
| 771 | ! |
report_mig@dc@dc_selected, |
| 772 | ! |
report_mig@taxa@taxa_selected, |
| 773 | ! |
report_mig@stage@stage_selected, |
| 774 | ! |
annee, |
| 775 |
# une valeur |
|
| 776 | ! |
rep(jour_dans_lannee_non_nuls, ncol(data[, c( |
| 777 | ! |
"MESURE", |
| 778 | ! |
"CALCULE", |
| 779 | ! |
"EXPERT", |
| 780 | ! |
"PONCTUEL", |
| 781 | ! |
"Effectif_total", |
| 782 | ! |
"Effectif_total.p", |
| 783 | ! |
"Effectif_total.e", |
| 784 | ! |
"poids_depuis_effectifs", |
| 785 | ! |
"Poids_total", |
| 786 | ! |
"taux_d_echappement", |
| 787 | ! |
"coe_valeur_coefficient" |
| 788 |
)])), |
|
| 789 | ! |
utils::stack(data[, c( |
| 790 | ! |
"MESURE", |
| 791 | ! |
"CALCULE", |
| 792 | ! |
"EXPERT", |
| 793 | ! |
"PONCTUEL", |
| 794 | ! |
"Effectif_total", |
| 795 | ! |
"Effectif_total.p", |
| 796 | ! |
"Effectif_total.e", |
| 797 | ! |
"poids_depuis_effectifs", |
| 798 | ! |
"Poids_total", |
| 799 | ! |
"taux_d_echappement", |
| 800 | ! |
"coe_valeur_coefficient" |
| 801 |
)]), |
|
| 802 | ! |
Sys.time(), |
| 803 | ! |
get_org()) |
| 804 |
} else {
|
|
| 805 | 5x |
aat_reportmigrationjournalier_bjo = cbind( |
| 806 | 5x |
report_mig@dc@dc_selected, |
| 807 | 5x |
report_mig@taxa@taxa_selected, |
| 808 | 5x |
report_mig@stage@stage_selected, |
| 809 | 5x |
annee, |
| 810 |
# une valeur |
|
| 811 | 5x |
rep(jour_dans_lannee_non_nuls, ncol(data[, c( |
| 812 | 5x |
"MESURE", |
| 813 | 5x |
"CALCULE", |
| 814 | 5x |
"EXPERT", |
| 815 | 5x |
"PONCTUEL", |
| 816 | 5x |
"Effectif_total", |
| 817 | 5x |
"taux_d_echappement", |
| 818 | 5x |
"coe_valeur_coefficient" |
| 819 |
)])), |
|
| 820 | 5x |
utils::stack(data[, c( |
| 821 | 5x |
"MESURE", |
| 822 | 5x |
"CALCULE", |
| 823 | 5x |
"EXPERT", |
| 824 | 5x |
"PONCTUEL", |
| 825 | 5x |
"Effectif_total", |
| 826 | 5x |
"taux_d_echappement", |
| 827 | 5x |
"coe_valeur_coefficient" |
| 828 |
)]), |
|
| 829 | 5x |
Sys.time(), |
| 830 | 5x |
get_org() |
| 831 |
) |
|
| 832 |
} |
|
| 833 | 5x |
aat_reportmigrationjournalier_bjo = stacomirtools::killfactor(aat_reportmigrationjournalier_bjo[!is.na(aat_reportmigrationjournalier_bjo$values), ]) |
| 834 | 5x |
colnames(aat_reportmigrationjournalier_bjo) <- |
| 835 | 5x |
c( |
| 836 | 5x |
"bjo_dis_identifiant", |
| 837 | 5x |
"bjo_tax_code", |
| 838 | 5x |
"bjo_std_code", |
| 839 | 5x |
"bjo_annee", |
| 840 | 5x |
"bjo_jour", |
| 841 | 5x |
"bjo_valeur", |
| 842 | 5x |
"bjo_labelquantite", |
| 843 | 5x |
"bjo_horodateexport", |
| 844 | 5x |
"bjo_org_code" |
| 845 |
) |
|
| 846 |
|
|
| 847 |
|
|
| 848 |
##### |
|
| 849 |
# Ci dessous conversion de la classe vers migration Interannuelle pour utiliser |
|
| 850 |
# les methodes de cette classe |
|
| 851 | 5x |
bil = as(report_mig, "report_mig_interannual") |
| 852 |
# the argument check_for_bjo ensures that we don't re-run the connect method |
|
| 853 |
# in loop when the write_database is called from within the report_mig_interannual connect method |
|
| 854 |
# check = FALSE tells the method not to check for missing data (we don't want that check when the |
|
| 855 |
# write database is called from the report_mig class |
|
| 856 |
# so far bil@data has no data |
|
| 857 |
|
|
| 858 | 5x |
if (check_for_bjo) bil = connect(bil, silent = silent, check = FALSE) # now should have data in the data slot |
| 859 |
|
|
| 860 | 5x |
confirm = function() {
|
| 861 | 5x |
supprime(bil) |
| 862 | 5x |
con <- new("ConnectionDB")
|
| 863 | 5x |
con <- connect(con) |
| 864 | 5x |
on.exit(pool::poolClose(con@connection)) |
| 865 | 5x |
pool::dbWriteTable(con@connection, |
| 866 | 5x |
name = "aat_reportmigrationjournalier_bjo", |
| 867 | 5x |
value=aat_reportmigrationjournalier_bjo, |
| 868 | 5x |
temporary=TRUE) |
| 869 | 5x |
sql <- |
| 870 | 5x |
stringr::str_c( |
| 871 | 5x |
"INSERT INTO ", |
| 872 | 5x |
get_schema(), |
| 873 | 5x |
"t_bilanmigrationjournalier_bjo (",
|
| 874 | 5x |
"bjo_dis_identifiant,bjo_tax_code,bjo_std_code,bjo_annee,bjo_jour,bjo_valeur,bjo_labelquantite,bjo_horodateexport,bjo_org_code)", |
| 875 | 5x |
" SELECT * FROM aat_reportmigrationjournalier_bjo;" |
| 876 |
) |
|
| 877 |
# con already created above |
|
| 878 |
|
|
| 879 |
#CHECKME : i removed the capture output is it OK |
|
| 880 |
# utils::capture.output(pool::dbExecute(con@connection, statement = sql)) |
|
| 881 | 5x |
pool::dbExecute(con@connection, statement = sql) |
| 882 |
|
|
| 883 | 5x |
if (!silent) {
|
| 884 | 1x |
funout(gettextf("Writing daily summary in the database %s \n", annee))
|
| 885 |
} |
|
| 886 |
|
|
| 887 |
# ecriture egalement du report mensuel |
|
| 888 | 5x |
taxa <- report_mig@taxa@data[report_mig@taxa@data$tax_code %in% report_mig@taxa@taxa_selected,"tax_nom_latin"] |
| 889 | 5x |
stage <- report_mig@stage@data[report_mig@stage@data$std_code %in% report_mig@stage@stage_selected,"std_libelle"] |
| 890 | 5x |
DC <- as.numeric(report_mig@dc@dc_selected) |
| 891 | 5x |
tableau <- report_mig@calcdata[[stringr::str_c("dc_", DC)]][["data"]]
|
| 892 | 5x |
resum = funstat( |
| 893 | 5x |
tableau = tableau, |
| 894 | 5x |
time.sequence = tableau$debut_pas, |
| 895 | 5x |
taxa, |
| 896 | 5x |
stage, |
| 897 | 5x |
DC, |
| 898 | 5x |
silent = silent |
| 899 |
) |
|
| 900 | 5x |
fun_write_monthly(report_mig, resum, silent = silent) |
| 901 | 5x |
}#end function hconfirm |
| 902 |
|
|
| 903 |
# below we write if !silent and "yes", if silent and if no data in the db |
|
| 904 |
# we don't write write !only don't write if not silent and "no" |
|
| 905 |
# |
|
| 906 |
|
|
| 907 | 5x |
if (nrow(bil@data) > 0) # this means also check_for_bjo |
| 908 |
{
|
|
| 909 | 1x |
if (!silent) {
|
| 910 | ! |
choice <- menu( |
| 911 | ! |
c("yes", "no"),
|
| 912 | ! |
graphics = TRUE, |
| 913 | ! |
title = gettextf( |
| 914 | ! |
"Summary exists :%s Overwrite ?", |
| 915 | ! |
unique(bil@data$bjo_horodateexport) |
| 916 |
) |
|
| 917 |
) |
|
| 918 | ! |
if (choice=="yes"){
|
| 919 | ! |
confirm() |
| 920 |
} |
|
| 921 | 1x |
} else { # silent write anyways
|
| 922 | 1x |
confirm() |
| 923 |
} |
|
| 924 | 5x |
} else { # no data in bjo so we write anyways
|
| 925 | 4x |
confirm() |
| 926 |
} |
|
| 927 | 5x |
return(invisible(NULL)) |
| 928 |
|
|
| 929 |
} |
|
| 930 |
) |
| 1 |
#' Class 'ref_list' |
|
| 2 |
#' |
|
| 3 |
#' Enables to load a 'ref_list' object from a list given by a 'report' object |
|
| 4 |
#' @param liste choice='character' A vector of character to choose within a droplist |
|
| 5 |
#' @param label='character' the title of the box |
|
| 6 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 7 |
#' \code{new('ref_list', listechoice, label)}. \describe{
|
|
| 8 |
#' \item{list('listechoice')}{Object of class \code{'character'}}\item{:}{Object
|
|
| 9 |
#' of class \code{'character'}} \item{list('label')}{Object of class
|
|
| 10 |
#' \code{'character'}}\item{:}{Object of class \code{'character'}} }
|
|
| 11 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 12 |
#' @keywords internal |
|
| 13 |
#' @family referential objects |
|
| 14 |
setClass(Class = "ref_list", representation = representation(listechoice = "character", |
|
| 15 |
selectedvalue = "character", label = "character")) |
|
| 16 | ||
| 17 | ||
| 18 |
#' Loading method for ref_list referential objects |
|
| 19 |
#' @aliases charge.ref_list |
|
| 20 |
#' @return An S4 object of class \link{ref_list-class}
|
|
| 21 |
#' @param object An object of class \link{ref_list-class}
|
|
| 22 |
#' @param listechoice A character vector setting the possible values in which the user can select |
|
| 23 |
#' @param label A label for refliste |
|
| 24 |
#' @return An S4 object of class \link{ref_list-class}
|
|
| 25 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 26 |
#' @examples |
|
| 27 |
#' \dontrun{
|
|
| 28 |
#' object=new('ref_list')
|
|
| 29 |
#' charge(object) |
|
| 30 |
#' } |
|
| 31 |
setMethod("charge", signature = signature("ref_list"), definition = function(object,
|
|
| 32 |
listechoice, label) {
|
|
| 33 | 11x |
object@listechoice = listechoice |
| 34 | 11x |
object@label = label |
| 35 | 11x |
return(object) |
| 36 |
}) |
|
| 37 | ||
| 38 | ||
| 39 |
#' Choice_c method for ref_list referential objects |
|
| 40 |
#' @aliases choice_c.ref_list |
|
| 41 |
#' @param object An object of class \link{ref_list-class}
|
|
| 42 |
#' @param selectedvalue the value selected in the combo |
|
| 43 |
#' @return An S4 object of class \link{ref_list-class}
|
|
| 44 |
#' @note the choice method assigns an object of class refList named ref_list in the environment envir_stacomi |
|
| 45 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 46 |
#' @examples |
|
| 47 |
#' \dontrun{
|
|
| 48 |
#' object=new('ref_list')
|
|
| 49 |
#' object<-charge(object,vecteur=c('1','2'),label='please choose')
|
|
| 50 |
#' object<-choice_c(object) |
|
| 51 |
#' } |
|
| 52 |
setMethod("choice_c", signature = signature("ref_list"), definition = function(object,
|
|
| 53 |
selectedvalue) {
|
|
| 54 | 11x |
if (length(selectedvalue) > 1) |
| 55 | ! |
stop("valeurchoisie should be a vector of length 1")
|
| 56 | 11x |
if (inherits(selectedvalue, "numeric")) |
| 57 | ! |
selectedvalue <- as.character(selectedvalue) |
| 58 |
# the charge method must be performed before |
|
| 59 | ||
| 60 | 11x |
if (!selectedvalue %in% object@listechoice) {
|
| 61 | ! |
stop(stringr::str_c("The selected valeur,", selectedvalue, " not in the list of possible values :",
|
| 62 | ! |
stringr::str_c(object@listechoice, collapse = ","))) |
| 63 |
} else {
|
|
| 64 | 11x |
object@selectedvalue <- selectedvalue |
| 65 |
} |
|
| 66 | 11x |
return(object) |
| 67 |
}) |
| 1 |
#' Function to calculate statistics per month |
|
| 2 |
#' @param tableau A table with the following columns : No.pas,debut_pas,fin_pas, |
|
| 3 |
#' ope_dic_identifiant,lot_tax_code,lot_std_code,type_de_quantite,MESURE,CALCULE, |
|
| 4 |
#' EXPERT,PONCTUEL,Effectif_total,taux_d_echappement,coe_valeur_coefficient |
|
| 5 |
#' @note this function is intended to be called from within the summary method |
|
| 6 |
#' @param time.sequence Passed from report_mig or report_mig_mult |
|
| 7 |
#' @param taxa Taxa |
|
| 8 |
#' @param stage The Stage |
|
| 9 |
#' @param DC The counting device |
|
| 10 |
#' @param silent Message displayed or not |
|
| 11 |
#' @return No return value, called for side effects |
|
| 12 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 13 |
#' @export |
|
| 14 |
funstat = function(tableau, time.sequence, taxa, stage, DC, silent) {
|
|
| 15 | 11x |
if (!silent) |
| 16 | 4x |
funout(gettext("Calculation of the monthly balance sheet\n", domain = "R-stacomiR"))
|
| 17 | 11x |
mois = strftime(as.POSIXlt(time.sequence), "%m") |
| 18 | 11x |
moislab = unique(mois) |
| 19 | 11x |
annee = paste(unique(strftime(as.POSIXlt(time.sequence), "%Y")), collapse = ",") |
| 20 | 11x |
somme = tapply(tableau$Effectif_total, mois, sum, na.rm = TRUE) # sums |
| 21 | 11x |
moyennes_journalieres = tapply(tableau$Effectif_total, mois, mean, na.rm = TRUE) # means |
| 22 |
# ecarts_types=tapply(tableau$Effectif_total, mois, sd, na.rm=TRUE) # std. |
|
| 23 |
# deviations nombre=as.integer(tapply(tableau$Effectif_total, mois, |
|
| 24 |
# function(x) sum(!is.na(x)))) # counts |
|
| 25 | 11x |
resum = rbind(somme, moyennes_journalieres) #,moyennes_journalieres,ecarts_types,nombre) |
| 26 | 11x |
if (taxa == "Anguilla anguilla" & stage == "civelle") {
|
| 27 | 2x |
poids_depuis_effectif = tapply(tableau$poids_depuis_effectif, mois, sum, |
| 28 | 2x |
na.rm = TRUE) |
| 29 | 2x |
poids_mesure = tapply(tableau$Poids_total, mois, sum, na.rm = TRUE) |
| 30 | 2x |
Poids_total = poids_depuis_effectif + poids_mesure |
| 31 | 2x |
resum = rbind(somme, moyennes_journalieres, poids_depuis_effectif, poids_mesure, |
| 32 | 2x |
Poids_total) |
| 33 |
} |
|
| 34 | 11x |
resum = resum[, moislab, drop = FALSE] |
| 35 | 11x |
resum = as.data.frame(resum) |
| 36 | 11x |
resum["somme", "year"] = round(sum(tableau$Effectif_total, na.rm = TRUE), 2) |
| 37 | 11x |
resum["moyennes_journalieres", "year"] = mean(tableau$Effectif_total, na.rm = TRUE) |
| 38 |
# resum['moyennes_journalieres','year']=round(mean(tableau$Effectif_total, |
|
| 39 |
# na.rm=TRUE),2) |
|
| 40 |
# resum['ecarts_types','report']=round(sd(tableau$Effectif_total, |
|
| 41 |
# na.rm=TRUE),2) |
|
| 42 | 11x |
if (taxa == "Anguilla anguilla" & stage == "civelle") {
|
| 43 | 2x |
resum["poids_depuis_effectif", "year"] = round(sum(tableau$poids_depuis_effectif, |
| 44 | 2x |
na.rm = TRUE), 2) |
| 45 | 2x |
resum["poids_mesure", "year"] = round(sum(tableau$Poids_total, na.rm = TRUE), |
| 46 | 2x |
2) |
| 47 | 2x |
resum["Poids_total", "year"] = round(sum(Poids_total, na.rm = TRUE), 2) |
| 48 |
} |
|
| 49 | 11x |
resum = cbind(label = paste("DC", DC, taxa, stage, annee, sep = "_"), resum)
|
| 50 |
# funout(paste(DC,taxa,stage,annee,'\n')) |
|
| 51 |
# funout(paste('DC','code_taxa','code_stage','annee','\n'))
|
|
| 52 | 11x |
if (!silent) {
|
| 53 | 4x |
funout(gettext("Calculation of the monthly balance sheet\n", domain = "R-stacomiR"))
|
| 54 | 4x |
print(resum["somme", ]) |
| 55 |
} |
|
| 56 | 11x |
return(resum) |
| 57 |
} |
| 1 |
UNE_SECONDE = as.difftime(c("0:0:1"))
|
|
| 2 | ||
| 3 |
UNE_MINUTE = 60 * UNE_SECONDE |
|
| 4 | ||
| 5 |
DIX_MINUTES = 10 * UNE_MINUTE |
|
| 6 | ||
| 7 |
QUINZE_MINUTES = 15 * UNE_MINUTE |
|
| 8 | ||
| 9 |
TRENTE_MINUTES = 30 * UNE_MINUTE |
|
| 10 | ||
| 11 |
UNE_HEURE = 60 * UNE_MINUTE |
|
| 12 | ||
| 13 |
DOUZE_HEURES = 12 * UNE_HEURE |
|
| 14 | ||
| 15 |
UN_JOUR = 24 * UNE_HEURE |
|
| 16 | ||
| 17 |
UNE_SEMAINE = 7 * UN_JOUR |
|
| 18 | ||
| 19 |
DEUX_SEMAINES = 2 * UNE_SEMAINE |
|
| 20 | ||
| 21 |
UN_MOIS = 30 * UN_JOUR |
|
| 22 | ||
| 23 |
TROIS_MOIS = 91 * UN_JOUR |
|
| 24 | ||
| 25 |
SIX_MOIS = 182 * UN_JOUR |
|
| 26 | ||
| 27 |
UN_AN = 365 * UN_JOUR |
|
| 28 | ||
| 29 | ||
| 30 |
Valeurref_timestep = c( |
|
| 31 |
UNE_SECONDE, |
|
| 32 |
UNE_MINUTE, |
|
| 33 |
DIX_MINUTES, |
|
| 34 |
QUINZE_MINUTES, |
|
| 35 |
TRENTE_MINUTES, |
|
| 36 |
UNE_HEURE, |
|
| 37 |
DOUZE_HEURES, |
|
| 38 |
UN_JOUR, |
|
| 39 |
UNE_SEMAINE, |
|
| 40 |
DEUX_SEMAINES, |
|
| 41 |
UN_MOIS, |
|
| 42 |
TROIS_MOIS, |
|
| 43 |
SIX_MOIS, |
|
| 44 |
UN_AN |
|
| 45 |
) |
|
| 46 |
Labelref_timestep = c( |
|
| 47 |
"1 sec", |
|
| 48 |
"1 min", |
|
| 49 |
"10 min" , |
|
| 50 |
"15 min" , |
|
| 51 |
"30 min", |
|
| 52 |
"1 h" , |
|
| 53 |
"12 h" , |
|
| 54 |
"1 jour" , |
|
| 55 |
"1 sem" , |
|
| 56 |
"2 sem" , |
|
| 57 |
"1 mois" , |
|
| 58 |
"3 mois" , |
|
| 59 |
"6 mois" , |
|
| 60 |
"1 an" |
|
| 61 |
) |
|
| 62 |
Lesref_timestep = data.frame("Valeurref_timestep" = Valeurref_timestep)
|
|
| 63 |
Lesref_timestep[, "Labelref_timestep"] = Labelref_timestep |
|
| 64 |
rownames(Lesref_timestep) = |
|
| 65 |
c( |
|
| 66 |
"UNE_SECONDE", |
|
| 67 |
"UNE_MINUTE", |
|
| 68 |
"DIX_MINUTES", |
|
| 69 |
"QUINZE_MINUTES", |
|
| 70 |
"TRENTE_MINUTES", |
|
| 71 |
"UNE_HEURE", |
|
| 72 |
"DOUZE_HEURES", |
|
| 73 |
"UN_JOUR", |
|
| 74 |
"UNE_SEMAINE", |
|
| 75 |
"DEUX_SEMAINES", |
|
| 76 |
"UN_MOIS", |
|
| 77 |
"TROIS_MOIS", |
|
| 78 |
"SIX_MOIS", |
|
| 79 |
"UN_AN" |
|
| 80 |
) |
|
| 81 |
rm( |
|
| 82 |
UNE_SECONDE, |
|
| 83 |
UNE_MINUTE, |
|
| 84 |
DIX_MINUTES, |
|
| 85 |
QUINZE_MINUTES, |
|
| 86 |
TRENTE_MINUTES, |
|
| 87 |
UNE_HEURE, |
|
| 88 |
DOUZE_HEURES, |
|
| 89 |
UN_JOUR, |
|
| 90 |
UNE_SEMAINE, |
|
| 91 |
DEUX_SEMAINES, |
|
| 92 |
UN_MOIS, |
|
| 93 |
TROIS_MOIS, |
|
| 94 |
SIX_MOIS, |
|
| 95 |
UN_AN, |
|
| 96 |
Labelref_timestep |
|
| 97 |
) |
|
| 98 | ||
| 99 | ||
| 100 |
validity_ref_timestep = function(object) |
|
| 101 |
{
|
|
| 102 | 24x |
retValue = NULL |
| 103 | 24x |
rep1 = inherits(object@dateDebut[1], "POSIXlt") |
| 104 | 24x |
if (!rep1) |
| 105 | ! |
retValue = "object@dateDebut is not of class POSIXlt" |
| 106 | 24x |
rep2 = length(object@step_duration) == 1 |
| 107 | 24x |
if (!rep2) |
| 108 | ! |
retValue = paste(retValue, "length(object@step_duration) !=1") |
| 109 | 24x |
rep3 = length(object@nb_step) == 1 |
| 110 | 24x |
if (!rep3) |
| 111 | ! |
retValue = paste(retValue, "length(object@nb_step) !=1") |
| 112 | 24x |
rep4 = length(object@nocurrent_step) == 1 |
| 113 | 24x |
if (!rep4) |
| 114 | ! |
retValue = paste(retValue, "length(object@nocurrent_step) !=1") |
| 115 | 24x |
return(ifelse(rep1 & rep2 & rep3 & rep4, TRUE, retValue)) |
| 116 |
} |
|
| 117 | ||
| 118 |
#' Class "ref_timestep" |
|
| 119 |
#' |
|
| 120 |
#' Describes a time step |
|
| 121 |
#' |
|
| 122 |
#' |
|
| 123 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 124 |
#' \code{new("ref_timestep",
|
|
| 125 |
#' dateDebut="POSIXt",step_duration=numeric(),nb_step=numeric(),nocurrent_step=integer())}. |
|
| 126 |
#' \describe{
|
|
| 127 |
#' \item{list("dateDebut")}{Object of class \code{"POSIXt"} Starting
|
|
| 128 |
#' date } |
|
| 129 |
#' \item{:}{Object of class \code{"POSIXt"} Starting date }
|
|
| 130 |
#' \item{list("step_duration")}{Object of class \code{"numeric"} Step length
|
|
| 131 |
#' }\item{:}{Object of class \code{"numeric"} Step length }
|
|
| 132 |
#' \item{list("nb_step")}{Object of class \code{"numeric"} Number of steps
|
|
| 133 |
#' }\item{:}{Object of class \code{"numeric"} Number of steps }
|
|
| 134 |
#' \item{list("nocurrent_step")}{Object of class \code{"integer"} Number of the
|
|
| 135 |
#' current step }\item{:}{Object of class \code{"integer"} Number of the
|
|
| 136 |
#' current step } } |
|
| 137 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 138 |
#' @seealso \code{\linkS4class{ref_timestep_daily}}
|
|
| 139 |
#' @concept report Object |
|
| 140 |
setClass( |
|
| 141 |
Class = "ref_timestep", |
|
| 142 |
representation = |
|
| 143 |
representation( |
|
| 144 |
dateDebut = "POSIXlt", |
|
| 145 |
step_duration = "numeric", |
|
| 146 |
nb_step = "numeric", |
|
| 147 |
nocurrent_step = "integer" |
|
| 148 |
), |
|
| 149 |
validity = validity_ref_timestep, |
|
| 150 |
prototype = prototype( |
|
| 151 |
dateDebut = as.POSIXlt(Hmisc::truncPOSIXt(Sys.time(), "year")), |
|
| 152 |
step_duration = as.numeric(86400), |
|
| 153 |
nb_step = as.numeric(1), |
|
| 154 |
nocurrent_step = as.integer(0) |
|
| 155 |
) |
|
| 156 |
) |
|
| 157 |
# timestep= new("ref_timestep")
|
|
| 158 | ||
| 159 | ||
| 160 |
validity_ref_timestepChar = function(object) |
|
| 161 |
{
|
|
| 162 | ! |
rep1 = inherits(object@dateDebut[1],"POSIXlt") |
| 163 | ! |
rep2 = length(object@step_duration) == 1 |
| 164 | ! |
rep3 = length(object@nb_step) == 1 |
| 165 | ! |
rep4 = length(object@nocurrent_step) == 1 |
| 166 | ! |
rep5 = object@step_duration %in% Lesref_timestep[, "Labelref_timestep"] |
| 167 | ! |
return(ifelse(rep1 & |
| 168 | ! |
rep2 & |
| 169 | ! |
rep3 & rep4 & rep5, TRUE, c(1:5)[!c(rep1, rep2, rep3, rep4, rep5)])) |
| 170 |
} |
|
| 171 |
#' Class "ref_timestepChar" |
|
| 172 |
#' |
|
| 173 |
#' Character to represent a ref_timestep |
|
| 174 |
#' |
|
| 175 |
#' |
|
| 176 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 177 |
#' \code{new("ref_timestepChar", \dots{})}
|
|
| 178 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 179 |
#' @seealso \code{\linkS4class{ref_timestep}}
|
|
| 180 |
#' @keywords classes |
|
| 181 |
#' @examples |
|
| 182 |
#' |
|
| 183 |
#' showClass("ref_timestepChar")
|
|
| 184 |
#' |
|
| 185 |
setClass( |
|
| 186 |
Class = "ref_timestepChar", |
|
| 187 |
representation = |
|
| 188 |
representation( |
|
| 189 |
dateDebut = "POSIXlt", |
|
| 190 |
step_duration = "character", |
|
| 191 |
nb_step = "numeric", |
|
| 192 |
nocurrent_step = "integer" |
|
| 193 |
), |
|
| 194 |
validity = validity_ref_timestepChar, |
|
| 195 |
prototype = prototype( |
|
| 196 |
dateDebut = as.POSIXlt( |
|
| 197 |
strptime("2008-01-01 00:00:00", format = "%Y-%m-%d %H:%M:%S"),
|
|
| 198 |
tz = "GMT" |
|
| 199 |
), |
|
| 200 |
step_duration = as.character("1 jour"),
|
|
| 201 |
nb_step = as.numeric(1), |
|
| 202 |
nocurrent_step = as.integer(0) |
|
| 203 |
) |
|
| 204 |
) |
|
| 205 | ||
| 206 |
setAs("ref_timestepChar", "ref_timestep", # from to
|
|
| 207 |
function(from, to) {
|
|
| 208 | ! |
index = Lesref_timestep[, "Labelref_timestep"] %in% from@step_duration |
| 209 | ! |
newstep_duration = Lesref_timestep[index, "Valeurref_timestep"] |
| 210 | ! |
new( |
| 211 | ! |
"ref_timestep", |
| 212 | ! |
dateDebut = from@dateDebut, |
| 213 | ! |
step_duration = newstep_duration, |
| 214 | ! |
nb_step = from@nb_step, |
| 215 | ! |
nocurrent_step = from@nocurrent_step |
| 216 |
) |
|
| 217 |
}) |
|
| 218 |
# timestep=as(timestepChar,"ref_timestep") |
|
| 219 | ||
| 220 | ||
| 221 |
#' Gets the final horodate for an object of class \link{ref_timestep-class}
|
|
| 222 |
#' @param object An object of class \link{ref_timestep-class}
|
|
| 223 |
#' @return end_date, The final date corresponding to nb_step*time duration + initial date |
|
| 224 |
#' @keywords internal |
|
| 225 |
setMethod( |
|
| 226 |
"end_date", |
|
| 227 |
signature = signature("ref_timestep"),
|
|
| 228 |
definition = function(object) {
|
|
| 229 | 155x |
end_date = object@dateDebut + object@step_duration * (object@nb_step) |
| 230 |
# pour les pb de changement d'heure |
|
| 231 |
|
|
| 232 | 155x |
return(end_date) |
| 233 |
} |
|
| 234 |
) |
|
| 235 | ||
| 236 | ||
| 237 | ||
| 238 | ||
| 239 |
#' Gets the year or a vector of years corresponding to the timestep ("ref_timestep") object
|
|
| 240 |
#' @param object An object of class \link{ref_timestep-class}
|
|
| 241 |
#' @return A numeric with year or vector of years corresponding to the timestep |
|
| 242 |
#' @keywords internal |
|
| 243 |
setMethod( |
|
| 244 |
"get_year", |
|
| 245 |
signature = signature("ref_timestep"),
|
|
| 246 |
definition = function(object) {
|
|
| 247 | 36x |
dateFin = end_date(object) |
| 248 | 36x |
dateDebut = object@dateDebut |
| 249 | 36x |
seq = seq.POSIXt(from = dateDebut, to = dateFin, by = "day") |
| 250 | 36x |
seq = seq[-length(seq)] |
| 251 | 36x |
annees = unique(strftime(seq, "%Y")) |
| 252 | 36x |
return (as.numeric(annees)) |
| 253 |
} |
|
| 254 |
) |
|
| 255 |
| 1 |
#' Class 'ref_df' |
|
| 2 |
#' |
|
| 3 |
#' Representation of a fishway, contains description data of all fishways from |
|
| 4 |
#' the database along with the selected fishways (df) (integer) |
|
| 5 |
#' Objects from the Class: Objects can be created by calls of the form |
|
| 6 |
#' \code{new('ref_df', df_selected=integer(), ouvrage=integer(),
|
|
| 7 |
#' data=data.frame())}. |
|
| 8 |
#' @param df_selected Object of class \code{'integer'} The identifier of the fishway
|
|
| 9 |
#' @param ouvrage Object of class \code{'integer'} The attached dam
|
|
| 10 |
#' @param data Object of class \code{'data.frame'} Data concerning the fishway
|
|
| 11 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 12 |
#' @family referential objects |
|
| 13 |
setClass(Class = "ref_df", representation = representation(df_selected = "integer", |
|
| 14 |
ouvrage = "integer", data = "data.frame")) |
|
| 15 | ||
| 16 |
setValidity("ref_df", method = function(object) {
|
|
| 17 |
if (length(object@df_selected) != 0) {
|
|
| 18 |
if (nrow(object@data) > 0) {
|
|
| 19 |
concord <- object@df_selected %in% object@data$df |
|
| 20 |
if (any(!concord)) {
|
|
| 21 |
return(paste("No data for DF", object@df_selected[!concord]))
|
|
| 22 |
|
|
| 23 |
} else {
|
|
| 24 |
return(TRUE) |
|
| 25 |
} |
|
| 26 |
} else {
|
|
| 27 |
return("You tried to set a value for df_selected without initializing the data slot")
|
|
| 28 |
} |
|
| 29 |
} else return(TRUE) |
|
| 30 |
|
|
| 31 |
}) |
|
| 32 |
#' Loading method for DF referential objects |
|
| 33 |
#' @param object An object of class \link{ref_df-class}
|
|
| 34 |
#' @return An object of class ref_df with df loaded |
|
| 35 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 36 |
#' @examples |
|
| 37 |
#' \dontrun{
|
|
| 38 |
#' object=new('ref_df')
|
|
| 39 |
#' charge(object) |
|
| 40 |
#' } |
|
| 41 |
setMethod("charge", signature = signature("ref_df"), definition = function(object) {
|
|
| 42 | 25x |
requete = new("RequeteDB")
|
| 43 | 25x |
requete@sql = paste("select dis_identifiant as DF,", " dis_date_creation,", " dis_date_suppression,",
|
| 44 | 25x |
" dis_commentaires,", " dif_ouv_identifiant,", " ouv_libelle,", " dif_code as DF_code,", |
| 45 | 25x |
" dif_localisation,", " dif_orientation,", " tdf_libelle as type_DF", " from ", |
| 46 | 25x |
get_schema(), "tg_dispositif_dis", " JOIN ", get_schema(), "t_dispositiffranchissement_dif ON dif_dis_identifiant=dis_identifiant", |
| 47 | 25x |
" JOIN ", get_schema(), "tj_dfesttype_dft ON dif_dis_identifiant=dft_df_identifiant", |
| 48 | 25x |
" JOIN ", get_schema(), "t_ouvrage_ouv on dif_ouv_identifiant=ouv_identifiant", |
| 49 | 25x |
" JOIN ref.tr_typedf_tdf ON tdf_code=dft_tdf_code", " ORDER BY dis_identifiant;", |
| 50 | 25x |
sep = "") |
| 51 | 25x |
requete <- stacomirtools::query(requete) |
| 52 | 25x |
object@data <- requete@query |
| 53 | 25x |
return(object) |
| 54 |
}) |
|
| 55 | ||
| 56 | ||
| 57 |
#' Command line interface to choose a fishway |
|
| 58 |
#' |
|
| 59 |
#' the choice_c method is intended to have the same behaviour as choice (which creates a |
|
| 60 |
#' widget in the graphical interface) but from the command line. The parameters for dF are transformed to integer as the ref_df only |
|
| 61 |
#' takes integer in the df slots. |
|
| 62 |
#' DF are third in hierarchy in the stacomi database Station>ouvrage>DF>DC>operation. This class is only used in the |
|
| 63 |
#' report_df class. |
|
| 64 |
#' @param object an object of class \link{ref_df-class}
|
|
| 65 |
#' @param df a character vector of df chosen |
|
| 66 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 67 |
#' @return An object of class ref_df with df selected |
|
| 68 |
#' @examples |
|
| 69 |
#' \dontrun{
|
|
| 70 |
#' win=gwindow() |
|
| 71 |
#' group=ggroup(container=win,horizontal=FALSE) |
|
| 72 |
#' object=new('ref_df')
|
|
| 73 |
#' object<-charge(object) |
|
| 74 |
#' objectreport=new('report_mig_mult')
|
|
| 75 |
#' choice_c(object=object,objectreport=objectreport,dc=1) |
|
| 76 |
#' } |
|
| 77 |
setMethod("choice_c", signature = signature("ref_df"), definition = function(object,
|
|
| 78 |
df) {
|
|
| 79 |
# object<-ref_df |
|
| 80 | 27x |
if (inherits(df, "numeric")) {
|
| 81 | 4x |
df <- as.integer(df) |
| 82 | 27x |
} else if (inherits(df, "character")) {
|
| 83 |
|
|
| 84 | 2x |
suppressWarnings(expr = {df <- as.integer(as.numeric(df))})
|
| 85 | ||
| 86 |
} |
|
| 87 | 27x |
if (any(is.na(df))) |
| 88 | 27x |
stop("NA values df")
|
| 89 | 26x |
object@df_selected <- df |
| 90 | 26x |
object@ouvrage = object@data$dif_ouv_identifiant[object@data$df %in% object@df_selected] |
| 91 | 26x |
validObject(object) |
| 92 |
# the method validObject verifies that the df is in the data slot of |
|
| 93 |
# ref_df |
|
| 94 |
|
|
| 95 | 26x |
assign("ref_df", object, envir = envir_stacomi)
|
| 96 | 26x |
return(object) |
| 97 |
}) |
| 1 |
#' Report on operations |
|
| 2 |
#' |
|
| 3 |
#' Operations are monitoring operations generated between two dates. In the case of video monitoring |
|
| 4 |
#' or similar, they can be instantaneous |
|
| 5 |
#' |
|
| 6 |
#' @include ref_dc.R |
|
| 7 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 8 |
#' \code{new("report_ope")}.
|
|
| 9 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 10 |
#' @concept report Object |
|
| 11 |
#' @keywords classes |
|
| 12 |
#' @aliases report_ope |
|
| 13 |
#' @export |
|
| 14 |
setClass( |
|
| 15 |
Class = "report_ope", |
|
| 16 |
representation = representation( |
|
| 17 |
data = "data.frame", |
|
| 18 |
dc = "ref_dc", |
|
| 19 |
horodatedebut = "ref_horodate", |
|
| 20 |
horodatefin = "ref_horodate" |
|
| 21 |
), |
|
| 22 |
prototype = prototype( |
|
| 23 |
data = data.frame(), |
|
| 24 |
dc = new("ref_dc"),
|
|
| 25 |
horodatedebut = new("ref_horodate"),
|
|
| 26 |
horodatefin = new("ref_horodate")
|
|
| 27 |
|
|
| 28 |
) |
|
| 29 |
) |
|
| 30 | ||
| 31 |
#' connect method for report_ope |
|
| 32 |
#' |
|
| 33 |
#' @param object An object of class \link{report_ope-class}
|
|
| 34 |
#' load data from the operation table, one dataset per DC |
|
| 35 |
#' @param silent Boolean, TRUE removes messages. |
|
| 36 |
#' @return An object of class \link{report_ope-class} with slot data \code{@data} filled
|
|
| 37 |
#' @aliases connect.report_ope |
|
| 38 |
#' @author cedric.briand |
|
| 39 |
setMethod( |
|
| 40 |
"connect", |
|
| 41 |
signature = signature("report_ope"),
|
|
| 42 |
definition = function(object, silent = FALSE) {
|
|
| 43 |
# object<-report_ope |
|
| 44 | 20x |
req <- new("RequeteDBwheredate")
|
| 45 | 20x |
lesdc <- object@dc@dc_selected |
| 46 | 20x |
req@colonnedebut = "ope_date_debut" |
| 47 | 20x |
req@colonnefin = "ope_date_debut" |
| 48 | 20x |
req@order_by = "ORDER BY ope_dic_identifiant, ope_date_debut" |
| 49 | 20x |
req@datedebut <- object@horodatedebut@horodate |
| 50 |
#below to be consistet with BIlanMigrationMult |
|
| 51 | 20x |
req@datefin <- |
| 52 | 20x |
as.POSIXlt(object@horodatefin@horodate + as.difftime("23:59:59"))
|
| 53 | 20x |
req@select <- |
| 54 | 20x |
paste("SELECT * FROM ",
|
| 55 | 20x |
get_schema(), |
| 56 | 20x |
"t_operation_ope ") |
| 57 | 20x |
req@and = paste("AND ope_dic_identifiant in",
|
| 58 | 20x |
stringr::str_c("(", stringr::str_c(lesdc, collapse = ","), ")"))
|
| 59 | 20x |
req <- |
| 60 | 20x |
stacomirtools::query(req) |
| 61 | 20x |
object@data <- req@query |
| 62 | 20x |
if (!silent) |
| 63 | ! |
funout(gettext("Loading data for operations", domain = "R-stacomiR"))
|
| 64 | 20x |
return(object) |
| 65 |
} |
|
| 66 |
) |
|
| 67 | ||
| 68 | ||
| 69 |
#' charge method for report_ope |
|
| 70 |
#' |
|
| 71 |
#' |
|
| 72 |
#' used by the graphical interface to retrieve referential classes |
|
| 73 |
#' assigned to envir_stacomi |
|
| 74 |
#' @param object An object of class \link{report_ope-class}
|
|
| 75 |
#' @param silent Keeps program silent |
|
| 76 |
#' @return An object of class \link{report_ope-class} with slots filled from values assigned in \code{envir_stacomi} environment
|
|
| 77 |
#' @aliases charge.report_ope |
|
| 78 |
#' @author cedric.briand |
|
| 79 |
#' @keywords internal |
|
| 80 |
setMethod( |
|
| 81 |
"charge", |
|
| 82 |
signature = signature("report_ope"),
|
|
| 83 |
definition = function(object, silent = FALSE) {
|
|
| 84 |
# object<-report_ope |
|
| 85 | 21x |
if (exists("ref_dc", envir = envir_stacomi)) {
|
| 86 | 21x |
object@dc <- get("ref_dc", envir = envir_stacomi)
|
| 87 |
} else {
|
|
| 88 | ! |
funout( |
| 89 | ! |
gettext( |
| 90 | ! |
"You need to choose a counting device, clic on validate\n", |
| 91 | ! |
domain = "R-stacomiR" |
| 92 |
), |
|
| 93 | ! |
arret = TRUE |
| 94 |
) |
|
| 95 |
} |
|
| 96 |
|
|
| 97 | 21x |
if (exists("report_ope_date_debut", envir = envir_stacomi)) {
|
| 98 | 21x |
object@horodatedebut@horodate <- |
| 99 | 21x |
get("report_ope_date_debut", envir = envir_stacomi)
|
| 100 |
} else {
|
|
| 101 | ! |
funout(gettext("You need to choose the starting date\n", domain = "R-stacomiR"),
|
| 102 | ! |
arret = TRUE) |
| 103 |
} |
|
| 104 |
|
|
| 105 | 21x |
if (exists("report_ope_date_fin", envir = envir_stacomi)) {
|
| 106 | 21x |
object@horodatefin@horodate <- |
| 107 | 21x |
get("report_ope_date_fin", envir = envir_stacomi)
|
| 108 |
} else {
|
|
| 109 | ! |
funout(gettext("You need to choose the ending date\n", domain = "R-stacomiR"),
|
| 110 | ! |
arret = TRUE) |
| 111 |
} |
|
| 112 | 21x |
assign("report_ope", object, envir = envir_stacomi)
|
| 113 | 21x |
return(object) |
| 114 |
} |
|
| 115 |
) |
| 1 |
#' Generic method for manual choice using the command line |
|
| 2 |
#' @param object Object |
|
| 3 |
#' @param ... Additional parms |
|
| 4 |
#' @author cedric.briand |
|
| 5 |
#' @keywords internal |
|
| 6 |
#' @export |
|
| 7 | 469x |
setGeneric("choice_c", def = function(object, ...) standardGeneric("choice_c"))
|
| 8 |
#' Generic method to load referentials |
|
| 9 |
#' @param object Object |
|
| 10 |
#' @param ... Additional parm |
|
| 11 |
#' @author cedric.briand |
|
| 12 |
#' @export |
|
| 13 | 256x |
setGeneric("charge", def = function(object, ...) standardGeneric("charge"))
|
| 14 |
#' Generic method to load referentials, with filters from the parent object in the database |
|
| 15 |
#' @param object Object |
|
| 16 |
#' @param ... Additional parms |
|
| 17 |
#' @author cedric.briand |
|
| 18 |
#' @keywords internal |
|
| 19 |
#' @export |
|
| 20 | 141x |
setGeneric("charge_with_filter", def = function(object, ...) standardGeneric("charge_with_filter"))
|
| 21 |
# setGeneric('connect',def=function(object,...) standardGeneric('connect')) #
|
|
| 22 |
# package stacomirtools setGeneric('plot',def=function(x,y,...)
|
|
| 23 |
# standardGeneric('plot'))
|
|
| 24 |
#' Generic for prediction |
|
| 25 |
#' @param object Object |
|
| 26 |
#' @param ... Additional parms |
|
| 27 |
#' @author cedric.briand |
|
| 28 |
#' @export |
|
| 29 | 3x |
setGeneric("model", def = function(object, ...) standardGeneric("model"))
|
| 30 |
#' Generic method to load additional data |
|
| 31 |
#' @param object Object |
|
| 32 |
#' @param ... Additional parms |
|
| 33 |
#' @author cedric.briand |
|
| 34 |
#' @export |
|
| 35 | 2x |
setGeneric("charge_complement", def = function(object, ...) standardGeneric("charge_complement"))
|
| 36 |
#' Generic method for calculations |
|
| 37 |
#' @param object Object |
|
| 38 |
#' @param ... Additional parms |
|
| 39 |
#' @author cedric.briand |
|
| 40 |
#' @export |
|
| 41 | 54x |
setGeneric("calcule", def = function(object, ...) standardGeneric("calcule"))
|
| 42 |
#' Generic method to delete entires from the database |
|
| 43 |
#' @param object Object |
|
| 44 |
#' @param ... Additional parms |
|
| 45 |
#' @author cedric.briand |
|
| 46 |
#' @seealso \link{calcule.report_ge_weight}, \link{calcule.report_mig_char}, \link{calcule.report_mig_env},
|
|
| 47 |
#' \link{calcule.report_mig_interannual},\link{calcule.report_mig_mult},\link{calcule.report_mig_mult},
|
|
| 48 |
#' \link{calcule.report_sample_char}, \link{calcule.report_sea_age}, \link{calcule.report_silver_eel},
|
|
| 49 |
#' \link{calcule.report_species}
|
|
| 50 |
#' @export |
|
| 51 | 9x |
setGeneric("supprime", def = function(object, ...) standardGeneric("supprime"))
|
| 52 |
#' Generic method write_database |
|
| 53 |
#' @param object Object |
|
| 54 |
#' @param ... Additional parms |
|
| 55 |
#' @author cedric.briand |
|
| 56 |
#' @export |
|
| 57 | 7x |
setGeneric("write_database", def = function(object, ...) standardGeneric("write_database"))
|
| 58 |
#' Generic method getvalue |
|
| 59 |
#' @param object Object |
|
| 60 |
#' @param ... Additional parms |
|
| 61 |
#' @author cedric.briand |
|
| 62 |
#' @export |
|
| 63 | ! |
setGeneric("getvalue", def = function(object, ...) standardGeneric("getvalue"))
|
| 64 |
#' Generic method to transform quantitative par into a qualitative one |
|
| 65 |
#' @param object Object |
|
| 66 |
#' @param ... Additional parms |
|
| 67 |
#' @author cedric.briand |
|
| 68 |
#' @export |
|
| 69 | 8x |
setGeneric("setasqualitative", def = function(object, ...) standardGeneric("setasqualitative"))
|
| 70 |
#' Generic method for getting the final date |
|
| 71 |
#' @param object An object |
|
| 72 |
#' @param ... Additional parameters passed to the method |
|
| 73 |
#' @keywords internal |
|
| 74 |
setGeneric( |
|
| 75 |
"end_date", |
|
| 76 | 155x |
def = function(object, ...) |
| 77 | 155x |
standardGeneric("end_date")
|
| 78 |
) |
|
| 79 | ||
| 80 | ||
| 81 |
#' Generic method to get the years |
|
| 82 |
#' @param object An object |
|
| 83 |
#' @param ... Additional parameters passed to the method |
|
| 84 |
#' @keywords internal |
|
| 85 |
setGeneric( |
|
| 86 |
"get_year", |
|
| 87 | 36x |
def = function(object, ...) |
| 88 | 36x |
standardGeneric("get_year")
|
| 89 |
) |
|
| 90 | ||
| 91 |
#' Environment where most objects from the package are stored and then loaded |
|
| 92 |
#' by the charge method |
|
| 93 |
#' |
|
| 94 |
#' envir_stacomi \code{envir_stacomi <- new.env(parent = baseenv())} is the
|
|
| 95 |
#' environment where most object created by the interface are stored |
|
| 96 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 97 |
"envir_stacomi" |
| 1 |
#' Class 'ref_timestep_daily' |
|
| 2 |
#' |
|
| 3 |
#' Representation of a ref_timestep object with a step length equal to one day. |
|
| 4 |
#' It receives an inheritance from ref_timestep |
|
| 5 |
#' |
|
| 6 |
#' validity_ref_timestep_daily |
|
| 7 |
#' @include ref_timestep.R |
|
| 8 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 9 |
#' \code{new('ref_timestep_daily',
|
|
| 10 |
#' dateDebut='POSIXt',step_duration=numeric(),nb_step=numeric(),nocurrent_step=integer())}. |
|
| 11 |
#' \describe{ \item{list('dateDebut')}{Object of class \code{'POSIXt'} Starting
|
|
| 12 |
#' date }\item{:}{Object of class \code{'POSIXt'} Starting date }
|
|
| 13 |
#' \item{list('step_duration')}{Object of class \code{'numeric'} Step length
|
|
| 14 |
#' }\item{:}{Object of class \code{'numeric'} Step length }
|
|
| 15 |
#' \item{list('nb_step')}{Object of class \code{'numeric'} Number of steps
|
|
| 16 |
#' }\item{:}{Object of class \code{'numeric'} Number of steps }
|
|
| 17 |
#' \item{list('nocurrent_step')}{Object of class \code{'integer'} Number of the
|
|
| 18 |
#' current step }\item{:}{Object of class \code{'integer'} Number of the
|
|
| 19 |
#' current step } } |
|
| 20 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 21 |
#' @seealso \code{\linkS4class{ref_timestep}}
|
|
| 22 |
#' @keywords classes |
|
| 23 |
setClass(Class = "ref_timestep_daily", contains = "ref_timestep", prototype = (step_duration = 86400)) |
|
| 24 | ||
| 25 | ||
| 26 | ||
| 27 |
setValidity(Class = "ref_timestep_daily", function(object) {
|
|
| 28 |
retValue <- NULL |
|
| 29 |
rep1 = validity_ref_timestep(object) |
|
| 30 |
if (!is.logical(rep1)) |
|
| 31 |
retValue <- rep1 |
|
| 32 |
rep2 = (object@step_duration == 86400) |
|
| 33 |
if (!rep2) |
|
| 34 |
retValue = paste(retValue, gettext("Time step duration should be daily",
|
|
| 35 |
domain = "R-stacomiR")) |
|
| 36 |
rep3 = length(get_year(object)) == 1 |
|
| 37 |
if (!rep3) |
|
| 38 |
retValue = paste(retValue, gettext("Time step can't include more than one year",
|
|
| 39 |
domain = "R-stacomiR")) |
|
| 40 |
return(ifelse(rep1 & rep2 & rep3, TRUE, retValue)) |
|
| 41 |
}) |
|
| 42 |
# pour test #object=new('ref_timestep_daily')
|
|
| 43 | ||
| 44 | ||
| 45 | ||
| 46 |
#' choice_c method for class ref_timestep_daily |
|
| 47 |
#' |
|
| 48 |
#' the choice_c method is intended to have the same behaviour as choice (which creates a |
|
| 49 |
#' widget in the graphical interface) but from the command line. |
|
| 50 |
#' @param object An object of class \link{ref_timestep_daily-class}
|
|
| 51 |
#' @param datedebut A character (format \code{'15/01/1996'} or \code{'1996-01-15'} or \code{'15-01-1996'}), or POSIXct object
|
|
| 52 |
#' @param datefin A character |
|
| 53 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 54 |
#' @examples |
|
| 55 |
#' \dontrun{
|
|
| 56 |
#' object=new('ref_dc')
|
|
| 57 |
#' object<-charge(object) |
|
| 58 |
#' choice_c(object=object,datedebut='2012-01-01',datefin='2013-01-01') |
|
| 59 |
#' } |
|
| 60 |
#' @return An S4 object of class \link{ref_timestep_daily-class} with date selected
|
|
| 61 |
setMethod("choice_c", signature = signature("ref_timestep_daily"), definition = function(object,
|
|
| 62 |
datedebut, datefin) {
|
|
| 63 | 24x |
if (inherits(datedebut, "character")) {
|
| 64 | 24x |
if (grepl("/", datedebut)) {
|
| 65 | ! |
datedebut = strptime(datedebut, format = "%d/%m/%Y") |
| 66 | ! |
if (is.na(datedebut)) {
|
| 67 | ! |
datedebut = strptime(datedebut, format = "%d/%m/%y") |
| 68 |
} |
|
| 69 | 24x |
} else if (grepl("-", datedebut)) {
|
| 70 | 24x |
datedebut = strptime(datedebut, format = "%Y-%m-%d") |
| 71 | 24x |
if (is.na(datedebut)) {
|
| 72 | ! |
datedebut = strptime(datedebut, format = "%d-%m-%Y") |
| 73 |
} |
|
| 74 |
} |
|
| 75 | 24x |
if (is.na(datedebut)) {
|
| 76 | ! |
stop("datedebut not parsed to datetime try format like '01/01/2017'")
|
| 77 |
} |
|
| 78 |
} |
|
| 79 | ||
| 80 |
# the datedebut can have a POSIXct format |
|
| 81 | 24x |
if (inherits(datefin, "character")) {
|
| 82 | 24x |
if (grepl("/", datefin)) {
|
| 83 | 7x |
datefin = strptime(datefin, format = "%d/%m/%Y") |
| 84 | 7x |
if (is.na(datefin)) {
|
| 85 | ! |
datefin = strptime(datefin, format = "%d/%m/%y") |
| 86 |
} |
|
| 87 | 17x |
} else if (grepl("-", datefin)) {
|
| 88 | 17x |
datefin = strptime(datefin, format = "%Y-%m-%d") |
| 89 | 17x |
if (is.na(datefin)) {
|
| 90 | ! |
datefin = strptime(datefin, format = "%d-%m-%Y") |
| 91 |
} |
|
| 92 |
} |
|
| 93 | 24x |
if (is.na(datefin)) {
|
| 94 | ! |
stop("datefin not parsed to datetime try format like '01/01/2017'")
|
| 95 |
} |
|
| 96 |
} |
|
| 97 | 24x |
object@dateDebut <- as.POSIXlt(datedebut) |
| 98 | 24x |
object@nb_step = as.numeric(difftime(datefin, datedebut, units = "days")) # to fit with end_date(object) |
| 99 | 24x |
validObject(object) |
| 100 | 24x |
assign("timestep", object, envir_stacomi)
|
| 101 | 24x |
return(object) |
| 102 |
}) |
| 1 |
#' Class 'ref_dc' |
|
| 2 |
#' |
|
| 3 |
#' Description of a control device. |
|
| 4 |
#' |
|
| 5 |
#' @include create_generic.R |
|
| 6 |
#' @slot dc_selected Object of class \code{'integer'}, The selected device
|
|
| 7 |
#' @slot ouvrage Object of class \code{'integer'}, the attached dam
|
|
| 8 |
#' @slot station Object of class \code{'character'}, the attached migration monitoring station, this is necessary to join the
|
|
| 9 |
#' table of escapements calculated at the station level. |
|
| 10 |
#' @slot data Object of class \code{'data.frame'} data pertaining to the control device
|
|
| 11 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 12 |
#' \code{new('ref_dc', dc_selected=integer(), ouvrage=integer(),
|
|
| 13 |
#' data=data.frame())}. |
|
| 14 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 15 |
#' @keywords classes |
|
| 16 |
#' @family referential objects |
|
| 17 |
setClass( |
|
| 18 |
Class = "ref_dc", |
|
| 19 |
representation = representation( |
|
| 20 |
dc_selected = "integer", |
|
| 21 |
ouvrage = "integer", |
|
| 22 |
station = "character", |
|
| 23 |
data = "data.frame" |
|
| 24 |
), |
|
| 25 |
prototype = prototype( |
|
| 26 |
dc_selected = integer(), |
|
| 27 |
ouvrage = integer(), |
|
| 28 |
station = character(), |
|
| 29 |
data = data.frame() |
|
| 30 |
) |
|
| 31 |
) |
|
| 32 | ||
| 33 | ||
| 34 | ||
| 35 |
setValidity( |
|
| 36 |
"ref_dc", |
|
| 37 |
method = function(object) {
|
|
| 38 |
if (length(object@dc_selected) != 0) {
|
|
| 39 |
if (nrow(object@data) > 0) {
|
|
| 40 |
concord <- object@dc_selected %in% object@data$dc |
|
| 41 |
if (any(!concord)) {
|
|
| 42 |
return(paste("No data for DC", object@dc_selected[!concord]))
|
|
| 43 |
|
|
| 44 |
} else {
|
|
| 45 |
return(TRUE) |
|
| 46 |
} |
|
| 47 |
} else {
|
|
| 48 |
return( |
|
| 49 |
"You tried to set a value for dc_selected without initializing the data slot" |
|
| 50 |
) |
|
| 51 |
} |
|
| 52 |
} else |
|
| 53 |
return(TRUE) |
|
| 54 |
|
|
| 55 |
} |
|
| 56 |
) |
|
| 57 | ||
| 58 | ||
| 59 |
#' Method to load the counting devices of the control station |
|
| 60 |
#' @param object An object of class \link{ref_dc-class}
|
|
| 61 |
#' @return an object of class ref_dc with data loaded |
|
| 62 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 63 |
setMethod( |
|
| 64 |
"charge", |
|
| 65 |
signature = signature("ref_dc"),
|
|
| 66 |
definition = function(object) {
|
|
| 67 | 70x |
requete = new("RequeteDB")
|
| 68 | 70x |
requete@sql = paste( |
| 69 | 70x |
"select dis_identifiant as DC,", |
| 70 | 70x |
" dis_date_creation,", |
| 71 | 70x |
" dis_date_suppression,", |
| 72 | 70x |
" dif_dis_identifiant as DF,", |
| 73 | 70x |
" dis_commentaires,", |
| 74 | 70x |
" dif_ouv_identifiant,", |
| 75 | 70x |
" ouv_libelle,", |
| 76 | 70x |
" dif_code as DF_code,", |
| 77 | 70x |
" dic_code as DC_code,", |
| 78 | 70x |
" dif_localisation,", |
| 79 | 70x |
" dif_orientation,", |
| 80 | 70x |
" tdf_libelle as type_DF,", |
| 81 | 70x |
" tdc_libelle as type_DC,", |
| 82 | 70x |
"sta_code", |
| 83 | 70x |
" FROM ", |
| 84 | 70x |
get_schema(), |
| 85 | 70x |
"tg_dispositif_dis", |
| 86 | 70x |
" JOIN ", |
| 87 | 70x |
get_schema(), |
| 88 | 70x |
"t_dispositifcomptage_dic ON dic_dis_identifiant =dis_identifiant", |
| 89 | 70x |
" JOIN ", |
| 90 | 70x |
get_schema(), |
| 91 | 70x |
"t_dispositiffranchissement_dif ON dif_dis_identifiant=dic_dif_identifiant", |
| 92 | 70x |
" JOIN ", |
| 93 | 70x |
get_schema(), |
| 94 | 70x |
"tj_dfesttype_dft ON dif_dis_identifiant=dft_df_identifiant", |
| 95 | 70x |
" JOIN ", |
| 96 | 70x |
get_schema(), |
| 97 | 70x |
"t_ouvrage_ouv on dif_ouv_identifiant=ouv_identifiant", |
| 98 | 70x |
" JOIN ", |
| 99 | 70x |
get_schema(), |
| 100 | 70x |
"t_station_sta on ouv_sta_code=sta_code", |
| 101 | 70x |
" JOIN ref.tr_typedf_tdf ON tdf_code=dft_tdf_code", |
| 102 | 70x |
" JOIN ref.tr_typedc_tdc ON dic_tdc_code=tdc_code", |
| 103 | 70x |
" WHERE dft_rang=1", |
| 104 | 70x |
" ORDER BY dis_identifiant;", |
| 105 | 70x |
sep = "" |
| 106 |
) |
|
| 107 | 70x |
requete <- stacomirtools::query(requete) |
| 108 |
# funout(gettext('The query to load counting devices is done
|
|
| 109 |
# \n',domain='R-stacomiR')) |
|
| 110 | 70x |
object@data <- requete@query |
| 111 | 70x |
return(object) |
| 112 |
} |
|
| 113 |
) |
|
| 114 | ||
| 115 | ||
| 116 | ||
| 117 |
#' Command line interface to select a counting device |
|
| 118 |
#' |
|
| 119 |
#' the choice_c method is intended to have the same behaviour as choice (which creates a |
|
| 120 |
#' widget in the graphical interface) but from the command line. The parameters for dc are transformed to integer as the ref_dc only |
|
| 121 |
#' takes integer in the dc slots. The method also loads the stations and ouvrages (dams) associated with the counting device (dc). |
|
| 122 |
#' The values passed to the choice_c method are then checked with the setValidty method. |
|
| 123 |
#' Finally, if an objectreport is passed as a parameter, the method will do a charge_with_filter to select only the taxa present in the counting devices |
|
| 124 |
#' @param object an object of class ref_dc |
|
| 125 |
#' @param dc a character vector of dc chosen |
|
| 126 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 127 |
#' @return An object of class ref_dc with dc selected |
|
| 128 |
#' @examples |
|
| 129 |
#' \dontrun{
|
|
| 130 |
#' win=gwindow() |
|
| 131 |
#' group=ggroup(container=win,horizontal=FALSE) |
|
| 132 |
#' object=new('ref_dc')
|
|
| 133 |
#' object<-charge(object) |
|
| 134 |
#' objectreport=new('report_mig_mult')
|
|
| 135 |
#' choice_c(object=object,objectreport=objectreport,dc=1) |
|
| 136 |
#' } |
|
| 137 |
setMethod( |
|
| 138 |
"choice_c", |
|
| 139 |
signature = signature("ref_dc"),
|
|
| 140 |
definition = function(object, |
|
| 141 |
dc) {
|
|
| 142 | 70x |
if (inherits(dc, "numeric")) {
|
| 143 | 64x |
dc <- as.integer(dc) |
| 144 | 6x |
} else if (inherits(dc, "character")) {
|
| 145 | ! |
dc = as.integer(as.numeric(dc)) |
| 146 |
} |
|
| 147 | 70x |
if (any(is.na(dc))) |
| 148 | ! |
stop("NA values dc")
|
| 149 |
|
|
| 150 |
|
|
| 151 | 70x |
object@dc_selected <- dc |
| 152 | 70x |
validObject(object) |
| 153 |
# the method validObject verifies that the dc is in the data slot of |
|
| 154 |
# ref_dc |
|
| 155 |
|
|
| 156 | 69x |
object@station <- |
| 157 | 69x |
as.character(object@data$sta_code[object@data$dc %in% object@dc_selected]) |
| 158 | 69x |
object@ouvrage <- |
| 159 | 69x |
object@data$dif_ouv_identifiant[object@data$dc %in% object@dc_selected] |
| 160 | 69x |
assign("ref_dc", object, envir = envir_stacomi)
|
| 161 | 69x |
return(object) |
| 162 |
} |
|
| 163 |
) |
| 1 |
# Name : ref_coe(classe) |
|
| 2 | ||
| 3 |
#' Class 'ref_coe' |
|
| 4 |
#' |
|
| 5 |
#' Enables to load conversion coefficients quantity-number. This class only exists to load |
|
| 6 |
#' the data with its method charge. It is not used directly as component of the graphical interface, |
|
| 7 |
#' as the year is already loaded in the different report objects |
|
| 8 |
#' |
|
| 9 |
#' |
|
| 10 |
#' @note Class loading coefficient of conversion between quantity (weights or |
|
| 11 |
#' volumes of glass eel) and numbers between a starting and finishing date |
|
| 12 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 13 |
#' \code{new('ref_coe')}.
|
|
| 14 |
#' @slot data A \code{data.frame}
|
|
| 15 |
#' @slot datedebut A 'POSIXlt' |
|
| 16 |
#' @slot datefin A 'POSIXlt' |
|
| 17 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 18 |
#' @family referential objects |
|
| 19 |
#' @keywords classes |
|
| 20 |
setClass(Class = "ref_coe", representation = representation(data = "data.frame", |
|
| 21 |
datedebut = "POSIXlt", datefin = "POSIXlt"), prototype = prototype(data = data.frame())) |
|
| 22 | ||
| 23 |
#' loads the coefficients for the period defined in class |
|
| 24 |
#' |
|
| 25 |
#' |
|
| 26 |
#' The slots datedebut and datefin have to be filled before using charge |
|
| 27 |
#' @param object An object of class \link{ref_coe-class}
|
|
| 28 |
#' @return An object of class \link{ref_coe-class}
|
|
| 29 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 30 |
#' @examples |
|
| 31 |
#' \dontrun{
|
|
| 32 |
#' object<- new('ref_coe')
|
|
| 33 |
#' object@datedebut<-strptime('01/01/1996',format='%d/%m/%Y')
|
|
| 34 |
#' object@datefin<-strptime('01/01/1997',format='%d/%m/%Y')
|
|
| 35 |
#' charge(object) |
|
| 36 |
#' } |
|
| 37 |
setMethod("charge", signature = signature("ref_coe"), definition = function(object) {
|
|
| 38 | 4x |
requete = new("RequeteDBwheredate")
|
| 39 | 4x |
requete@datedebut = object@datedebut |
| 40 | 4x |
requete@datefin = object@datefin |
| 41 | 4x |
requete@colonnedebut = "coe_date_debut" |
| 42 | 4x |
requete@colonnefin = "coe_date_fin" |
| 43 | 4x |
requete@datefin = as.POSIXlt(object@datefin) |
| 44 | 4x |
requete@select = stringr::str_c("select * from ", get_schema(), "tj_coefficientconversion_coe")
|
| 45 | 4x |
requete@and = " and coe_tax_code='2038' and coe_std_code='CIV' and coe_qte_code='1'" |
| 46 | 4x |
requete <- query(requete) |
| 47 | 4x |
object@data <- requete@query |
| 48 | 4x |
return(object) |
| 49 |
}) |
|
| 50 | ||
| 51 | ||
| 52 |
#' supprime method for 'ref_coe' class |
|
| 53 |
#' @param object An object of class \link{ref_coe-class}
|
|
| 54 |
#' @param tax '2038=Anguilla anguilla' |
|
| 55 |
#' @param std 'CIV=civelle' |
|
| 56 |
#' @param silent Default FALSE, if TRUE the program should no display messages |
|
| 57 |
#' @return Nothing, called for side effect |
|
| 58 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 59 |
#' @export |
|
| 60 |
setMethod("supprime", signature = signature("ref_coe"), definition = function(object,
|
|
| 61 |
tax, std, silent = FALSE) {
|
|
| 62 |
# object<-r_gew@coe;tax=2038;std='CIV' getting the data to import |
|
| 63 |
|
|
| 64 |
# here I assume that dc_selected will be unique (no report with several |
|
| 65 |
# dc) |
|
| 66 | 1x |
req = new("RequeteDB")
|
| 67 | 1x |
req@sql <- stringr::str_c( |
| 68 | 1x |
"WITH deleted AS (",
|
| 69 | 1x |
"DELETE FROM ", get_schema(), "tj_coefficientconversion_coe ", |
| 70 | 1x |
"WHERE coe_date_debut >= '",object@datedebut,"'", |
| 71 | 1x |
" AND coe_date_fin <= '", object@datefin, "'", |
| 72 | 1x |
" AND coe_tax_code='", tax, "' and coe_std_code='", std, |
| 73 | 1x |
"' and coe_qte_code='1'", |
| 74 | 1x |
" RETURNING *)", |
| 75 | 1x |
" SELECT * FROM deleted" |
| 76 |
) |
|
| 77 | 1x |
del <- stacomirtools::getquery(query(req)) |
| 78 | 1x |
nr <- nrow(del) |
| 79 | 1x |
if (!silent) |
| 80 | 1x |
funout(gettextf("%s rows deleted from table tj_coefficientconversion_coe",
|
| 81 | 1x |
nr, domain = "R-stacomiR")) |
| 82 | 1x |
return(invisible(NULL)) |
| 83 |
}) |
|
| 84 |
| 1 |
#' Class 'ref_parquan' |
|
| 2 |
#' |
|
| 3 |
#' Class enabling to load the list of quantitative parameters and to select one |
|
| 4 |
#' of them. It inherits from 'ref_par', uses its 'choice' method |
|
| 5 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 6 |
#' @keywords classes |
|
| 7 |
#' @family referential objects |
|
| 8 |
#' @include ref_par.R |
|
| 9 |
setClass(Class = "ref_parquan", contains = "ref_par") |
|
| 10 | ||
| 11 |
#' Loading method for Reparquan referential objects |
|
| 12 |
#' @param object An object of class \link{ref_parquan-class}
|
|
| 13 |
#' @return An S4 object of class \link{ref_parquan-class} with data loaded
|
|
| 14 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 15 |
#' @examples |
|
| 16 |
#' \dontrun{
|
|
| 17 |
#' object=new('ref_parquan')
|
|
| 18 |
#' charge(object) |
|
| 19 |
#' } |
|
| 20 |
setMethod("charge", signature = signature("ref_parquan"), definition = function(object) {
|
|
| 21 | 1x |
requete = new("RequeteDB")
|
| 22 | 1x |
requete@sql = "SELECT par_code, par_nom, par_unite, par_nature, par_definition FROM ref.tg_parametre_par |
| 23 | 1x |
INNER JOIN ref.tr_parametrequantitatif_qan ON qan_par_code=par_code" |
| 24 | 1x |
requete <- stacomirtools::query(requete) |
| 25 |
# funout(gettext('The query to load parameters is done
|
|
| 26 |
# \n',domain='R-stacomiR')) |
|
| 27 | 1x |
object@data <- requete@query |
| 28 | 1x |
return(object) |
| 29 |
}) |
|
| 30 | ||
| 31 | ||
| 32 |
#' Loading method for Reparquan referential objects searching only those parameters existing for a DC (counting device), a Taxon, and a stage |
|
| 33 |
#' @param object An object of class \link{ref_parquan-class}
|
|
| 34 |
#' @param dc_selected The dc set in the report object |
|
| 35 |
#' @param taxa_selected The taxa set in the report object |
|
| 36 |
#' @param stage_selected The stage set in the report object |
|
| 37 |
#' @return An S4 object of class \link{ref_parquan-class} with data loaded showing available parameters for one DC
|
|
| 38 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 39 |
#' @examples |
|
| 40 |
#' \dontrun{
|
|
| 41 |
#' dc_selected=6 |
|
| 42 |
#'taxa_selected=2038 |
|
| 43 |
#' stage_selected='AGJ' |
|
| 44 |
#' object=new('ref_parquan')
|
|
| 45 |
#' charge_with_filter(object,dc_selected,taxa_selected,stage_selected) |
|
| 46 |
#' } |
|
| 47 |
setMethod("charge_with_filter", signature = signature("ref_parquan"), definition = function(object,
|
|
| 48 |
dc_selected, taxa_selected, stage_selected) {
|
|
| 49 | 8x |
requete = new("RequeteDBwhere")
|
| 50 | 8x |
requete@select = paste("SELECT DISTINCT ON (par_code) par_code, par_nom, par_unite, par_nature, par_definition", " FROM ",
|
| 51 | 8x |
get_schema(), "tg_dispositif_dis", " JOIN ", get_schema(), "t_dispositifcomptage_dic on dis_identifiant=dic_dis_identifiant", |
| 52 | 8x |
" JOIN ", get_schema(), "t_operation_ope on ope_dic_identifiant=dic_dis_identifiant", |
| 53 | 8x |
" JOIN ", get_schema(), "t_lot_lot on lot_ope_identifiant=ope_identifiant", |
| 54 | 8x |
" JOIN ", get_schema(), "tj_caracteristiquelot_car on car_lot_identifiant=lot_identifiant", |
| 55 | 8x |
" JOIN ref.tg_parametre_par on par_code=car_par_code", " JOIN ref.tr_parametrequantitatif_qan ON qan_par_code=par_code", |
| 56 | 8x |
sep = "") |
| 57 | 8x |
requete@where = paste("where dis_identifiant in ", vector_to_listsql(dc_selected))
|
| 58 | 8x |
requete@and = paste("and lot_tax_code in ", vector_to_listsql(taxa_selected),
|
| 59 | 8x |
" and lot_std_code in ", vector_to_listsql(stage_selected), "", sep = "") |
| 60 | 8x |
requete@order_by = "ORDER BY par_code" |
| 61 | 8x |
requete <- stacomirtools::query(requete) |
| 62 | 8x |
object@data <- requete@query |
| 63 | 8x |
return(object) |
| 64 |
}) |
|
| 65 | ||
| 66 |
| 1 |
setAs("report_mig", "report_mig_interannual", function(from) {
|
|
| 2 | 6x |
start_year = new("ref_year")
|
| 3 | 6x |
end_year = new("ref_year")
|
| 4 | 6x |
start_year@year_selected = min(get_year(from@timestep)) |
| 5 | 6x |
end_year@year_selected = max(get_year(from@timestep)) |
| 6 | 6x |
report_mig_interannual = new("report_mig_interannual")
|
| 7 | 6x |
report_mig_interannual@dc = from@dc |
| 8 | 6x |
report_mig_interannual@taxa = from@taxa |
| 9 | 6x |
report_mig_interannual@stage = from@stage |
| 10 | 6x |
report_mig_interannual@start_year = start_year |
| 11 | 6x |
report_mig_interannual@end_year = end_year |
| 12 | 6x |
return(report_mig_interannual) |
| 13 |
}) |
|
| 14 | ||
| 15 | ||
| 16 |
setAs("report_mig_interannual", "report_mig_mult", function(from) {
|
|
| 17 | ! |
report_mig_mult = new("report_mig_mult")
|
| 18 | ! |
report_mig_mult@dc = from@dc |
| 19 | ! |
report_mig_mult@taxa = from@taxa |
| 20 | ! |
report_mig_mult@stage = from@stage |
| 21 | ! |
report_mig_mult@timestep@dateDebut = strptime(stringr::str_c(from@start_year@year_selected, |
| 22 | ! |
"-01-01"), format = "%Y-%m-%d") |
| 23 | ! |
report_mig_mult@timestep@nb_step = 364 |
| 24 | ! |
return(report_mig_mult) |
| 25 |
}) |
|
| 26 | ||
| 27 | ||
| 28 |
setAs("report_mig", "report_mig_mult", function(from) {
|
|
| 29 | 14x |
bMM = new("report_mig_mult")
|
| 30 | 14x |
bMM@dc = from@dc |
| 31 | 14x |
bMM@taxa = from@taxa |
| 32 | 14x |
bMM@stage = from@stage |
| 33 | 14x |
bMM@timestep = from@timestep |
| 34 | 14x |
bMM@coef_conversion = from@coef_conversion |
| 35 | 14x |
bMM@data = from@data |
| 36 | 14x |
bMM@time.sequence = from@time.sequence |
| 37 | 14x |
bMM@calcdata = from@calcdata |
| 38 | 14x |
return(bMM) |
| 39 |
}) |
|
| 40 | ||
| 41 |
setAs("report_mig_interannual", "report_annual", function(from) {
|
|
| 42 | 10x |
r_ann = new("report_annual")
|
| 43 | 10x |
r_ann@dc = from@dc |
| 44 | 10x |
r_ann@taxa = from@taxa |
| 45 | 10x |
r_ann@stage = from@stage |
| 46 | 10x |
r_ann@start_year = from@start_year |
| 47 | 10x |
r_ann@end_year = from@end_year |
| 48 | 10x |
return(r_ann) |
| 49 |
}) |
| 1 |
#' This writes monthly data in t_reportmensuel_mens table |
|
| 2 |
#' |
|
| 3 |
#' @note This function is launched by fun_write_daily, the resum |
|
| 4 |
#' dataset is created by the \link{funstat} function
|
|
| 5 |
#' |
|
| 6 |
#' |
|
| 7 |
#' @param report_mig an object of class \code{\linkS4class{report_mig}}
|
|
| 8 |
#' @param resum data frame with summary per month |
|
| 9 |
#' @param silent Suppresses messages |
|
| 10 |
#' @return No return value, called for side effects |
|
| 11 |
#' @export |
|
| 12 |
fun_write_monthly<-function(report_mig,resum,silent){
|
|
| 13 | 5x |
t_reportmigrationmensuel_bme <- stacomirtools::killfactor( |
| 14 | 5x |
cbind(report_mig@dc@dc_selected, |
| 15 | 5x |
report_mig@taxa@taxa_selected, |
| 16 | 5x |
report_mig@stage@stage_selected, |
| 17 | 5x |
as.integer(unique(strftime(as.POSIXlt(report_mig@time.sequence),"%Y"))), # une valeur bme_annee |
| 18 | 5x |
rep(rownames(resum),(ncol(resum)-2)), # nb of month except columns report and label # bme_labelquantite |
| 19 | 5x |
stack(resum, select=c(2:(ncol(resum)-1))),# stack re-ordonne les tab de donnees ! |
| 20 | 5x |
as.POSIXct(format(Sys.time(), "%Y-%m-%d %H:%M:%S")), |
| 21 | 5x |
get_org() |
| 22 |
) |
|
| 23 |
) |
|
| 24 | 5x |
colnames(t_reportmigrationmensuel_bme) <- |
| 25 | 5x |
c("bme_dis_identifiant","bme_tax_code","bme_std_code","bme_annee","bme_labelquantite","bme_valeur",
|
| 26 | 5x |
"bme_mois","bme_horodateexport","bme_org_code") |
| 27 | 5x |
t_reportmigrationmensuel_bme$bme_mois<- as.integer(t_reportmigrationmensuel_bme$bme_mois) |
| 28 |
# ecriture dans la base... |
|
| 29 | 5x |
con <- new("ConnectionDB")
|
| 30 | 5x |
con <- connect(con) |
| 31 | 5x |
on.exit(pool::poolClose(con@connection)) |
| 32 | 5x |
pool::dbWriteTable(con@connection, |
| 33 | 5x |
name = "temp_t_reportmigrationmensuel_bme", |
| 34 | 5x |
value=t_reportmigrationmensuel_bme, |
| 35 | 5x |
temporary=TRUE, |
| 36 | 5x |
overwrite=TRUE) |
| 37 |
|
|
| 38 | 5x |
sql=paste( |
| 39 | 5x |
"INSERT INTO ", |
| 40 | 5x |
get_schema(), |
| 41 | 5x |
"t_bilanmigrationmensuel_bme (",
|
| 42 | 5x |
"bme_dis_identifiant,bme_tax_code,bme_std_code,bme_annee,bme_labelquantite,bme_valeur, |
| 43 | 5x |
bme_mois,bme_horodateexport,bme_org_code)", |
| 44 | 5x |
" SELECT * FROM temp_t_reportmigrationmensuel_bme") |
| 45 |
|
|
| 46 | 5x |
nline <- pool::dbExecute(con@connection, statement = sql) |
| 47 |
|
|
| 48 | 5x |
if (!silent) funout(gettextf("Writing monthly summary (n=%s) in the database\n", nline, domain="R-stacomiR"))
|
| 49 | 5x |
return(invisible(NULL)) |
| 50 |
} # end function |
|
| 51 |
| 1 |
#' Class 'ref_choice' |
|
| 2 |
#' |
|
| 3 |
#' ref_choice referential class allows to choose within several values with |
|
| 4 |
#' radiobuttons interface |
|
| 5 |
#' |
|
| 6 |
#' @section Objects from the Class: Objects can be created by calls of the form |
|
| 7 |
#' \code{new('ref_choice', listechoice=character() ,label=character()
|
|
| 8 |
#' ,selected=integer())}. |
|
| 9 |
#' @slot listechoice A character vector giving possible choices |
|
| 10 |
#' @slot label A character, title of the box giving the possible choices |
|
| 11 |
#' @slot selected An \code{Integer} the initial selected value (as an index), first=1 used in gradio
|
|
| 12 |
#' @author cedric.briand@eptb-vilaine.fr |
|
| 13 |
#' @family referential objects |
|
| 14 |
setClass(Class = "ref_choice", representation = representation(listechoice = "ANY", |
|
| 15 |
label = "character", selected = "integer", selectedvalue = "ANY"), prototype = list(selectedvalue = vector())) |
|
| 16 | ||
| 17 |
#' Loading method for Refchoice referential objects |
|
| 18 |
#' |
|
| 19 |
#' @family referential objects |
|
| 20 |
#' @return An S4 object of class \link{ref_choice-class}
|
|
| 21 |
#' @param object An object of class ref_choice |
|
| 22 |
#' @param vecteur A vector of name, see example code. |
|
| 23 |
#' @param label Labels for the choices |
|
| 24 |
#' @param selected An integer indicating which object is selected at launch |
|
| 25 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 26 |
#' @examples |
|
| 27 |
#' \dontrun{
|
|
| 28 |
#' object=new('ref_choice')
|
|
| 29 |
#' charge(object,vecteur=c('oui','non'),label='essai',selected=as.integer(1))
|
|
| 30 |
#' } |
|
| 31 |
setMethod("charge", signature = signature("ref_choice"), definition = function(object,
|
|
| 32 |
vecteur, label, selected) {
|
|
| 33 | 7x |
object@listechoice = vecteur |
| 34 | 7x |
object@label = label |
| 35 | 7x |
object@selected = selected |
| 36 | 7x |
object |
| 37 | 7x |
return(object) |
| 38 |
}) |
|
| 39 | ||
| 40 | ||
| 41 |
#' Choice_c method for ref_choice referential objects |
|
| 42 |
#' @param object An object of class \link{ref_choice-class}
|
|
| 43 |
#' @param selectedvalue the value selected in the combo |
|
| 44 |
#' @return An S4 object of class \link{ref_choice-class}
|
|
| 45 |
#' @author Cedric Briand \email{cedric.briand@eptb-vilaine.fr}
|
|
| 46 |
#' @examples |
|
| 47 |
#' \dontrun{
|
|
| 48 |
#' object=new('ref_list')
|
|
| 49 |
#' object<-charge(object,vecteur=c('1','2'),label='please choose')
|
|
| 50 |
#' object<-choice_c(object) |
|
| 51 |
#' } |
|
| 52 |
setMethod("choice_c", signature = signature("ref_choice"), definition = function(object,
|
|
| 53 |
selectedvalue) {
|
|
| 54 | ||
| 55 | 7x |
if (length(selectedvalue) > 1) |
| 56 | ! |
stop("valeurchoisie should be a vector of length 1")
|
| 57 | 7x |
if (inherits(selectedvalue,"numeric")) |
| 58 | ! |
selectedvalue <- as.character(selectedvalue) |
| 59 |
# the charge method must be performed before |
|
| 60 | ||
| 61 | 7x |
if (!selectedvalue %in% object@listechoice) {
|
| 62 | ! |
stop(stringr::str_c("The selected valeur,", selectedvalue, " not in the list of possible values :",
|
| 63 | ! |
stringr::str_c(object@listechoice, collapse = ","))) |
| 64 |
} else {
|
|
| 65 | 7x |
object@selectedvalue <- selectedvalue |
| 66 |
} |
|
| 67 | 7x |
return(object) |
| 68 | ||
| 69 | ||
| 70 |
}) |
|
| 71 |