-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Metropolitan-Council/emissions
Emissions
- Loading branch information
Showing
14 changed files
with
356 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
Type: Package | ||
Package: councilR | ||
Title: Functions and Templates for the Metropolitan Council | ||
Version: 0.1.1.9000 | ||
Date: 2022-03-11 | ||
Version: 0.1.2 | ||
Date: 2022-03-15 | ||
Authors@R: c( | ||
person("Metropolitan Council", role = "cph"), | ||
person("Liz", "Roten", , "[email protected]", role = c("cre", "aut"), | ||
comment = c(ORCID = "0000-0002-5346-3549")), | ||
person("Garrick", "Aden-Buie", , "[email protected]", role = "ctb", | ||
comment = c(ORCID = "0000-0002-7111-0077")), | ||
person("Ellen", "Esch", , "[email protected]", role = "ctb", | ||
comment = c(ORCID = "0000-0002-4253-0910")) | ||
comment = c(ORCID = "0000-0002-4253-0910")), | ||
person("Mauricio", "León", , "[email protected]", role = "ctb") | ||
) | ||
Description: A collection of commonly used templates, color palettes, | ||
functions, and more! | ||
License: MIT + file LICENSE | ||
BugReports: https://github.com/Metropolitan-Council/councilR/issues | ||
Depends: | ||
R (>= 2.10) | ||
Imports: | ||
|
@@ -39,7 +41,7 @@ Suggests: | |
repmis (>= 0.5), | ||
rmarkdown (>= 2.11), | ||
stringr (>= 1.4.0), | ||
testthat (>= 2.3.2), | ||
testthat (>= 3.0.0), | ||
tibble (>= 3.1.6), | ||
usethis (>= 1.6.1) | ||
Enhances: | ||
|
@@ -49,7 +51,9 @@ VignetteBuilder: | |
knitr | ||
Remotes: | ||
crsh/citr | ||
Config/testthat/edition: 3 | ||
Encoding: UTF-8 | ||
LazyData: true | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.1.2 | ||
Website: https://github.com/Metropolitan-Council/councilR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#' @title Emissions database table names | ||
#' | ||
#' @format Nested, named list of table names available in the CD_Emissions database | ||
#' \describe{ | ||
#' \item{mod_1}{named list of 1 table name} | ||
#' \item{mod_2}{named list of 3 table names} | ||
#' \item{mod_3}{named list of 8 table names } | ||
#' \item{metro_demos}{named list of 7 table names} | ||
#' \item{state_demos}{named list of 2 table names} | ||
#' \item{metro_energy}{named list of 9 table names} | ||
#' \item{state_energy}{named list of 1 table name} | ||
#' } | ||
#' | ||
#' @family datasets | ||
#' @examples | ||
#' library(councilR) | ||
#' emissions_db_table_names$mod_1 | ||
#' emissions_db_table_names$metro_demos | ||
# emissions_db_table_names ----- | ||
"emissions_db_table_names" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#' @title Fetch Greenhouse gas scenario planning data tables | ||
#' | ||
#' @param uid character, your network id. | ||
#' Default is `getOption("councilR.uid")`. For example, `"mc\\rotenle"` | ||
#' @param pwd character, your network password. | ||
#' Default is `getOption("councilR.pwd")`. For example, `"my_password"` | ||
#' @param serv character, database server. | ||
#' Default is `"dbsqlcl11t.test.local,65414"` (the test database). | ||
#' @param db character, database name. Default is `"CD_Emissions"` | ||
#' @param module character, which module tables to pull. One of `"mod_1"`, `"mod_2"`, | ||
#' `"mod_3"`,` "metro_demos"`, | ||
#' `"state_demos"`, `"metro_energy"`, `"state_energy"`, or `"all"` | ||
#' @param local logical, whether to pull from the onsite database or Azure. | ||
#' | ||
#' @description WARNING: Function error may results in RStudio crash. | ||
#' | ||
#' @note See `vignette("Options")` to review package options. | ||
#' You must be set up with the appropriate database drivers to use this function. | ||
#' **Windows** users need ODBC with Microsoft SQL. Contact IS support for ODBC installation. | ||
#' **Mac** users need `unixodbc` and `freetds`. See instructions in | ||
#' [`{MetroTransitR}`](https://github.com/Metropolitan-Council/MetroTransitR) | ||
#' | ||
#' @return a list of tables from the CD_Emissions database. List length depends | ||
#' `module` parameter. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' library(councilR) | ||
#' | ||
#' # set options if you haven't already | ||
#' options( | ||
#' councilR.uid = "mc\\myuid", | ||
#' councilR.pwd = "mypwd" | ||
#' ) | ||
#' | ||
#' mod_1_tables <- import_from_emissions(module = "mod_1") | ||
#' mod_2_tables <- import_from_emissions(module = "mod_2") | ||
#' mod_3_tables <- import_from_emissions(module = "mod_3") | ||
#' | ||
#' # or fetch all tables | ||
#' | ||
#' all <- import_from_emissions(module = "all") | ||
#' } | ||
#' | ||
#' @importFrom DBI dbCanConnect dbGetQuery dbConnect dbDisconnect | ||
#' @importFrom odbc odbc | ||
#' @importFrom purrr map flatten | ||
#' @importFrom utils osVersion | ||
import_from_emissions <- function(uid = getOption("councilR.uid"), | ||
pwd = getOption("councilR.pwd"), | ||
module = c( | ||
"mod_1", "mod_2", "mod_3", | ||
"metro_demos", "state_demos", | ||
"metro_energy", "state_energy", | ||
"all" | ||
), | ||
local = TRUE, | ||
serv = "dbsqlcl11t.test.local,65414", | ||
db = "CD_Emissions") { | ||
# browser() | ||
# decide which driver to use based on OS | ||
|
||
if (local == FALSE) { | ||
stop("Non-local isn't ready yet!") | ||
} | ||
|
||
drv <- if (grepl("mac", osVersion)) { | ||
"FreeTDS" | ||
} else { | ||
"SQL Server" | ||
} | ||
|
||
# check that DB connection works | ||
if ( | ||
DBI::dbCanConnect( | ||
odbc::odbc(), | ||
Driver = drv, | ||
Database = db, | ||
Uid = uid, | ||
Pwd = pwd, | ||
Server = serv | ||
) == FALSE) { | ||
stop("Database failed to connect") | ||
} | ||
|
||
|
||
tables_to_fetch <- if (module == "all") { | ||
purrr::flatten(emissions_db_table_names) | ||
} else { | ||
emissions_db_table_names[[module]] | ||
} | ||
|
||
if (length(tables_to_fetch) == 0) { | ||
stop("No matching module name") | ||
} | ||
|
||
conn <- DBI::dbConnect(odbc::odbc(), | ||
Driver = drv, | ||
Database = db, | ||
Uid = uid, | ||
Pwd = pwd, | ||
Server = serv | ||
) | ||
|
||
db_sp_tables <- purrr::map( | ||
tables_to_fetch, | ||
function(x) { | ||
DBI::dbGetQuery( | ||
conn, | ||
paste0("SELECT * FROM ", x) | ||
) | ||
} | ||
) | ||
|
||
names(db_sp_tables) <- names(tables_to_fetch) | ||
|
||
DBI::dbDisconnect(conn) | ||
|
||
return(db_sp_tables) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
## code to prepare `emissions_db_table_names` dataset goes here | ||
|
||
|
||
emissions_db_table_names <- list( | ||
# mod 1 | ||
mod_1 = list( | ||
"t_ctu_forecast" = "metro_sp_mod_1.vw_ctu_forecast" | ||
), | ||
|
||
|
||
# mod 2 | ||
mod_2 = list( | ||
"t_ztrax_building_sqft" = "metro_sp_mod_2.vw_ztrax_building_sqft", | ||
"t_ztrax_sqft_summary_ctu" = "metro_sp_mod_2.vw_ztrax_sqft_summary_ctu", | ||
"t_ztrax_sqft_summary_county" = "metro_sp_mod_2.ztrax_sqft_summary_county" | ||
), | ||
|
||
# mod 3 | ||
mod_3 = list( | ||
"aeo_factor" = "metro_sp_mod_3.aeo_factor", | ||
"aeo_scenario" = "metro_sp_mod_3.aeo_scenario", | ||
"cost_factor" = "metro_sp_mod_3.cost_factor", | ||
"ghg_factor" = "metro_sp_mod_3.ghg_factor", | ||
"mode" = "metro_sp_mod_3.mode", | ||
"pass_transpo" = "metro_sp_mod_3.pass_transpo", | ||
"sources" = "metro_sp_mod_3.sources", | ||
"variables" = "metro_sp_mod_3.variables" | ||
), | ||
|
||
# metro demos | ||
metro_demos = list( | ||
"t_ctu_population" = "metro_demographic.vw_ctu_population", | ||
"t_ctu_qcew_ctu" = "metro_demographic.vw_qcew_ctu", | ||
"t_ctu_county" = "metro_demographic.vw_ctu_county", | ||
"t_housing_stock_ctu" = "metro_demographic.vw_housing_stock_ctu", | ||
"t_emp_forecast_county" = "metro_demographic.vw_emp_forecast_county", | ||
"t_emp_forecast_ctu" = "metro_demographic.vw_emp_forecast_ctu", | ||
"t_led_industry_county" = "metro_demographic.vw_led_industry_county" | ||
), | ||
|
||
|
||
# state demos | ||
state_demos = list( | ||
"t_county" = "state_demographic.county", | ||
"t_state_qcew" = "state_demographic.vw_state_qcew" | ||
), | ||
|
||
# metro energy | ||
metro_energy = list( | ||
"t_electricity_residential_ctu" = " metro_energy.vw_electricity_residential_ctu", | ||
"t_natural_gas_residential_ctu" = "metro_energy.vw_natural_gas_residential_ctu", | ||
"t_intersect_landuse_utility_service_area_ctu" = "metro_energy.vw_intersect_landuse_utility_service_area_ctu", | ||
"t_utility_electricity_by_ctu" = "metro_energy.vw_utility_electricity_by_ctu", | ||
"t_eia_electricity_servicewide" = "metro_energy.vw_eia_electricity_servicewide", | ||
"t_mndoc_electricity_county" = "metro_energy.vw_mndoc_electricity_county", | ||
"t_intersect_landuse_utility_service_area_county" = "metro_energy.vw_intersect_landuse_utility_service_area_county", | ||
"t_nrel_energy_consumption_ctu" = "metro_energy.vw_nrel_energy_consumption_ctu", | ||
"t_utility_natural_gas_by_ctu" = "metro_energy.vw_utility_natural_gas_by_ctu" | ||
), | ||
|
||
# state energy | ||
state_energy = list( | ||
"t_eia_energy_consumption_state" = "state_energy.eia_energy_consumption_state" | ||
) | ||
) | ||
|
||
|
||
usethis::use_data(emissions_db_table_names, overwrite = TRUE) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.