#BEGIN: description
#' Reformat file generated by use_r.function to CRAN valid structure
#'
#'
#'
#' @usage
#' reformat_packaging(
#' file_path,
#' dry_run = TRUE
#' )
#'
#' @param file_path \[character\], length 1, file path to reformat
#' @param dry_run \[logical\], length 1, if TRUE, print the reformatted file
#'
#' @return
#'
#' @export
#' @importFrom magrittr %>%
#END: description
#BEGIN: examples
#' @examples
#' #BEGIN: example
#' \dontrun{
#' # observe that it moves examples next to description
#' use_r.function('myfun.R')
#' reformat_package('myfun.R')
#' }
#' #END: example
#END: examples
#BEGIN: code
reformat_packaging = function(
file_path,
dry_run = TRUE
) {
#BEGIN: setup params
#END: setup params
#BEGIN: param checks
if (!(length(file_path) == 1))
rlang::abort(message = 'param file_path must satisfy length(v) == 1')
if (!(class(file_path) == 'character'))
rlang::abort(message = "param file_path must satisfy class(v) == 'character'")
if (!(length(dry_run) == 1))
rlang::abort(message = 'param dry_run must satisfy length(v) == 1')
if (!(class(dry_run) == 'logical'))
rlang::abort(message = "param dry_run must satisfy class(v) == 'logical'")
#END: param checks
#BEGIN: computation
file_path %>% readr::read_lines() -> lines
i_begin_description = which(lines == "#BEGIN: description")
i_end_description = which(lines == "#END: description")
i_begin_examples = which(lines == "#BEGIN: examples")
i_end_examples = which(lines == "#END: examples")
i_begin_code = which(lines == "#BEGIN: code")
i_end_code = which(lines == "#END: code")
if (!all(c(length(i_begin_description),length(i_end_description)) == 1))
rlang::abort(message = "'#BEGIN: description' and '#END: description' must occur exactly 1 time in file")
if (!all(c(length(i_begin_examples),length(i_end_examples)) == 1))
rlang::abort(message = "'#BEGIN: examples' and '#END: examples' must occur exactly 1 time in file")
if (!all(c(length(i_begin_code),length(i_end_code)) == 1))
rlang::abort(message = "'#BEGIN: code' and '#END: code' must occur exactly 1 time in file")
lines[
c(
# description goes first
(i_begin_description):(i_end_description),
# then examples
(i_begin_examples):(i_end_examples),
# then code
(i_begin_code):(i_end_code)
)
] -> reformated_lines
if (dry_run) {
reformated_lines %>% stringi::stri_join(collapse = '\n') %>% cat()
} else {
reformated_lines %>% readr::write_lines(file = file_path)
}
#END: computation
#BEGIN: return
#END: return
}
#END: code
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.