lbs<-c('rvest','stringr','openxlsx','DT','kableExtra','plyr','data.table')
req <- substitute(require(x, character.only = TRUE))
sapply(lbs, function(x) eval(req) || {install.packages(x); eval(req)})
## rvest stringr openxlsx DT kableExtra plyr data.table
## TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Nomes deverão ser conforme cadastrados no SMO
Nomes<-c("SO SERGIO RICARDO FARIAS CAVALCANTE","SO ARIONE ARAUJO PITTA","SO CARLOS ROBERTO DE SOUSA",
"SO MAURO BARBOSA OUGANO", "1S BET WAGNER ARAUJO DE OLIVEIRA","3S LARISSA PASSOS BRAGA",
"SAMUEL BARROS PEREIRA","VENTURELLI")
### Codigo dos Militares
COD<-c("C","P","J","O","W","L","S","V")
html<-read_html(file.choose())
dados<-html_table(html, fill = TRUE)[[3]][[4]][-c(1,2)]
qtd_mes<-length(dados)/2 ; n<-seq_len(qtd_mes)
turno1a<-dados[2*n-1] ; turno2a<-dados[2*n]
dados_escala1<-data.frame(turno1a,turno2a)
m<-dim(dados_escala1)[2]
for ( j in 1:length(Nomes)) {
for ( i in 1:m) {
dados_escala1[,i]<-str_replace(dados_escala1[,i],Nomes[j],COD[j])
}
}
datatable(dados_escala1,extensions = 'Buttons',
options = list(dom='Blfrtip',
buttons=c('copy','csv','excel','pdf','print'),
lengthMenu=list(c(-1,5,7,9),c('Todas',5,7,9))),
caption = "Escala Cumprida em Janeiro de 2021")
## Extração das Etapas
extracao_etapas<-function(tipo){
cod<-c("C","P","J","O","W","L","S","V") # Identificacao em ordem hier.
escala<-dados_escala1
n<-length(cod); Info12h<- list() ; Info24h<-list()
for (i in seq_len(n)){
svc<-which(escala==cod[i],arr.ind = TRUE)[,1]
svc_12h<-sort(svc[!svc %in% svc[duplicated(svc)]])
svc_24h<-sort(svc[duplicated(svc)])
Info12h[[i]]<-matrix(svc_12h,nrow=1)
Info24h[[i]]<-matrix(svc_24h,nrow=1)
}
if (tipo==12){
dias<-rbind.fill.matrix(Info12h)
dias[is.na(dias)]<-""
}else if (tipo==24){
dias<-rbind.fill.matrix(Info24h)
dias[is.na(dias)]<-""
}
row.names(dias)<-cod
return(dias)
}
datatable(extracao_etapas(12),extensions = 'Buttons',
options = list(dom='Blfrtip',
buttons=c('copy','csv','excel','pdf','print'),
lengthMenu=list(c(-1,5,7,9),c('Todas',5,7,9))),
caption = "Dias de Serviços em Janeiro de 2021")
datatable(extracao_etapas(24),extensions = 'Buttons',
options = list(dom='Blfrtip',
buttons=c('copy','csv','excel','pdf','print'),
lengthMenu=list(c(-1,5,7,9),c('Todas',5,7,9))),
caption = "Dias de Serviços em Janeiro de 2021")
(dt = rbindlist(
lapply(l, function(x) data.table(t(x))),
fill=TRUE
))
## Militar svc_12h svc_24h
## 1: C 16,18,19,20,21,22,... 27
## 2: P 5, 8,11,14,26 2
## 3: J 4,13,16,20,22,23,... 15
## 4: O 21,25,26,30 7,17
## 5: W 5, 9,10,14,18,19 3,12,24
## 6: L 28,29,31 1
## 7: S 4, 8, 9,10,11,13,... 6
## 8: V
head(l,4) # mostrando apenas as 4 primeiras saidas
## [[1]]
## [[1]]$Militar
## [1] "C"
##
## [[1]]$svc_12h
## [1] 16 18 19 20 21 22 25 31
##
## [[1]]$svc_24h
## [1] 27
##
##
## [[2]]
## [[2]]$Militar
## [1] "P"
##
## [[2]]$svc_12h
## [1] 5 8 11 14 26
##
## [[2]]$svc_24h
## [1] 2
##
##
## [[3]]
## [[3]]$Militar
## [1] "J"
##
## [[3]]$svc_12h
## [1] 4 13 16 20 22 23 28 29 30
##
## [[3]]$svc_24h
## [1] 15
##
##
## [[4]]
## [[4]]$Militar
## [1] "O"
##
## [[4]]$svc_12h
## [1] 21 25 26 30
##
## [[4]]$svc_24h
## [1] 7 17
str(l)
## List of 8
## $ :List of 3
## ..$ Militar: chr "C"
## ..$ svc_12h: int [1:8] 16 18 19 20 21 22 25 31
## ..$ svc_24h: int 27
## $ :List of 3
## ..$ Militar: chr "P"
## ..$ svc_12h: int [1:5] 5 8 11 14 26
## ..$ svc_24h: int 2
## $ :List of 3
## ..$ Militar: chr "J"
## ..$ svc_12h: int [1:9] 4 13 16 20 22 23 28 29 30
## ..$ svc_24h: int 15
## $ :List of 3
## ..$ Militar: chr "O"
## ..$ svc_12h: int [1:4] 21 25 26 30
## ..$ svc_24h: int [1:2] 7 17
## $ :List of 3
## ..$ Militar: chr "W"
## ..$ svc_12h: int [1:6] 5 9 10 14 18 19
## ..$ svc_24h: int [1:3] 3 12 24
## $ :List of 3
## ..$ Militar: chr "L"
## ..$ svc_12h: int [1:3] 28 29 31
## ..$ svc_24h: int 1
## $ :List of 3
## ..$ Militar: chr "S"
## ..$ svc_12h: int [1:7] 4 8 9 10 11 13 23
## ..$ svc_24h: int 6
## $ :List of 3
## ..$ Militar: chr "V"
## ..$ svc_12h: int(0)
## ..$ svc_24h: int(0)
wb<-createWorkbook()
addWorksheet(wb,"sheet 1")
addWorksheet(wb,"sheet 2")
writeData(wb,1,dt,withFilter = TRUE)
writeData(wb,2,dados_escala1,withFilter = TRUE)
saveWorkbook(wb,file="Info_Etappas_jan21.xlsx")