Skip to content

Commit

Permalink
project workflow improvements
Browse files Browse the repository at this point in the history
 - Use capsule for easier renv management
 - Create a separate sandbox targets project
 - Load multiple .env files
    - Use .env_user for setting USE_CAPSULE and TAR_PROJECT)
 - .gitignore stuff appropriately to retain outputs and keep meta files
 - Add all_targets() convenience function
  • Loading branch information
Noam Ross committed Aug 11, 2023
1 parent dea82d2 commit 0b7abee
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 24 deletions.
23 changes: 15 additions & 8 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
if (file.exists(".env")) {
try(readRenviron(".env"))
} else {
message("No .env file")
for (env_file in list.files(all.files = TRUE, pattern = "^\\.env.*")) {
try(readRenviron(env_file), silent = TRUE)
}


# Put the project library *outside* the project
#Sys.setenv(RENV_PATHS_LIBRARY_ROOT = file.path(normalizePath("~/.renv-project-libraries", mustWork = FALSE)))

if (file.exists("renv/activate.R")) {
source("renv/activate.R")
if(Sys.getenv("USE_CAPSULE") %in% c("1", "TRUE", "true")) {
if (interactive() && file.exists("renv.lock")) {
message("renv library not loaded (found env var USE_CAPSULE=", Sys.getenv("USE_CAPSULE"), "). Use `capsule` functions (see https://github.com/MilesMcBain/capsule)")
if(require(capsule, quietly = TRUE)) {
capsule::whinge()
} else {
message('Install {capsule} with install.packages("capsule", repos = c(mm = "https://milesmcbain.r-universe.dev", getOption("repos")))')
}
}
} else {
message("No renv/activate.R")
}
source("renv/activate.R")
}


# Use the local user's .Rprofile when interactive.
# Good for keeping local preferences, but not always reproducible.
Expand Down
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
.RData
.Ruserdata
.DS_Store
_targets/objects
_targets.yaml
.env_user
_targets/
_targets/*
!_targets/meta/meta
_targets_sandbox/
_targets_sandbox/*
!_targets_sandbox/meta/meta
nohup.out
outputs/
!outputs/.gitkeep
!outputs/example_report.html
data/archive
data/**/*.csv.gz
data/**/*.grib
Expand Down
29 changes: 29 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#' Collect all targets and lists of targets in the environment
all_targets <- function(env = parent.env(environment()), type = "tar_target") {
rfn <- function(obj) inherits(obj, type) || (is.list(obj) && all(vapply(obj, rfn, logical(1))))
objs <- ls(env)
out <- list()
for(o in objs) {
obj <- get(o, envir = env)
if (rfn(obj)) {
out[[length(out) + 1]] <- obj
}
}
return(out)
}

# TODO: convenience functions for reading targets from S3
# Need to figure out the best way for these to work with both regular and
# file-type targets, which should emerge from working with them in the sandbox
# tar_read_s3 <- function(target_name, bucket = Sys.getenv("AWS_BUCKET_ID"),
# prefix = "_targets") {
# ## if target_name does not exist, convert the symbol to character
# if (!exists(target_name)) {
# target_name <- as.character(substitute(target_name))
# }
#
# }
#
# tar_load_s3 <- function(target_name, ...) {
#
# }
18 changes: 7 additions & 11 deletions _targets.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Re-record current dependencies for CAPSULE users
if(Sys.getenv("USE_CAPSULE") %in% c("1", "TRUE", "true"))
capsule::capshot(c("packages.R",
list.files(pattern = "_targets.*\\.(r|R)$", full.names = TRUE),
list.files("R", pattern = "\\.(R|r)$", full.names = TRUE)))

# Load packages (in packages.R) and load project-specific functions in R folder
suppressPackageStartupMessages(source("packages.R"))
for (f in list.files(here::here("R"), full.names = TRUE)) source (f)
Expand Down Expand Up @@ -234,14 +240,4 @@ test_targets <- tar_plan(
)

# List targets -----------------------------------------------------------------

list(
static_targets,
dynamic_targets,
data_targets,
model_targets,
deploy_targets,
plot_targets,
report_targets,
test_targets
)
all_targets()
13 changes: 13 additions & 0 deletions _targets_sandbox.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Re-record current dependencies for CAPSULE users
if(Sys.getenv("USE_CAPSULE") %in% c("1", "TRUE", "true"))
capsule::capshot(c("packages.R",
list.files(pattern = "_targets.*\\.(r|R)$", full.names = TRUE),
list.files("R", pattern = "\\.(R|r)$", full.names = TRUE)))

# Load packages (in packages.R) and load project-specific functions in R folder
suppressPackageStartupMessages(source("packages.R"))
for (f in list.files(here::here("R"), full.names = TRUE)) source (f)



all_targets()
6 changes: 6 additions & 0 deletions targets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
main:
store: _targets
script: _targets.R
sandbox:
store: _targets_sandbox
script: _targets_sandbox.R

0 comments on commit 0b7abee

Please sign in to comment.