From e4c12c622cbb0bc1a4714a4c0002be9e0a90b40f Mon Sep 17 00:00:00 2001 From: Nicolas Coutin Date: Fri, 28 Aug 2015 09:34:48 -0700 Subject: [PATCH] first go at it --- R/hello.R | 18 ------------------ R/helpers.R | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ R/nd.R | 11 +++++++++++ 3 files changed, 59 insertions(+), 18 deletions(-) delete mode 100644 R/hello.R create mode 100644 R/helpers.R create mode 100644 R/nd.R diff --git a/R/hello.R b/R/hello.R deleted file mode 100644 index 26b1f90..0000000 --- a/R/hello.R +++ /dev/null @@ -1,18 +0,0 @@ -# Hello, world! -# -# This is an example function named 'hello' -# which prints 'Hello, world!'. -# -# You can learn more about package authoring with RStudio at: -# -# http://r-pkgs.had.co.nz/ -# -# Some useful keyboard shortcuts for package authoring: -# -# Build and Reload Package: 'Cmd + Shift + B' -# Check Package: 'Cmd + Shift + E' -# Test Package: 'Cmd + Shift + T' - -hello <- function() { - print("Hello, world!") -} diff --git a/R/helpers.R b/R/helpers.R new file mode 100644 index 0000000..b9ee135 --- /dev/null +++ b/R/helpers.R @@ -0,0 +1,48 @@ +# take files names that are arranged in data_categories and turn them into a +# tidy data frame. +# defaults to 4 variables: abbreviation or acronym / increment / slug / extension +# dames stands for datafied names +read_dames <- function(dames, var_sep = "_", name_vars = c("aoa", "inc","slug","ext")) { + pattern <- paste0("[", var_sep, "//.]") + split_ls <- data.table::tstrsplit(dames, pattern, type.convert = T) + named_ls <- setNames(split_ls, name_vars) + dplyr::as_data_frame(named_ls) +} + +# now need function to get next increment for folder or file +infer_next_inc <- function(dames_df, what = c("folders", "files"), inc_by = 1) { + what <- match.arg(what) + expr <- switch(what, + folders = ~is.na(ext), + files = ~!is.na(ext)) + inc_df <- dplyr::filter_(dames_df, expr) + if(length(inc_df$inc) == 0) # to deal with case where this is the first one + return(inc_by) + # summarise_(.dots = list(max = ~max(incr))) + max(inc_df$inc) + inc_by +} +# function to left pad inc if need be and convert to char +left_pad <- function(inc) { + left_pad <- NULL + if(nchar(inc) == 1) left_pad <- 0 + paste0(left_pad, inc, collapse="") +} + +# assembles new dame based on provided args +newdame <- function(slug = "my-next-file", aoa = "AOA", path = ".", ptrn = NULL) { + files_and_dirs <- list.files(path, ptrn) + if(length(files_and_dirs) == 0) { + writeLines(c("---", " output: ", " html_document:", " keep_md: TRUE", + "variant: markdown_github", "---", "", "```{r common-setup, echo=FALSE}", + "knitr::opts_chunk$set(", " collapse = TRUE,", " comment = \"#>\"", + ")", "pacman::p_load(readr,readrbio, dplyr, tidyr, stringi, ggplot2)", + "```", ""),"REF_00_template.Rmd") + files_and_dirs <- list.files(path, ptrn) + } + dames_df <- read_dames(files_and_dirs) + dames_df <- dplyr::filter_(dames_df, ~aoa == aoa) + inc <- infer_next_inc(dames_df) + inc <- left_pad(inc) + slug <- reprex:::construct_safeslug(slug) + paste(aoa, inc, slug, sep = "_") +} diff --git a/R/nd.R b/R/nd.R new file mode 100644 index 0000000..b819844 --- /dev/null +++ b/R/nd.R @@ -0,0 +1,11 @@ +# main workhorse +nd <- function(slug = "my_next_file", aoa = "AOA", path = ".", ptrn = NULL, template_path = "../REF_00_template.Rmd") { + dame <- newdame(slug, aoa, path, ptrn) + new_dir <- paste0(path, '/', dame) + dir.create(new_dir) + rmd_file <- reprex:::add_ext(dame,"Rmd") + rmd_lines <- c("---", "output: ", " html_document:", " keep_md: TRUE", + " variant: markdown_github", "---", "", + paste0("```{r common-setup, child=\"", template_path, "\"}"), "```", "") + writeLines(rmd_lines, paste(new_dir, rmd_file, sep = "/")) +}