make_basicReport.R 3.31 KB
Newer Older
1
2
3
4
# basic drake makefile

# Libraries ----
library(woRkflow) # remember to build it first :-)
B.Anderson's avatar
B.Anderson committed
5
6
woRkflow::setup() # load env.R set up the default paths etc

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 <- "now" # 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
  dt <- data.table::fread(f)
  return(dt)
}

makeGWPlot <- function(dt){
  # expects the eso data as a data.table
  # draws a plot
Ben Anderson's avatar
Ben Anderson committed
40
  dt[, rDateTime := lubridate::ymd_hms(DATETIME)] # hurrah, somebody read https://speakerdeck.com/jennybc/how-to-name-files?slide=21
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  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){
  # default = html
B.Anderson's avatar
B.Anderson committed
56
  rmarkdown::render(input = paste0(here::here("Rmd", f),".Rmd"), # we love here:here() - it helps us find the .Rmd to use
57
58
59
                    params = list(title = title,
                                  subtitle = subtitle,
                                  authors = authors),
B.Anderson's avatar
B.Anderson committed
60
                    output_file = paste0(here::here("docs", f),".html") # where the output goes
61
62
63
64
65
66
67
68
  )
}
# Set up ----
startTime <- proc.time()

# Set the drake plan ----
# Clearly this will fail if you do not have internet access...
plan <- drake::drake_plan(
Ben Anderson's avatar
Ben Anderson committed
69
  esoData = getData(urlToGet, update), # returns data as data.table. If you edit 'update' in any way it will reload - drake is watching you!
70
71
72
73
74
75
76
77
78
79
  skimTable = skimr::skim(esoData), # create a data description table
  gWPlot = makeGWPlot(esoData) # make a plot
)

# Run drake plan ----
plan # test the plan
make(plan) # run the plan, re-loading data if needed

# Run the report ----
# run the report - don't do this inside the drake plan as 
B.Anderson's avatar
B.Anderson committed
80
# drake can't seem to track the .rmd file if it is not explicitly named
E.Ridett's avatar
E.Ridett committed
81
makeReport(rmdFile)
82

Ben Anderson's avatar
Ben Anderson committed
83
84
85
86
# Just to show we can bring spirits back from the deep (i.e. from wherever drake hid them)
dt <- drake::readd(esoData)
message("Data covers ", min(dt$rDateTime), " to ", max(dt$rDateTime))

87
88
89
90
91
92
93
94
# 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))