-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.R
117 lines (94 loc) · 2.37 KB
/
init.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
# Most of this package assumes my private R package initR is pre-installed
# and an existing PostgreSQL environment is running on your local
# machine and/or network.
# Packages & Init Setup ---------------------------------------------------
proj_name <- 'baseball'
# setwd('~/Documents/dev/baseball')
# devtools::install_github("BillPetti/baseballr")
pkgs <- c(
'devtools',
'tidyverse',
'RPostgres',
'RPostgreSQL',
# 'RMariaDB',
'DBI',
'readr',
'pander',
'na.tools',
'devtools',
'teamcolors',
'glue',
'dplyr',
'rvest',
'arrow',
'RCurl',
'tictoc',
'animation',
'gt',
'DT',
'ggimage',
'ggpubr',
'ggthemes',
'bbplot',
'ggtext',
'ggforce',
'ggridges',
'ggrepel',
'ggbeeswarm',
'extrafont',
'RCurl',
'xml2',
'rvest',
'jsonlite',
'foreach',
'lubridate',
'snakecase',
'baseballr',
'initR',
NULL
)
installed_packages <- pkgs %in%
rownames(installed.packages())
if (any(installed_packages == FALSE)) {
install.packages(pkgs[!installed_packages])
}
if (any('bbplot' %in%
rownames(installed.packages()) == FALSE)) {
library(devtools)
devtools::install_github('bbc/bbplot')
}
invisible(lapply(pkgs, function(x) {
suppressMessages(suppressWarnings(library(
x,
warn.conflicts = FALSE,
quietly = TRUE,
character.only = TRUE
)))
}))
rm(pkgs, installed_packages)
'%notin%' <- Negate('%in%')
options(tibble.print_min=25)
# Initialize Working Directory --------------------------------------------
initR::fx.setdir(proj_name)
# Create standard objects -------------------------------------------------
# Connect to DB
con <- initR::fx.db_con(x.host = 'localhost')
current_season <- 2022
year <- substr(Sys.Date(), 1, 4)
date <- Sys.Date()
today <- format(Sys.Date(), '%Y-%d-%m')
source('plots/assets/plot_theme.R', echo = F)
source('scripts/statcast.R', echo = F)
# Scrape latest statcast data
map(.x = 2022,
~{payload_statcast <- annual_statcast_query(season = .x)
message(paste0('Formatting payload for ', .x, '...'))
df <- format_append_statcast(df = payload_statcast)
message(paste0('Deleting and uploading ', .x, ' data to database...'))
delete_and_upload(df,
year = .x,
con = initR::fx.db_con(x.host = 'localhost'))
message('Sleeping and collecting garbage...')
# Sys.sleep(5*60)
gc()
})