Skip to content

Commit

Permalink
first go at it
Browse files Browse the repository at this point in the history
  • Loading branch information
npjc committed Aug 28, 2015
1 parent 5d1fe09 commit e4c12c6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
18 changes: 0 additions & 18 deletions R/hello.R

This file was deleted.

48 changes: 48 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
@@ -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 = "_")
}
11 changes: 11 additions & 0 deletions R/nd.R
Original file line number Diff line number Diff line change
@@ -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 = "/"))
}

0 comments on commit e4c12c6

Please sign in to comment.