Mike Badescu, PhD
Dallas R Users Group
November 17, 2018
November 17, 2018
Mike Badescu, PhD
Dallas R Users Group
November 17, 2018
Status: Deregulated
Shopping for electricity involves:
Power To Choose website
Note: Several companies offer electricity comparison tools
Ultimately, electricity usage is consumer specific!
Smart Meter Texas portal
Five easy steps:
httr
, rvest
, jsonlite
DBI
, RPostgres
, aws.s3
pdftools
, tidyverse
Shiny
, ggplot2
, DT
, …future
, plumber
loggr
, config
, fs
, pool
jsonb
fields ==> can be indexedrmarkdown
reports
Simple (Synchronous) Shiny
source: Joe Cheng / RStudio
Simple (Synchronous) Shiny
plan_info_tdsp <- reactive({ db_get_plan_info(user_tdsp = user_tdsp) # connects to DB }) valid_res <- reactive({ plan_info_tdsp() %>% dplyr::filter(status %in% "valid") %>% calc_cost_multi(usage_df) })
Asynchronous Shiny
future
and promises
packagessource: Joe Cheng / RStudio
Asynchronous Shiny
plan_info_tdsp <- reactive({ future({ db_get_plan_info(user_tdsp = user_tdsp) # connects to DB }) }) valid_res <- reactive({ plan_info_tdsp() %...>% { plan_df <- . plan_df <- dplyr::filter(plan_df, status %in% "valid") calc_cost_multi(plan_df, usage_df) } })
API + Asynchronous Shiny
plumber
to serve json objecthttr
to get json objectAPI + Asynchronous Shiny: shiny server code
plan_info_tdsp <- reactive({ future({ pyapi_plan_info(user_tdsp = user_tdsp) # call API (http GET) }) }) valid_res <- reactive({ plan_info_tdsp() %...>% { plan_df <- . plan_df <- dplyr::filter(plan_df, status %in% "valid") calc_cost_multi(plan_df, usage_df) } })
API + Asynchronous Shiny: calling API code
pyapi_plan_info <- function(user_tdsp) { rsp <- httr::GET( url = cfg$pyapi$endpoint, port = cfg$pyapi$port, query = list(tdsp = user_tdsp) ) rsp %>% httr::content(as = "text", encoding = "UTF-8") %>% jsonlite::fromJSON(simplifyDataFrame = TRUE) %>% tibble::as_tibble() }
API + Asynchronous Shiny: plumber.R
file
#* @apiTitle Power Y'all #* List of all plans for a TDSP #* @param tdsp TDSP short name #* @get /plan_info function(tdsp = character()) { db_get_plan_info(user_tdsp = tdsp) }
Limited Demo
Asynchronous Shiny
API + Asynchronous Shiny
What works great
jsonb
fieldsWhat needs improvement
loggr
package
config
package to read yaml
config file
jsonb
fieldsplumber
(including data-frames with list columns)Best practices: The twelve-factor app
What requires developer’s attention
plumber
(containers)
plumber
httr
, rvest
DBI
, RPostgres
, aws.s3
tidyverse
jsonlite
, plumber
, httr
shiny
, ggplot
, …future
, containers, RStudio Connectloggr
, config
, fs
, pool