-
-
Save Stephen-McDaniel/6d8d3e54696682a57d2e9f4d54679d9d to your computer and use it in GitHub Desktop.
From R shiny app dashboards in 2021: The Ultimate Guide for Busy People at https://yakdata.com/ultimate-guide-r-shiny-app-dashboards-2021/
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
# From R shiny app dashboards in 2021: The Ultimate Guide for Busy People at | |
# https://yakdata.com/ultimate-guide-r-shiny-app-dashboards-2021/ | |
# | |
# Example for chapter 4 | |
# Here is an illustration of data access and caching with feather | |
# | |
# Stephen McDaniel at YakData | |
# Released under the MIT License, 2021 | |
library(feather) | |
library(readr) | |
library(dplyr) | |
file_path <- "/Development/R-code-examples/Data_Analysis/" | |
setwd(file_path) | |
feather_file <- "sales_analyze3.feather" | |
# check the age of our data file for this dashboard | |
file_age_hours <- function(filepath) { | |
if(!exists(filepath)) { | |
# if it doesn't exist, infinity | |
return(Inf) | |
} else { | |
file_modified <- file.info(feather_file)$mtime | |
file_age_hours <- floor((as.numeric(Sys.time()) - as.numeric(file_modified))/(60*60)*10)/10 | |
return(file_age_hours) | |
} | |
} | |
if (file_age_hours(feather_file) > 24) { | |
# older than 24 hours, recreate cached data | |
sales_analyze <- as_tibble(readRDS("sales_analyze_geo_elegant_tray2.RDS")) %>% | |
select( | |
Order_Date, | |
Customer_Segment, | |
State, | |
Category, | |
Subcategory, | |
Item_Total, | |
State_Name, | |
lat_state, | |
long_state | |
) %>% | |
mutate(State_Abbrev = State, | |
State = State_Name, | |
metric = Item_Total) %>% | |
select(-State_Name) %>% | |
mutate(Order_Date = as.Date(Order_Date)) %>% | |
mutate(Region = ifelse( | |
State_Abbrev %in% c( | |
"NJ", "MA", "PA", "MD", "DE", "CT", "NY", "NH", "ME", "VT", "DC", "RI" | |
), | |
"Northeast", | |
ifelse( | |
State_Abbrev %in% c("FL", "AL", "TN", "MS", "SC", "GA", "VA", "NC", "WV", "KY", "LA"), | |
"South", | |
ifelse( | |
State_Abbrev %in% c("OH", "IA", "WI", "IL", "IN", "MI", "MN", "NE"), | |
"Midwest", | |
ifelse( | |
State_Abbrev %in% c("MO", "KS", "OK", "TX", "AR"), | |
"South Central", | |
ifelse( | |
State_Abbrev %in% c("WY", "ID", "AZ", "NM", "NV", "CO", "UT", "ND", "SD", "MT"), | |
"Mountains", | |
ifelse( | |
State_Abbrev %in% c("CA", "AK", "WA", "HI", "OR"), | |
"Pacific", | |
"Other" | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
# larger data frames can benefit from the feather format | |
save_feather(sales_analyze, "sales_analyze3.feather") | |
chart_data <- sales_analyze %>% | |
group_by(Category, Subcategory, Customer_Segment, Region) %>% | |
summarise(metric = sum(metric)) %>% | |
ungroup() | |
# smaller data frames have minimal benefit from feather format | |
save_rds(chart_data, "chart_data.RDS") | |
} else { | |
sales_analyze <- read_feather("sales_analyze3.feather") | |
chart_data <- read_rds(chart_data, "chart_data.RDS") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment