mirror of
https://github.com/dylanbenzi/hurricane_normalization_app.git
synced 2026-07-30 05:08:57 +00:00
add queries.R to move query functions to separate file
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
# DB queries for shiny app
|
||||
|
||||
library(tidyverse)
|
||||
library(dplyr)
|
||||
library(DBI)
|
||||
|
||||
linuxdir <- "/home/dylan/Personal/Projects/Hurricane Normalization/"
|
||||
macdir <- "~/Desktop/Personal/Projects/Hurricane Normalization/"
|
||||
#baseDir <- macdir
|
||||
baseDir <- linuxdir
|
||||
|
||||
config <- config::get(file = paste0(baseDir, "R/dataScripts/restructured/app/config.yml"))
|
||||
|
||||
# SUPABASE CON
|
||||
con <- dbConnect(
|
||||
RPostgres::Postgres(),
|
||||
host = config$db_host,
|
||||
port = config$db_port,
|
||||
dbname = config$db_dbname,
|
||||
user = config$db_user,
|
||||
password = config$db_password
|
||||
)
|
||||
|
||||
# lazy load DB tables
|
||||
|
||||
econ.normalized_landfalls <- tbl(con, I("econ.normalized_landfalls"))
|
||||
econ.storm_base_loss <- tbl(con, I("econ.storm_base_loss"))
|
||||
econ.usa_yearly <- tbl(con, I("econ.usa_yearly"))
|
||||
|
||||
fatal.storm_fatalities_type <- tbl(con, I("fatal.storm_fatalities_type"))
|
||||
fatal.storm_total_fatalities <- tbl(con, I("fatal.storm_total_fatalities"))
|
||||
|
||||
fips.counties <- tbl(con, I("fips.counties"))
|
||||
fips.states <- tbl(con, I("fips.states"))
|
||||
|
||||
gis.affected_area_landfalls <- tbl(con, I("gis.affected_area_landfalls"))
|
||||
|
||||
hurdat.best_track <- tbl(con, I("hurdat.best_track"))
|
||||
hurdat.hurdat_storms <- tbl(con, I("hurdat.hurdat_storms"))
|
||||
|
||||
metrics.geo_attributes <- tbl(con, I("metrics.geo_attributes"))
|
||||
metrics.pop_and_housing <- tbl(con, I("metrics.pop_and_housing"))
|
||||
|
||||
public.counties <- tbl(con, I("public.counties"))
|
||||
|
||||
# returns a list of loss storms we have data on
|
||||
get_all_loss_storms <- function() {
|
||||
query <- econ.storm_base_loss %>%
|
||||
select(
|
||||
storm_basin, storm_year, storm_name
|
||||
) %>%
|
||||
distinct(
|
||||
storm_basin, storm_year, storm_name
|
||||
) %>%
|
||||
left_join(
|
||||
hurdat.hurdat_storms %>%
|
||||
mutate(hurdatId = paste0(storm_basin, storm_number, storm_year)),
|
||||
by = c("storm_basin", "storm_year", "storm_name")) %>%
|
||||
select(
|
||||
hurdatId, storm_basin, storm_name, storm_year
|
||||
)
|
||||
|
||||
result <- query %>% collect()
|
||||
|
||||
return(result)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user