R/create_polish_app.R

Defines functions create_polish_app

Documented in create_polish_app

#' Create a ready-to-use Polish App skeleton
#'
#' Creates needed files and directories so that you can quickly begin development of your Shiny App.
#'
#' @param app.dirname New directory that will be created and contain the shiny app files
#' @param example.modules If `TRUE`, two example modules will be created along with the app skeleton
#' @param helpful.comments If `TRUE`, comments will be included to help the user learn how Polish Apps work
#'
#' @export
#' @examples
#' # Create a new polish app skeleton with example modules and helpful comments in the app.R file.
#' \dontrun{
#' polish::create_polish_app(
#'   app.dirname = file.path('.', 'my_new_shiny_app'),
#'   example.modules = TRUE,
#'   helpful.comments = TRUE
#' )
#' }
create_polish_app <- function(app.dirname, example.modules = FALSE, helpful.comments = FALSE) {

  dir <- system.file("boilerplate", package = "polish", mustWork = TRUE)

  file_paths <- list.files(path = dir, full.names = T, recursive = T, include.dirs = T)
  file_names <- list.files(path = dir, full.names = F, recursive = T, include.dirs = T)


  if(!dir.exists(app.dirname)) {
    dir.create(app.dirname)
  }

  for(i in 1:length(file_paths)) {

    if(!example.modules & stringr::str_detect(file_paths[i], 'modules/')) next

    if(dir.exists(file_paths[i]) & !dir.exists(file.path(app.dirname, file_names[i]))) {
      dir.create(file.path(app.dirname, file_names[i]))
    }

    if(!dir.exists(file_paths[i])) {

      file.copy(
        from = file_paths[i],
        to = file.path(app.dirname, file_names[i])
      )

      file_content <- readr::read_lines(file = file.path(app.dirname, file_names[i]))

      if(helpful.comments) {
        file_content <- stringr::str_replace(string = file_content, pattern = 'HELP: ', replacement = '')
        file_content <- file_content[!stringr::str_detect(string = file_content, pattern = 'BASE:')]
      } else {
        file_content <- stringr::str_replace(string = file_content, pattern = 'BASE: ', replacement = '')
        file_content <- file_content[!stringr::str_detect(string = file_content, pattern = 'HELP:')]
      }

      readr::write_lines(
        x = file_content,
        path = file.path(app.dirname, file_names[i])
      )

    }

  }

  invisible(app.dirname)

}
ndrewGele/polish documentation built on March 9, 2021, 6:34 p.m.