Last active
November 5, 2018 03:46
-
-
Save muschellij2/b3487583a3396f691273ec88b3ea9562 to your computer and use it in GitHub Desktop.
Getting data from https://webapp.psc.state.md.us/ecm/web/Compare_new1.cfm
This file contains hidden or 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
# see http://www.opc.state.md.us/RegulatoryActivities/Publications.aspx | |
# https://webapp.psc.state.md.us/ecm/web/Compare_new1.cfm | |
library(rvest) | |
library(dplyr) | |
# doc = read_html("rates.html") | |
# tab = html_table(doc, header = TRUE)[[1]] | |
# tab = tab %>% | |
# rename( | |
# name = `Company Name3`, | |
# price = `Price/KWH`, | |
# estimated_charge = `Estimated Monthly Supply Charge`, | |
# plan_type = `Type of Plan5`, | |
# intro = `Intro Offer`, | |
# duration = `Term Duration (in Months)`, | |
# pct_renewable = `% Renewable4`, | |
# cancellation_fee = `Cancellation Fee`, | |
# incentives = `Incentives/Other Info` | |
# ) | |
# tab = tab %>% mutate( | |
# price = sub("$", "", price, fixed = TRUE), | |
# price = as.numeric(price) | |
# ) %>% | |
# arrange(price) | |
location = "BGE" | |
kwh = 1000 | |
get_estimates =function( | |
kwh = 1000, | |
location = c("BGE", "Choptank", "Delmarva", "Pepco", "Potomac Edison", | |
"SMECO") | |
) { | |
location = match.arg(location) | |
res = httr::POST( | |
url = "http://webapp.psc.state.md.us/ecm/web/Compare_new1.cfm", | |
body = list(Elocation = location, | |
estimates = kwh, | |
submit = " Submit "), | |
encode = 'form') | |
doc = content(res) | |
nodes = doc %>% | |
html_nodes(css = ".text .text .text tr") | |
x = c("<html>", "<body>", "<table>", "<tbody>", | |
# paste("<tr>", | |
as.character(nodes), | |
# "</tr>"), | |
"</tbody>", "</table>", "</body>", "</html>") | |
tfile = tempfile(fileext = ".html") | |
writeLines(x, tfile) | |
doc = read_html(tfile) | |
tab = html_table(doc, header = TRUE) | |
tab = tab[[1]] | |
tab = tab %>% | |
rename( | |
name = `Company Name3`, | |
price = `Price/KWH`, | |
estimated_charge = `Estimated Monthly Supply Charge`, | |
plan_type = `Type of Plan5`, | |
intro = `Intro Offer`, | |
duration = `Term Duration (in Months)`, | |
pct_renewable = `% Renewable4`, | |
cancellation_fee = `Cancellation Fee`, | |
incentives = `Incentives/Other Info` | |
) | |
tab = tab %>% mutate( | |
price = sub("$", "", price, fixed = TRUE), | |
price = as.numeric(price), | |
estimated_charge = sub("$", "", estimated_charge, fixed = TRUE), | |
estimated_charge = as.numeric(estimated_charge), | |
cancellation_fee = recode(cancellation_fee, | |
"No" = FALSE, | |
"Yes" = TRUE) | |
) %>% | |
arrange(price) | |
return(tab) | |
} | |
tab = get_estimates() | |
tab = tab %>% | |
filter(!cancellation_fee, | |
plan_type == "Fixed") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment