{.tabset .tabset-pills}

library(data.tree)
library(dplyr)
library(kableExtra)
library(plyr)
library(purrr)
library(tibble)

sys_info <- Sys.info()
computer_name <- sys_info[["nodename"]]

src_dir <- here::here("models")
dest_dir <- ifelse(computer_name == "hake-precision",
                   "/srv/hake/models",
                   src_dir)
if(!dir.exists(dest_dir)){
  stop("Directory `", dest_dir,"` does not exist. Bailing out")
}
if(!dir.exists(src_dir)){
  stop("Directory `", src_dir,"` does not exist. Bailing out")
}
if(src_dir != dest_dir){
  # Check that the directories in the documentation source are all found in
  # the destination directory recursively
  drs_dest_dir <- list.dirs(dest_dir,
                            recursive = TRUE,
                            full.names = FALSE)
  drs_src_dir <- list.dirs(src_dir,
                           recursive = TRUE,
                           full.names = FALSE)
  if(!length(drs_dest_dir)){
    stop("Directory `", dest_dir, "` does not contain any directories")
  }
  if(!length(drs_src_dir)){
    stop("Directory `", src_dir, "` does not contain any directories")
  }
  in_dest_dir <- drs_src_dir %in% drs_dest_dir
  if(!all(in_dest_dir)){
    stop("Not all the model documentation directories are in the ",
         "destination directory. Missing directories are:\n",
         paste(drs_src_dir[!in_dest_dir], collapse = "\n"))
  }
}

drs_dest_dir <- list.dirs(dest_dir, recursive = FALSE)
drs_dest_dir_nice <- gsub("\\.\\/", "", drs_dest_dir)
drs_src_dir <- list.dirs(src_dir, recursive = FALSE)
drs_src_dir_nice <- gsub("\\.\\/", "", drs_src_dir)

walk2(drs_dest_dir, drs_dest_dir_nice, ~{
  x <- file.path(.x, "this-year.html")
  cat(paste0("[Click to enter **", .y, "** directory]("), x, ")\n\n")
})
split_at <- function(x, pos){
  # Split vector up at indices in vector `pos`
  # Example:
  # a <- c(1, 2, 2, 3)
  # split_at(a, c(2, 4))
  # [[1]]
  # [1] 1
  # 
  # [[2]]
  # [1] 2 2
  # 
  # [[3]]
  # [1] 3
  unname(split(x, cumsum(seq_along(x) %in% pos))) 
}
path <- list.dirs(dest_dir,
                  recursive = TRUE,
                  full.names = FALSE)
path <- path[path != ""]
# Extract unique root dirs
root_inds <- grep("^[-a-zA-Z0-9_]+$", path)
if(!length(root_inds)){
  vecs <- NULL
}else if(length(root_inds) == 1){
  # There's only one subdirectory
  vecs <- list(path)
}else{
  vecs <- split_at(path, root_inds)
}

# Create a list element for each root dir
lst <- map(vecs, ~{
  df <- map(strsplit(.x, "/"), ~{
    as_tibble(t(.x)) |> 
      rbind.fill()
  }) |> 
    map_df(~{.x})
  df$pathString <-
    apply(df,
          1,
          function(x){
            paste(trimws(na.omit(x)),
                  collapse = "/") 
          })

  as.Node(df)
}) |> 
  setNames(path[root_inds])

# Print them out
map(lst, ~{
  .x  
})
walk2(drs_src_dir, drs_dest_dir, ~{
  suppressWarnings(rmarkdown::render(file.path(.x, "this-year.Rmd")))
  if(src_dir != dest_dir){
    copied <- file.copy(file.path(.x, "this-year.html"), 
                        file.path(.y, "this-year.html"),
                        overwrite = TRUE)
      if(copied){
        message("File `", .x, "` copied to `", .y, "`")
      }else{
        stop("File `", .x, "` was not copied to `", .y, "`")
      }
  }
})


pacific-hake/hake-assessment documentation built on Nov. 8, 2024, 1:16 p.m.