Skip to content

Commit

Permalink
Add dev scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardlavender committed Oct 23, 2024
1 parent 8b9861e commit 4a15043
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 0 deletions.
132 changes: 132 additions & 0 deletions dev/01-dev.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
###########################
###########################
#### 01-dev.R

#### Aims
# 1) Record project development

#### Prerequisites
# 1) NA


###########################
###########################
#### Project set up

#### Use git
# usethis::use_git()
# usethis::use_github()
usethis::git_vaccinate()

#### Use dependency management
renv::init()

#### Install package(s)
renv::install("edwardlavender/dv", prompt = FALSE)
# commonmark/(r)markdown packages (for README documentation)
if (!requireNamespace("commonmark", quietly = TRUE))
renv::install("commonmark", prompt = FALSE)
if (!requireNamespace("markdown", quietly = TRUE))
renv::install("markdown", prompt = FALSE)
if (!requireNamespace("rmarkdown", quietly = TRUE))
renv::install("rmarkdown", prompt = FALSE)
if (!requireNamespace("yaml", quietly = TRUE))
renv::install("yaml", prompt = FALSE)

#### Use dv templates

# Set up template project structure
dv::use_template_proj()

# Update .gitignore
dv::use_template_gitignore()

# Add a README and associated files
usethis::use_code_of_conduct("[email protected]")
dv::use_template_readme(title = "Estimating residency in acoustic telemetry systems",
author = "Edward Lavender",
email = "[email protected]")

# Add template scripts
if (!requireNamespace("pacman", quietly = TRUE))
renv::install("pacman", prompt = FALSE)
dv::use_template_script(here_r("insert_script_name_1.R"))
dv::use_template_script(here_r("insert_script_name_2.R"))


###########################
###########################
#### Project maintenance

#### Enforce consistent syntax
# usethis::use_tidy_style()
# Check code is syntactically valid
lapply(list.files(dv::here_r(), full.names = TRUE, pattern = ".R"), parse)

#### Check project spelling
spelling::spell_check_files("README.Rmd", lang = "en-GB")
spelling::spell_check_files(
list.files(dv::here_r(), full.names = TRUE, pattern = ".R"),
lang = "en-GB")

#### List project dependencies
# Define helper functions
pkg_version <- function(pkg) {
packageDescription(pkg)$Version
}
pkg_github_user <- function(pkg) {
usr <- packageDescription(pkg)$GithubUsername
ifelse(is.null(usr), "", paste0(usr, "/"))
}
pkg_github_commit <- function(pkg) {
com <- packageDescription(pkg)$RemoteSha
ifelse(is.null(com), "", com)
}
# Get dependencies
pkg <- renv::dependencies()
unique(pkg$Require); unique(pkg$Version); unique(pkg$Dev)
pkg <- data.frame(package = sort(unique(pkg$Package)),
version = NA,
github_user = NA,
github_commit = NA)
# Get versions and github source (if necessary)
for (i in seq_len(nrow(pkg))) {
pkg$version[i] <- pkg_version(pkg$package[i])
pkg$github_user[i] <- pkg_github_user(pkg$package[i])
pkg$github_commit[i] <- pkg_github_commit(pkg$package[i])
}
pkg$version[pkg$github_user != ""] <- pkg$github_commit[pkg$github_user != ""]
# Define install code
pkg$install <- paste0("renv::install('",
pkg$github_user,
pkg$package,
"@", pkg$version,
"', prompt = FALSE)")
pkg <- pkg[, c("package", "install")]
# Save dataframe
# View(pkg)
saveRDS(pkg, dv::here_data("inst", "dependencies.rds"))

#### Update renv
## Take snapshot
renv::snapshot()
## Clean snapshot
# Note that this may attempt to drop 'suggested packages'
# ... that are required by (some) functions from other packages
# ... but which are not used directly. To guard against this,
# ... make an arbitrary call to the required packages
# ... where they are needed.
renv::clean()

#### Save sessionInfo
saveRDS(sessionInfo(), dv::here_data("inst", "session-info.rds"))

#### Save the project directory 'tree'
# ... This enables the project directory tree to be rebuilt on another machine
# ... This function should be re-run when the directory tree is updated
dv::use_template_tree(save = dv::here_data("inst", "tree.rds"))


#### End of code.
###########################
###########################
85 changes: 85 additions & 0 deletions dev/02-clone.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
###########################
###########################
#### 02-clone.R

#### Aims
# 1) Clone a project.
# This script is designed to be used to set up an established project, shared
# via GitHub, on another machine. This includes:
# ... A) Creation of directory structure
# ... B) Installation of dependencies

#### Prerequisites
# 1) NA


###########################
###########################
#### Restore project dependencies

#### Option (1): Automatic restoration via renv
# Restore the project's dependencies (including dv) from the lockfile
restore <- tryCatch(renv::restore(), error = function(e) e)

#### Option (2): Manual re-installation of packages
if (inherits(restore, "error")) {

# Read dependency list
pkg <- readRDS(file.path("data", "inst", "dependencies.rds"))
# Install {renv}
if (!requireNamespace("renv", quietly = TRUE)) {
install.packages("renv")
}
# Manually re-install packages using {renv}
t1 <- Sys.time()
inst_log <-
lapply(split(pkg, seq_len(nrow(pkg))), function(d) {
success <- tryCatch(eval(parse(text = d$install)),
error = function(e) e)

if (!inherits(success, "error")) {
msg <- ""
success <- TRUE
} else {
msg <- as.character(success)
success <- FALSE
}
d$success <- success
d$msg <- msg
d
})
# Check success
t2 <- Sys.time()
difftime(t2, t1)
inst_log <- do.call(rbind, inst_log)
inst_log

# Manually handle any failures e.g., via renv::hydrate().
# ... OK.

}


###########################
###########################
#### Rebuild project structure

#### Recreate high-level project structure
dv::use_template_proj(overwrite = FALSE)

#### Rebuild directory tree
tree <- here::here("data", "dv", "tree.rds")
if (file.exists(tree)) {
tree <- readRDS(tree)
dv::use_template_tree(tree = tree, recreate = TRUE)
}

#### Re-run project workflow
# Re-obtain raw dataset(s)
# Re-run data processing
# Re-run analysis


#### End of code.
###########################
###########################

0 comments on commit 4a15043

Please sign in to comment.