diff --git a/_drake_basicReport.R b/_drake_basicReport.R new file mode 100644 index 0000000000000000000000000000000000000000..29dac4eef218859f179da414af0ec1c74207e33b --- /dev/null +++ b/_drake_basicReport.R @@ -0,0 +1,77 @@ +# basic drake makefile + +# Libraries ---- +library(woRkflow) # remember to build it first :-) +woRkflow::setup() # load env.R set up the default paths etc + +reqLibs <- c("data.table", # data munching + "drake", # what's done stays done + "here", # here + "lubridate", # dates and times + "ggplot2", # plots + "skimr" # for skim +) +# load them +woRkflow::loadLibraries(reqLibs) + +# Parameters ---- + +# Some data to play with: +# https://data.nationalgrideso.com/carbon-intensity1/historic-generation-mix/r/historic_gb_generation_mix + +urlToGet <- "http://data.nationalgrideso.com/backend/dataset/88313ae5-94e4-4ddc-a790-593554d8c6b9/resource/7b41ea4d-cada-491e-8ad6-7b62f6a63193/download/df_fuel_ckan.csv" +update <- "please" # edit this in any way (at all) to get drake to re-load the data from the url +rmdFile <- "basicReport" # <- name of the .Rmd file to run at the end +title <- "UK Electricity Generation" +subtitle <- "UK ESO grid data" +authors <- "Ben Anderson" + +# Functions ---- +# for use in drake +getData <- function(f,update){ + # gets the data + message("Getting data from: ", f) + dt <- data.table::fread(f) + return(dt) +} + +makeGWPlot <- function(dt){ + message("Rebuilding plot") + # expects the eso data as a data.table + # draws a plot + dt[, rDateTime := lubridate::ymd_hms(DATETIME)] # hurrah, somebody read https://speakerdeck.com/jennybc/how-to-name-files?slide=21 + dt[, weekDay := lubridate::wday(rDateTime, label = TRUE)] + # draw a megaplot for illustrative purposes + p <- ggplot2::ggplot(dt, aes(x = rDateTime, + y = GENERATION/1000, + colour = weekDay)) + + geom_point() + + theme(legend.position = "bottom") + + labs(x = "Time", + y = "Generation (GW - mean per halfhour?)", + caption = "Source: UK Grid ESO (http://data.nationalgrideso.com)") + return(p) +} + +makeReport <- function(f){ + message("Re-running report: ", f) + # default = html + rmarkdown::render(input = paste0(here::here("Rmd", f),".Rmd"), # we love here:here() - it helps us find the .Rmd to use + params = list(title = title, + subtitle = subtitle, + authors = authors), + output_file = paste0(here::here("docs", f),".html") # where the output goes + ) +} + +# Set the drake plan ---- +# Clearly this will fail if you do not have internet access... +plan <- drake::drake_plan( + esoData = getData(urlToGet, update), # returns data as data.table. If you edit 'update' in any way it will reload - drake is watching you! + skimTable = skimr::skim(esoData), # create a data description table + gWPlot = makeGWPlot(esoData), # make a plot + out = makeReport(rmdFile) +) + +drake::drake_config(plan, verbose = 2) + diff --git a/make_basicReport.R b/make_basicReport.R new file mode 100644 index 0000000000000000000000000000000000000000..3527b4cd5f19962a62eccf3e6ffd8ddfb3169976 --- /dev/null +++ b/make_basicReport.R @@ -0,0 +1,20 @@ +# make file for drake +# see https://books.ropensci.org/drake/projects.html#usage + +# Set up ---- +startTime <- proc.time() + +# use r_make to run the plan inside a clean R session so nothing gets contaminated +drake::r_make(source = "_drake_basicReport.R") # where we keep the drake plan etc + +# we don't keep this in /R because that's where the package functions live +# we don't use "_drake.R" because we have lots of different plans + +# Finish off ---- + +t <- proc.time() - startTime # how long did it take? +elapsed <- t[[3]] + +print("Done") +print(paste0("Completed in ", round(elapsed/60,3), " minutes using ", + R.version.string, " running on ", R.version$platform)) \ No newline at end of file