knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "",
  fig.path = "README-"
)

library("data.tree")

# Adapted from https://stackoverflow.com/questions/14188197/representing-a-directory-tree-as-a-recursive-list
tree_list <- function(x) {
  if(grepl("/packrat$", x)) return(list(" ..." = list()))
  if(grepl("/\\.git$", x)) return(list(" ..." = list()))

  isdir <- file.info(x)$isdir

  if(!isdir) {
    out <- list()
  } else {
    files <- list.files(
      x
      , full.names   = TRUE
      , include.dirs = FALSE
      , all.files = TRUE
    )

    dirs <- list.dirs(
      x
      , full.names   = TRUE
      , recursive = FALSE
    )
    files <- files[!files %in% dirs]

    files <- c(
      dirs[!grepl("^\\.", basename(dirs))]
      , dirs[grepl("^\\.", basename(dirs))]
      , files[!grepl("^\\.", basename(files))]
      , files[grepl("^\\.", basename(files))]
    )

    files <- files[!grepl("\\.$", files)]
    if(length(files) > 0) {
      out <- lapply(files, tree_list)
      isdir <- sapply(files, function(x) file.info(x)$isdir)
      names(out) <- basename(files)
      names(out)[isdir] <- paste0(names(out)[isdir], "/")
    } else {
      out <- list(NULL)
      names(out) <- paste0(basename(files), "/")
    }
  }
  out
}

template

template contains functions to initialize a new project according to my needs.

Installation

You can install template from github with:

# install.packages("remotes")
remotes::install_github("crsh/template")

Example

To set up a new project repository using our standard project structure see init_project():

library("template")

# Set up project structure in temporary directory
tmp_dir <- tempfile("template-")

init_project(
  x = "myproject"
  , path = tmp_dir
)

project_path <- file.path(tmp_dir, "myproject")

# Start another study
add_study(x = "myproject", path = project_path)

# Start new manuscript
add_paper(
  x = project_path
  , shorttitle = "myproject-paper"
)

The resulting directory structure is looks like this:

project_structure <- tree_list(tmp_dir)
project_structure <- as.Node(project_structure)
print(project_structure$children$`myproject/`)


crsh/template documentation built on Dec. 5, 2023, 6:31 p.m.