A database repository may define post_build <- function(database) { ...; database } in R/post_build.R, for processing applied to the whole
database once its datasets are combined, such as adding a column or moving
a context into the traits table. Every pipeline build_setup_pipeline()
writes calls this after build_add_version() and before the database is
saved.
build_post_process(database, file = "R/post_build.R")Database object, as returned by build_add_version()
Path to the file defining post_build(). If the file does not
exist, or file is character(0) (as the targets pipeline passes when
there is no file), the database is returned unchanged and nothing is
recorded.
The database post_build() returned, with
build_info$post_build added; or database unchanged if there is no file
post_build() runs on every rebuild of the combined database, including a
targets or remake rebuild after one dataset changes, so it should be
quick. Outputs derived from the finished database that are wanted only when
publishing it, such as a flattened copy or parquet files, belong in
post_export() in R/post_export.R instead, which build_post_export()
runs when asked and no build runs.
What post_build() returns replaces the database; there is no second,
"processed" copy. It must therefore return the database, with every
component it was given, and anything else is an error. Side effects, such as
writing files, are allowed.
The file is sourced into a fresh environment whose parent is the search path
rather than the global environment. post_build() sees attached packages
(library(traits.build) attaches traits.build and its Depends), including
any the file attaches with library() outside post_build(), and whatever
the file itself defines, but not objects a pipeline happens to leave in the
global environment, which differ between build.R and a targets worker.
A package attached inside post_build() is not seen in a fresh session such
as a targets worker: attach it at the top of the file, or call it as
pkg::fun(). Other files in R/ are not sourced; source one from
R/post_build.R if post_build() needs it. Only R/post_build.R itself is
tracked, though: the targets and remake pipelines do not rebuild the
database when a file it sources changes, and the md5 recorded below does not
cover that file. Code that should trigger a rebuild belongs in
R/post_build.R.
When the step runs, database$build_info$post_build records the file and
the md5 checksum of its contents, so the code that last changed the database
can be identified even when it was not committed. Line endings are
normalised to \n before the checksum is taken, so a checkout with Windows
line endings records the same checksum as one on macOS or Linux; for a file
with \n endings it is the checksum tools::md5sum() gives the file.
if (FALSE) { # \dontrun{
# In R/post_build.R
post_build <- function(database) {
database$traits <- dplyr::mutate(database$traits, curve_id = NA_character_)
database
}
# In the pipeline, after `build_add_version()`
database <- build_post_process(database)
database$build_info$post_build
} # }