-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00_load_dependencies.R
53 lines (43 loc) · 1.9 KB
/
00_load_dependencies.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
## Load Libraries -----------------------------------------------
#' NOTE: The code below is intended to load all listed libraries. If you do not
#' have these libraries on your computer, the code will attempt to INSTALL them.
#'
#' IF YOU DO NOT WANT TO INSTALL ANY OF THESE PACKAGES, DO NOT RUN THIS CODE.
list.of.packages <- c("tidyverse", "janitor", "sf", "vroom",
"leaflet", "htmltools", "readxl", "censusapi",
'raster', 'rgdal', # for spTransform
'htmlwidgets','colorRamps', 'leaflet.extras',
'lwgeom')
# checks if packages has been previously installed
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
# if not, packages are installed
if(length(new.packages)) install.packages(new.packages)
# packages are loaded
lapply(list.of.packages, require, character.only = TRUE)
# function to unzip shapefiles
unzip_sf <- function(zip_url) {
temp <- tempfile()
temp2 <- tempfile()
#download the zip folder from the internet save to 'temp'
download.file(zip_url, temp)
#unzip the contents in 'temp' and save unzipped content in 'temp2'
unzip(zipfile = temp, exdir = temp2)
#if returns "character(0), then .shp may be nested within the folder
your_SHP_file <- ifelse(!identical(list.files(temp2, pattern = ".shp$",full.names=TRUE), character(0)),
list.files(temp2, pattern = ".shp$",full.names=TRUE),
list.files(list.files(temp2, full.names=TRUE), pattern = ".shp$", full.names = TRUE))
unlist(temp)
unlist(temp2)
return(your_SHP_file)
}
# function to download file
file_dl <- function(url) {
temp <- tempfile()
#download the file from the internet save to 'temp'
download.file(url, temp)
your_TIF_file <- temp
unlist(temp)
return(your_TIF_file)
}
# avoid sci notation
options(scipen = 999)