diff --git a/.gitignore b/.gitignore index 1b0f9bf..36c8d8c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,16 @@ .DS_Store .python-version +.Renviron +.Rhistory +.RData node_modules +tests/*.zip +tests/*.nb.html +tests/*_results/ + vignettes/*.html vignettes/*.R vignettes/output +.Rproj.user diff --git a/README.md b/README.md index 7416b8e..b2ab81f 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,14 @@ Rscript -e "testthat::test_dir('tests')" ## Releasing new version +- Open `bysync.Rproj` in RStudio +- In RStudio, format all the R files by running the following commands in RStudio + +```R +install.packages("styler") +styler::style_dir() +``` + - Create a branch with the prepared release change log. - Make sure the rnoaa and nmecr versions in `setup_environment.R` and ` are correct. - Update version in bsync.RProj and DESCRIPTION to the next correct semantic version diff --git a/bsyncr.Rproj b/bsyncr.Rproj index fb44373..a1aa64e 100644 --- a/bsyncr.Rproj +++ b/bsyncr.Rproj @@ -1,4 +1,5 @@ -Version: 0.1 +Version: 1.0 +ProjectId: b4573bbf-3288-46a6-89f8-fe2dc7103016 RestoreWorkspace: Default SaveWorkspace: Default diff --git a/setup_environment.R b/setup_environment.R index 53000d8..672cc08 100644 --- a/setup_environment.R +++ b/setup_environment.R @@ -7,8 +7,7 @@ cat(getwd(), "\n\n") # Install required packages if not already installed required_packages <- c( - "remotes", "crayon", "dplyr", "tidyr", "crul", "xml2", "testthat", "anytime", "lubridate", "segmented", "xts", "zoo", - "ggplot2", "scales", "XML", "rappdirs", "gridExtra", "isdparser", "geonames", "hoardr", "data.table" + "remotes", "crayon", "dplyr", "tidyr", "crul", "xml2", "testthat", "anytime", "lubridate", "segmented", "xts", "zoo", "ggplot2", "scales", "XML", "rappdirs", "gridExtra", "isdparser", "geonames", "hoardr", "data.table" ) cat("Checking and installing required packages...\n") diff --git a/tests/bsyncr_example.Rmd b/tests/bsyncr_example.Rmd new file mode 100644 index 0000000..e4d6cd1 --- /dev/null +++ b/tests/bsyncr_example.Rmd @@ -0,0 +1,85 @@ +--- +title: "bsyncr Functionality Demonstration" +output: html_notebook +--- + +Create a .Renviron file in the project folder and set your NOAA_TOKEN + +e.g., + +NOAA_TOKEN=xyz123 + + +```{r setup, include=FALSE} +# set the working directory to the project folder, not the location of this file. +setwd("..") + +# install the dependencies +source("./setup_environment.R") + +# Load required libraries +library(xml2) +library(rnoaa) +library(lubridate) +library(dplyr) +library(ggplot2) + + +# Source the utility functions +source("./R/bsync_utils.R") + +# get root path +root_path <- getwd() + +# Ensure NOAA token is set +NOAA_TOKEN <- Sys.getenv('NOAA_TOKEN') +if (NOAA_TOKEN == "") { + stop("Missing NOAA token env var: NOAA_TOKEN") +} +options(noaakey = NOAA_TOKEN) +``` + +```{r} + +# Path to the test file +# bsync path from root path +bsync_filepath <- file.path(root_path, "tests", "data", "ex_bsync.xml") + + +baseline_scenario_id <- "Scenario-bsyncr" +bsync_doc <- xml2::read_xml(bsync_filepath) %>% + bsyncr::bs_stub_scenarios(linked_building_id = "My-Fav-Building", baseline_id = baseline_scenario_id) + +baseline_xpath <- sprintf("//auc:Scenario[@ID = '%s']", baseline_scenario_id) +sc_baseline <- xml2::xml_find_first(bsync_doc, baseline_xpath) +not_used <- sc_baseline %>% bsyncr::bs_stub_derived_model(dm_id = "DerivedModel-bsyncr", + dm_period = "Baseline") + +b_df <- bsyncr::bs_parse_nmecr_df(bsync_doc, insert_weather_data = TRUE) +``` + +```{r} +# Create an SLR model +model <- nmecr::model_with_SLR(b_df, nmecr::assign_model_inputs(regression_type = "SLR")) + +model_df <- model$training_data %>% + tidyr::gather(key = "variable", value = "value", c("eload", "model_fit")) + +print(model_df) + +# add in the linear regression line from the model results, need to +# confirm, but it looks like model is in BTU and °C +intercept = model$model$coefficients[["(Intercept)"]] / 3.41214 # btu to kwh +# Model is in °C, so convert to F. +slope = model$model$coefficients[["temp"]] * 9/5 # °C to °F + +ggplot2::ggplot(model_df, aes(x = temp, y = value)) + + geom_point(aes(color = variable), data=model_df[model_df$variable == "eload",]) + + geom_line(aes(color = variable), data=model_df[model_df$variable == "model_fit",]) + + geom_abline(intercept = intercept, slope = slope, color = "red", linetype = "dashed") + + xlab("Temperature") + + scale_y_continuous(name = "Energy Data & Model Fit (kWh)", labels = scales::comma) + + theme_minimal() + + theme(legend.position = "bottom") + + theme(legend.title = element_blank()) +```