Skip to content

Commit 4422a3d

Browse files
Merge pull request #57 from California-Data-Collaborative/develop
removed hardcoding and fixed bugs
2 parents db3f294 + 92dda21 commit 4422a3d

File tree

12 files changed

+52
-65
lines changed

12 files changed

+52
-65
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Description: The RateComparison package is an R Shiny application for exploring
44
revenue, demand and equity impacts caused by a shift in water rates. The app compares
55
a baseline rate structure (usually the utility's current/historical rates) against
66
a hypothetical rate structure specified by the user.
7-
Version: 0.2.0
7+
Version: 0.3.0
88
Imports:
99
dplyr,
1010
tidyr,

R/class_graphs.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ classGraphOutput <- function(id, rate_codes){
108108
)
109109
)
110110
),#end row
111-
fluidRow(paste("Enter the starting value for each tier either as a CCF value, ",
111+
fluidRow(paste("Enter the starting value for each tier either as a billing unit value, ",
112112
" or as a percent of budget (water budget assumed as Indoor + Outdoor). ",
113113
"Where:")
114114
),
@@ -172,7 +172,7 @@ classGraph <- function(input, output, session, cust_class, df_original, df_total
172172

173173
rate_part_name2 <- "flat_rate"
174174
input_list[[rate_part_name2]] <- callModule(ratePart, rate_part_name2,
175-
part_name=rate_part_name2, part_name_long="Charge per CCF",
175+
part_name=rate_part_name2, part_name_long="Charge per unit",
176176
depends_col_list=dropdown_cols,
177177
value_map = rate_list()$rate_structure[[active_class()]][[rate_part_name2]]$values)
178178

@@ -208,15 +208,15 @@ classGraph <- function(input, output, session, cust_class, df_original, df_total
208208

209209
rate_part_name7 <- "tiered_starts"
210210
input_list[[rate_part_name7]] <- callModule(tierBox, rate_part_name7,
211-
part_name=rate_part_name7, part_name_long="Tier Starts (CCF)",
211+
part_name=rate_part_name7, part_name_long="Tier Starts (billing units)",
212212
rate_type=rate_list()$rate_structure[[active_class()]][["commodity_charge"]],
213213
rate_type_provided="Tiered",
214214
rate_part=rate_list()$rate_structure[[active_class()]][["tier_starts"]]
215215
)
216216

217217
rate_part_name8 <- "budget_starts"
218218
input_list[[rate_part_name8]] <- callModule(tierBox, rate_part_name8,
219-
part_name=rate_part_name8, part_name_long="Tier Starts (CCF)",
219+
part_name=rate_part_name8, part_name_long="Tier Starts (billing units)",
220220
rate_type=rate_list()$rate_structure[[active_class()]][["commodity_charge"]],
221221
rate_type_provided="Budget",
222222
rate_part=rate_list()$rate_structure[[active_class()]][["tier_starts"]]

R/helper_fns.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,19 @@ get_monthly_usage_by_account <- function(data){
8888
group_by(cust_id,usage_month) %>%
8989
summarise(usage_ccf = mean(usage_ccf,na.rm=TRUE))
9090

91+
customer_means <- data %>%
92+
group_by(cust_id) %>%
93+
summarise(customer_usage = mean(usage_ccf,na.rm=TRUE))
94+
9195
rate_code_means <- data %>%
9296
group_by(rate_code, usage_month) %>%
9397
summarise(rate_code_usage = mean(usage_ccf,na.rm=TRUE))
9498

95-
9699
df_out <- months %>% left_join(cust_id_with_rate_code, by="cust_id") %>%
97100
left_join(df_out, by=c("usage_month", "cust_id")) %>%
98101
left_join(rate_code_means, by=c("usage_month", "rate_code")) %>%
102+
left_join(customer_means, by=c("cust_id")) %>%
103+
mutate(usage_ccf = ifelse(is.na(usage_ccf), customer_usage, usage_ccf)) %>%
99104
mutate(usage_ccf = ifelse(is.na(usage_ccf), rate_code_usage, usage_ccf)) %>%
100105
dplyr::select(usage_month, cust_id, usage_ccf) %>%
101106
distinct(cust_id, usage_month, .keep_all=TRUE)

R/make_plots.R

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ plot_revenue_over_time <- function(data, display_type){
5454
monthly_usage <- data %>% group_by(usage_date) %>%
5555
summarise(hypothetical_usage=sum(hypothetical_usage, na.rm=TRUE),
5656
baseline_usage=sum(baseline_usage, na.rm=TRUE)) %>%
57-
mutate(Baseline = baseline_usage/1000000) %>%
58-
mutate(Hypothetical = hypothetical_usage/1000000) %>%
57+
mutate(Baseline = baseline_usage*af_conversion/1000) %>%
58+
mutate(Hypothetical = hypothetical_usage*af_conversion/1000) %>%
5959
select(usage_date,Baseline,Hypothetical)
6060
monthly_usage <- melt(monthly_usage, id="usage_date") %>% rename(Usage=variable)
6161

@@ -71,7 +71,7 @@ plot_revenue_over_time <- function(data, display_type){
7171
#geom_vline(xintercept=as.numeric(max(df$usage_date)),color='red3',linetype=2) +
7272
scale_linetype_manual(values = c("Baseline"="dashed", "Hypothetical"="solid")) +
7373
scale_color_manual(values=c("Baseline"="black", "Hypothetical"="steelblue")) +
74-
xlab("") + ylab("Usage (Million ccf)") +
74+
xlab("") + ylab("Usage (Thousand AF)") +
7575
# theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
7676
# scale_x_date(labels = date_format("%m-%y"), date_breaks="1 months") +
7777
scale_y_continuous(labels = comma)
@@ -130,7 +130,7 @@ plot_bill_change_histogram <- function(data, display_type, bar_type){
130130
p <- ggplot() +
131131
geom_vline(xintercept = 0, color="#CC0000")
132132
if(bar_type == "Absolute"){
133-
p <- p + xlab("Change in total amount used (ccf)")
133+
p <- p + xlab(paste("Change in total amount used (",unit_type,")") )
134134
}else{
135135
p <- p + xlab("% Change in total amount used")
136136
}
@@ -140,7 +140,7 @@ plot_bill_change_histogram <- function(data, display_type, bar_type){
140140
geom_vline(xintercept = mean(data$changes_in_usage, na.rm=TRUE), color="#CC0000") +
141141
theme(axis.ticks = element_blank(), axis.text.y = element_blank())
142142
if(bar_type == "Absolute"){
143-
p <- p + xlab("Change in total amount used (ccf)") + ylab("")
143+
p <- p + xlab(paste("Change in total amount used (",unit_type,")") ) + ylab("")
144144
}else{
145145
p <- p + xlab("% Change in total amount used") + ylab("")
146146
}
@@ -217,6 +217,9 @@ plot_barchart_by_tiers <- function(data, display_type, bar_type){
217217
if(!("TR1" %in% names(data)) || (sum(data$TR1, na.rm=TRUE) == 0 && sum(data$variable_ped_bill, na.rm=TRUE) > 0)){
218218
data$TR1 <- data$variable_ped_bill
219219
}
220+
if(!("BR1" %in% names(data)) || (sum(data$BR1, na.rm=TRUE) == 0 && sum(data$baseline_variable_bill, na.rm=TRUE) > 0)){
221+
data$BR1 <- data$baseline_variable_bill
222+
}
220223
# Select revenue in each tier
221224
d <- colSums(data %>% select(matches("[B|T]R[0-9]")), na.rm=TRUE)
222225
d <- tbl_df(data.frame(lapply(d, function(x) t(data.frame(x))))) %>%
@@ -229,8 +232,11 @@ plot_barchart_by_tiers <- function(data, display_type, bar_type){
229232
}
230233
else{
231234
# flat rates leave TR1 as null so need to populate it
232-
if(!("T1" %in% names(data)) || (sum(data$T1, na.rm=TRUE) == 0 && sum(data$variable_ped_bill, na.rm=TRUE) > 0)){
233-
data$X1 <- data$hypothetical_usage
235+
if(!("T1" %in% names(data)) || (sum(data$T1, na.rm=TRUE) == 0 && sum(data$hypothetical_usage, na.rm=TRUE) > 0)){
236+
data$T1 <- data$hypothetical_usage
237+
}
238+
if(!("B1" %in% names(data)) || (sum(data$B1, na.rm=TRUE) == 0 && sum(data$baseline_usage, na.rm=TRUE) > 0)){
239+
data$B1 <- data$baseline_usage
234240
}
235241
# Select usage in each tier
236242
d <- colSums(data %>% select(matches("[B|T][0-9]")), na.rm=TRUE)
@@ -239,7 +245,7 @@ plot_barchart_by_tiers <- function(data, display_type, bar_type){
239245
d <- melt(d, id.vars="id" ) %>%
240246
mutate(type=ifelse(grepl("B.*", variable), "Baseline", "Hypothetical"),
241247
Tier = get_tier_name(variable),
242-
value = value*0.00229569/1000)
248+
value = value*af_conversion/1000)
243249
lab_str <- "Usage During Time Period (Thousand AF)"
244250
}
245251

R/utility_code.R

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ It's easy to get the tool running locally using [data](https://data.smgov.net/Pu
1919

2020
3. Clone this repository.
2121

22+
4. Copy the three files in the `smc_example` directory into the `data` directory.
23+
2224
4. Run the Shiny app, either [from the command line](http://shiny.rstudio.com/articles/running.html) or using the "Run App" button from within RStudio.
2325

2426
\**The data provided in this github has been modified by dropping unneeded columns and by renaming others.*

global.R

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ library(lubridate)
66
library(data.table)
77

88
#set the utility_code from the config file
9-
source("R/utility_code.R")
9+
unit_type <- NULL
10+
source("data/config.R")
11+
if(!is.null(unit_type) && unit_type == "kgal"){
12+
af_conversion <- 0.00306889
13+
}else{
14+
unit_type <- "ccf"
15+
af_conversion <- 0.00229569
16+
}
17+
18+
1019
source("R/helper_fns.R", local=TRUE)
1120
source("R/make_plots.R", local=TRUE)
12-
1321
source("R/class_graphs.R")
1422
source("R/rate_inputs.R")
1523

@@ -39,11 +47,11 @@ read_data <- function(filename, cust_col, usage_col, month_col, year_col, et_col
3947
dplyr::mutate(cust_class = as.character(cust_class)) %>%
4048
dplyr::mutate(water_type = as.character(water_type)) %>%
4149
dplyr::mutate(meter_size = as.character(meter_size)) %>%
42-
dplyr::filter(usage_date < as.Date(less_than_date)) %>%
50+
# dplyr::filter(usage_date < as.Date(less_than_date)) %>%
4351
dplyr::arrange(usage_date, cust_class) %>%
4452
dplyr::mutate(sort_index=1:nrow(.))
4553

46-
if(!is.null(et_col)&!is.null(hhsize_col)&!is.null(irr_area_col)){
54+
if( all( c(et_col, hhsize_col, irr_area_col) %in% names(data)) ){
4755
data <- data %>%
4856
dplyr::rename_(.dots=setNames(list(et_col), "et_amount")) %>%
4957
dplyr::rename_(.dots=setNames(list(hhsize_col), "hhsize")) %>%
@@ -64,55 +72,21 @@ read_data <- function(filename, cust_col, usage_col, month_col, year_col, et_col
6472
generated_inputs <- list()
6573

6674

67-
owrs_file <- switch(utility_code,
68-
"IRWD"="",
69-
"MNWD"="mnwd.owrs",
70-
"LVMWD"="data/lvmwd_simplified.owrs",
71-
"SMWD"="data/smwd-2017-01-01_simplified.owrs",
72-
"SMC"="data/smc-2017-01-01_simplified.owrs",
73-
"EMWD"="data/emwd_simplified.owrs")
74-
75+
owrs_file <- "data/ratefile.owrs"
7576
baseline_rate_list <- RateParser::read_owrs_file(owrs_file)
7677

77-
is_budget <- switch(utility_code,
78-
"IRWD"=,
79-
"MNWD"=,
80-
"LVMWD"=,
81-
"EMWD"=,
82-
"SMWD"=TRUE,
83-
"SMC"=FALSE)
84-
85-
less_than_date <- switch(utility_code,
86-
"IRWD"="2017-01-01",
87-
"MNWD"="2017-01-01",
88-
"LVMWD"="2017-01-01",
89-
"SMWD"="2016-01-01",
90-
"SMC"="2017-01-01",
91-
"EMWD"="2017-01-01")
92-
93-
test_file <- switch(utility_code,
94-
"IRWD"="data/irwd_test.csv",
95-
"MNWD"="data/mnwd_sample_revised.csv",
96-
"LVMWD"="data/lvmwd_test2_comm_budgets_monthly.csv",
97-
"SMWD"="data/smwd_test2.csv",
98-
"SMC"="data/smc_test2.csv",
99-
"EMWD"="data/emwd_sample.csv")
78+
data_file <- "data/data.csv"
10079

10180

10281

10382
# Read data from file and rename the columns to be compatable with internal calls
104-
if(is_budget){
105-
df <- read_data(test_file, cust_col="cust_loc_id", usage_col="usage_ccf", month_col="usage_month",
106-
year_col="usage_year", et_col="usage_et_amount", hhsize_col="cust_loc_hhsize",
107-
irr_area_col="cust_loc_irr_area_sf", cust_class_col= "cust_loc_rate_class",
108-
rate_code_col = "cust_loc_class_from_utility", water_type_col="cust_loc_water_type",
109-
meter_size_col="cust_loc_meter_size", less_than_date=less_than_date)
110-
} else{
111-
df <- read_data(test_file, cust_col="cust_loc_id", usage_col="usage_ccf", month_col="usage_month",
112-
year_col="usage_year", cust_class_col= "cust_loc_rate_class",
113-
rate_code_col = "cust_loc_class_from_utility", water_type_col="cust_loc_water_type",
114-
meter_size_col="cust_loc_meter_size", less_than_date=less_than_date)
115-
}
83+
df <- read_data(data_file, cust_col="cust_loc_id", usage_col="usage_ccf", month_col="usage_month",
84+
year_col="usage_year", et_col="usage_et_amount", hhsize_col="cust_loc_hhsize",
85+
irr_area_col="cust_loc_irr_area_sf", cust_class_col= "cust_loc_rate_class",
86+
rate_code_col = "cust_loc_class_from_utility", water_type_col="cust_loc_water_type",
87+
meter_size_col="cust_loc_meter_size")
88+
is_budget <- all( c("et_amount", "hhsize", "irr_area") %in% names(data))
89+
11690

11791
#error checking of data
11892
check_duplicated_rate_codes(df)

server.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ shinyServer(function(input, output, clientData, session) {
478478

479479

480480
#******************************************************************
481-
# Calculate bills and tiers for the MNWD residential baseline rate
481+
# Calculate bills and tiers for the baseline rate
482482
#******************************************************************
483483
baseline_bill_info <- reactive({
484484
baseline(basedata=DF())

smc_example/config.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
utility_code <- "SMC"
File renamed without changes.

0 commit comments

Comments
 (0)