Skip to content
Snippets Groups Projects

merge a few edits

Merged Ben Anderson requested to merge ba1e12/datacleaning:master into master
1 file
+ 16
6
Compare changes
  • Side-by-side
  • Inline
+ 16
6
# basic _drake.R style file
# but adapted for use in a project with multiple plans
# but adapted for use in a project where there might be multiple plans in the same folder
# called using r_make() from make_cleanFeeders.R
# see https://books.ropensci.org/drake/projects.html#usage for explanation
# Libraries/Packages ----
# the drake book suggests putting this in packages.R but...
# Libraries ----
library(dataCleaning) # remember to build it first :-)
dataCleaning::setup() # load env.R set up the default paths etc
makeLibs <- c("data.table", # data munching
"drake", # for plans
"here", # here
"lubridate", # dates and times
"hms", # times
@@ -17,7 +20,7 @@ makeLibs <- c("data.table", # data munching
dataCleaning::loadLibraries(makeLibs)
# Parameters ----
updateData <- "yep" # edit this in any way (at all) to get drake to re-load the data
updateData <- "rerun" # edit this in any way (at all) to get drake to re-load the data
updateReport <- "yes" # edit this in any way (at all) to get drake to re-load the data
# Some data to play with:
@@ -32,6 +35,7 @@ authors <- "Ben Anderson & Ellis Ridett"
# Functions ----
# for use in drake
# the drake book suggests putting this in functions.R but...
addSeason <- function(dt,dateVar,h){
dt <- dt[, tmpM := lubridate::month(get(dateVar))] # sets 1 (Jan) - 12 (Dec). May already exist but we can't rely on it
@@ -79,7 +83,10 @@ getData <- function(f,updateData){
makeUniq <- function(dt){
# we suspect there may be duplicates by feeder_ID, dateTime & kW
# remove them (report this in the .Rmd)
uniq <- unique(dt, by = c("rDateTime", "feeder_ID", "kW"))
uniq <- unique(dt, by = c("rDateTime", # dateTime
"feeder_ID", # our constructed unique feeded ID
"kW") # kW
)
return(uniq)
}
@@ -150,6 +157,8 @@ makeReport <- function(f,version, type = "html", updateReport){
# Set the drake plan ----
# the drake book suggests putting this in plan.R but...
# I had expected r_make() to load drake() in the new clean R session but it doesn't
my_plan <- drake::drake_plan(
origData = getData(dFile, updateData), # returns data as data.table. If you edit 'update' in any way it will reload - drake is watching you!
uniqData = makeUniq(origData), # remove duplicates
@@ -162,4 +171,5 @@ my_plan <- drake::drake_plan(
)
# see https://books.ropensci.org/drake/projects.html#usage
drake_config(my_plan, verbose = 2)
\ No newline at end of file
# I had expected r_make() to load drake() in the new clean R session but it doesn't
drake::drake_config(my_plan, verbose = 2)
\ No newline at end of file
Loading