forked from leedrake5/Russia-Ukraine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.R
162 lines (129 loc) · 5.12 KB
/
global.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
list.of.packages <- c("shinythemes", "ggplot2", "scales", "data.table", "magrittr", "dplyr", "tidyr", "lubridate", "zoo", "DT", "R.utils")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) lapply(new.packages, function(x) install.packages(x, repos="http://cran.rstudio.com/", dep = TRUE, ask=FALSE, type="binary"))
library(ggplot2)
library(data.table)
library(magrittr)
library(dplyr)
library(tidyr)
library(lubridate)
library(zoo)
library(shinythemes)
library(DT)
library(R.utils)
full_data <- read.csv(paste0("data/bySystem/Raw/Full/", Sys.Date(), ".csv"))
dates = seq(as.Date("2022-02-24"), Sys.Date(), by="days")
daily_list <- list()
for(i in dates){
daily_list[[as.Date(i, format="%Y-%m-%d", origin="1970-01-01")]] <- read.csv(paste0("data/bySystem/Raw/Daily/", as.Date(i, format="%Y-%m-%d", origin="1970-01-01"), ".csv"))
}
daily_frame <- rbindlist(daily_list, use.names=TRUE, fill=TRUE)
daily_frame <- as.data.frame(merge(daily_frame, read.csv("data/classes.csv"), by="system", all=TRUE))
daily_frame <- daily_frame[,!colnames(daily_frame) %in% c("date_recorded", "X")]
daily_frame <- daily_frame[!is.na(daily_frame$status),]
total_by_system_wide <- function(indsn){
tidy_frame <- indsn %>% dplyr::select(country, system, class, status, Date) %>%
dplyr::group_by(country, system, class, status, Date) %>%
dplyr::summarise(count = dplyr::n()) %>%
tidyr::pivot_wider(names_from = status, values_from = count) %>%
dplyr::ungroup()
if(!"destroyed" %in% names(tidy_frame)){
tidy_frame$destroyed <- 0
}
if(!"captured" %in% names(tidy_frame)){
tidy_frame$captured <- 0
}
if(!"abandoned" %in% names(tidy_frame)){
tidy_frame$abandoned <- 0
}
if(!"damaged" %in% names(tidy_frame)){
tidy_frame$damaged <- 0
}
tidy_frame <- tidy_frame %>% dplyr::mutate(dplyr::across(where(is.numeric), ~ tidyr::replace_na(.x, 0)),
total = destroyed + captured + damaged + abandoned)
return(tidy_frame)
}
date_crunch <- function(indsn){
tidy_frame <- indsn %>% dplyr::select(country, system, class, status, Date) %>%
dplyr::group_by(country, status, Date) %>%
dplyr::summarise(count = dplyr::n()) %>%
tidyr::pivot_wider(names_from = status, values_from = count) %>%
dplyr::ungroup()
if(!"destroyed" %in% names(tidy_frame)){
tidy_frame$destroyed <- 0
}
if(!"captured" %in% names(tidy_frame)){
tidy_frame$captured <- 0
}
if(!"abandoned" %in% names(tidy_frame)){
tidy_frame$abandoned <- 0
}
if(!"damaged" %in% names(tidy_frame)){
tidy_frame$damaged <- 0
}
tidy_frame <- tidy_frame %>% dplyr::mutate(dplyr::across(where(is.numeric), ~ tidyr::replace_na(.x, 0)),
total = destroyed + captured + damaged + abandoned)
return(tidy_frame)
}
#' totals_by_type
#' @description Gets data by system category.
#'
#' @return a tibble
totals_by_type <- function(link="https://www.oryxspioenkop.com/2022/02/attack-on-europe-documenting-equipment.html", date=NULL) {
if(is.null(date)){
date <- format(Sys.Date(), "%m/%d/%Y")
}
heads <-
get_data(
link,
"article div"
) %>%
rvest::html_elements("h3") %>%
rvest::html_text2()
# Drop the empty cell padding
heads <- heads[nchar(heads) > 0]
# Get the positons of the Russia and Ukraine headers
rus_pos <- heads %>% stringr::str_which("Russia") %>% as.double()
ukr_pos <- heads %>% stringr::str_which("Ukraine") %>% as.double()
totals <- tibble(
country = character(),
equipment = character(),
destroyed = character(),
abandoned = character(),
captured = character(),
damaged = character()
)
for (l in seq_along(heads)) {
totals[l, "equipment"] <-
heads[l] %>% stringr::str_remove_all(" \\(.*\\)")
totals[l, "destroyed"] <-
heads[l] %>% stringr::str_extract("destroyed: \\d+") %>%
stringr::str_remove_all("[:alpha:]|[:punct:]")
totals[l, "abandoned"] <-
heads[l] %>% stringr::str_extract("(abandoned|aboned): \\d+") %>%
stringr::str_remove_all("[:alpha:]|[:punct:]")
totals[l, "captured"] <-
heads[l] %>% stringr::str_extract("captured: \\d+") %>%
stringr::str_remove_all("[:alpha:]|[:punct:]")
totals[l, "damaged"] <-
heads[l] %>% stringr::str_extract("damaged: \\d+") %>%
stringr::str_remove_all("[:alpha:]|[:punct:]")
}
totals_df <- totals %>%
dplyr::mutate(
dplyr::across(destroyed:damaged, ~ as.double(tidyr::replace_na(.x, "0"))),
type_total = destroyed + abandoned + captured + damaged,
row_id = 1:n(),
country = dplyr::case_when(row_id < ukr_pos ~ "Russia",
row_id >= ukr_pos ~ "Ukraine")
) %>%
select(-row_id) %>%
dplyr::mutate(
equipment = replace(equipment, rus_pos, "All Types"),
equipment = replace(equipment, ukr_pos, "All Types")
) %>%
dplyr::rename(equipment_type = equipment)
totals_df <- as.data.frame(totals_df)
totals_df$Date <- date
return(totals_df)
}