R/init_survey_analysis_project.R

Defines functions init_survey_analysis_project

Documented in init_survey_analysis_project

#' Auxiliary function to create a new data analysis project
#'
#' @param path The path in which to create the project
#' @param project_name The project name, used in the readme.md file
#' @param project_description A short description, used in the readme.md file
#' @examples
#' init_survey_analysis_project()
#'
#' @export
init_survey_analysis_project <- function(path,
                                         project_name = "",
                                         project_description = "") {


  dir.create(path, recursive = TRUE, showWarnings = FALSE)

  # generate the readme.md
  readme_md_contents <- c(
    paste0("# ", project_name),
    "",
    "## Background",
    "",
    project_description,
    "",
    "This repository has the following directory structure:",
    "",
    "```",
    project_name,
    "|   README.md",
    paste0("|   ", project_name, ".RProj"),
    "|",
    "|--- client_communications",
    "|--- data",
    "|--- dictionaries",
    "|--- exports",
    "|--- header_templates",
    "|--- questionnaire",
    "|--- R",
    "|--- templates",
    "```",
    "",
    "",
    "The `R` folder contains R scripts used in the analysis of the data.",
    "The `client_communications` folder contains communications and requirements sent by the client.",
    "`dictionaries` contains dictionaries for the close-ended questions in either xlsx or csv format.",
    "The `exports` folder is created for convinience. It should contain various exports such as the pptx presentation or excel files for the client.",
    "`header_templates` contains header renaming of variable names (either as excel or csv files)",
    "`questionnaire` contains a pdf/work version of the questionnaire",
    "`templates` contains pptx or any other templates used in the report generation process",
    "",
    "## Briefing Checklist",
    "",
    "   * Context, goals, introduction. Reflect this in background paragraph.",
    "   * Is this a continuing project? (where is the previous data, would we require comparisons to previous data?",
    "   * Data collection method",
    "   * Review the questionnaire: any branching? validation questions? how to treat partial questionnaires?",
    "   * Save the questionnaire in a pdf/word version in subdirectory `questionnaire`",
    "   * Schedule/commitments",
    "   * Sync on analysis methods which should be employed in this project, how to join datasets, desired outcomes/deliveries, how to handle complex operations.",
    "   * Infosec: discuss sensitivities, risk analysis, who can be/should be exposed to data, any special security requirements (i.e. encryption above the standards).",
    "   * Update relevant pulse in [monday](sarid.monday.com).",
    "",
    "## Terminology",
    "",
    "<Add terminlogy related text, if there is any>",
    "",
    "## Workflow",
    "",
    "The main R script is located in `R/main.r`. Run it to get everything going.",
    "Another important file is the `R/pre_process.r` which is used to pre-process the data.",
    ""
  )

  # create the readme.md file
  writeLines(readme_md_contents, con = file.path(path, "README.md"))

  # create all additional folders
  dir.create(file.path(path, "client_communications"))
  dir.create(file.path(path, "data"))
  dir.create(file.path(path, "dictionaries"))
  dir.create(file.path(path, "exports"))
  dir.create(file.path(path, "header_templates"))
  dir.create(file.path(path, "questionnaire"))
  dir.create(file.path(path, "R"))
  dir.create(file.path(path, "templates"))

  # create the main file and the pre_process file
  writeLines(c("# The file runs the analysis project.",
               "library(tidyverse)",
               'source("R/pre_process.R", encoding = "utf-8")',
               '',
               '# Example for officer code generation (presentation) ----',
               '',
               'library(officer)',
               '',
               '# > Some prep work ----',
               '',
               'main_content_location <- ph_location(left = 0.7/cm(1),',
               '                                     top = 4.52/cm(1),',
               '                                     height = 12.17/cm(1),',
               '                                     width = 32.61/cm(1))',
               '',
               '# > Actual build of presentation ----',
               '',
               'presentation_raw <- read_pptx("templates/presentation_template.pptx") %>%',
               '  add_slide(master = "master_face",',
               '            layout = "face") %>%',
               '  ph_with(location = ph_location_label("title"),',
               '          value = "Survey results") %>%',
               '  ph_with(location = ph_location_label("sub_title"),',
               '          value = as.character(Sys.Date()))',
               '',
               'presentation <- presentation_raw %>%',
               '  add_slide(layout = "Title only", master = "master_content") %>%',
               '  ph_with(value = "mtcars", location = ph_location_label("title")) %>%',
               '  ph_with(value = ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point(),',
               '          location = main_content_location)',
               '',
               'print(presentation, target = "exports/draft_presentation.pptx")'
               ),
             con = file.path(path, "R/main.R"))

  file.create(paste0(path, "/R/pre_process.R"))

  writeLines("", con = file.path(path, "R/pre_process.R"))

  # create the .gitignore file

  gitignore_contents <- c(
    "# History files",
    ".Rhistory",
    ".Rapp.history",
    "",
    "# Session Data files",
    ".RData",
    "",
    "# Example code in package build process",
    "*-Ex.R",
    "",
    "# Output files from R CMD build and check",
    "/*.tar.gz",
    "/*.Rcheck/",
    "",
    "# RStudio files",
    ".Rproj.user/",
    "",
    "# Produced vignettes",
    "vignettes/*.html",
    "vignettes/*.pdf",
    "",
    "# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3",
    ".httr-oauth",
    "",
    "# knitr and R markdown default cache directories",
    "/*_cache/",
    "/cache/",
    "",
    "# Temporary files created by R markdown",
    "*.utf8.md",
    "*.knit.md",
    "",
    "# Shiny token, see https://shiny.rstudio.com/articles/shinyapps.html",
    "rsconnect/"
  )

  writeLines(gitignore_contents, con = file.path(path, "/.gitignore"))

}
sarid-ins/saridr documentation built on Nov. 10, 2020, 9:07 p.m.