Guest User

Untitled

a guest
Jun 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. # OVERVIEW ====================================================================
  2. # AUTHOR: Brad West
  3. # CREATED ON: 2018-06-19
  4. # ---
  5. # DESCRIPTION: Script for defining a function for generating a default header.
  6. # The motivation is to create a consistent header that can be used to
  7. # scripts within a project or package. Easy access to the script's purpose,
  8. # author, and its date of creation. Also can be used for creating a skeleton
  9. # template.
  10. # ---
  11.  
  12. # FUNCTIONS ===================================================================
  13.  
  14. #' R Script Header
  15. #'
  16. #' Generate a formatted header for R scripts with a description, author, and
  17. #' date fields. Can specify the appropriate line width and pass additional
  18. #' fields which will server to break the script into sections. Motivation is
  19. #' to create a reproducible header for consistently documenting scripts within
  20. #' a project or package.
  21. #'
  22. #' @param description character vector length 1; A verbose description of the
  23. #' script
  24. #' @param auth character vector length 1; author
  25. #' @param date character vector length 1 or coercible to; the created date for
  26. #' the script, defaults to current date
  27. #' @param width numeric vector length 1; line width in characters
  28. #' @param sections character vector length n; additional sections to add to the
  29. #' script
  30. #'
  31. #' @return A vector of the lines of the header, invisibly
  32. #'
  33. #' @examples
  34. #' header("A very simple script", sections = c("IMPORT", "VARS" "FUNCTIONS", "ANALYSIS"))
  35. header <- function(description = "",
  36. auth = "Brad West",
  37. date = Sys.Date(),
  38. width = 80,
  39. sections = NULL) {
  40.  
  41. # split description on spaces
  42. s <- function(x, n) {
  43.  
  44. sst <- strsplit(x, '(?=\\s)', perl = T)[[1]]
  45. chars <- nchar(sst)
  46. rows <- c()
  47. row <- c()
  48. total <- 0
  49.  
  50. for (i in 1:length(chars)) {
  51. if (total + chars[i] < n) {
  52. row <- paste0(row, sst[i])
  53. total <- total + chars[i]
  54. } else {
  55. rows <- c(rows, row)
  56. total <- 0
  57. row <- c()
  58. }
  59. }
  60.  
  61. rows <- c(rows, row)
  62.  
  63. trimws(rows)
  64.  
  65. }
  66.  
  67. overview <- "# OVERVIEW "
  68. spaces <- stringr::str_locate_all(description, "\\s")[[1]][, 1]
  69. spaces_idx <- which(spaces < (width - nchar("# DESCRIPTION: ") + 1))
  70. last_space <- max(spaces[spaces_idx])
  71.  
  72. descrip <- c(paste0("# DESCRIPTION: ", substring(description, 1, last_space)))
  73.  
  74. rest_of_string <- substring(description, last_space + 1)
  75. indent <- "# "
  76. remaining_width <- width - nchar(indent)
  77. string_split <- s(rest_of_string, remaining_width)
  78.  
  79. descrip <- c(descrip, paste0(indent, string_split))
  80.  
  81. more_sections <- vapply(sections, function(x) {
  82. c(paste0("# ", x, " ",
  83. paste0(rep("=", width - nchar(x) - 4), collapse = "")),
  84. "", "")
  85. }, FUN.VALUE = character(3), USE.NAMES = F)
  86.  
  87. # build header
  88. out <- c(
  89. paste0(overview, paste0(rep("=", width - 1 - nchar(overview)), collapse = "")),
  90. paste0("# AUTHOR: ", auth),
  91. paste0("# CREATED ON: ", date),
  92. "# ---",
  93. descrip,
  94. "# ---",
  95. "",
  96. more_sections
  97. )
  98.  
  99. cat(out, sep = "\n")
  100.  
  101. invisible(out)
  102.  
  103. }
Add Comment
Please, Sign In to add comment