├── .gitignore ├── .Rbuildignore ├── LICENSE ├── tools ├── overview_folder_structure.png └── project_wizard_with_template.png ├── NAMESPACE ├── analysistemplates.Rproj ├── inst └── rstudio │ └── templates │ └── project │ └── standard_analysis.dcf ├── DESCRIPTION ├── man └── standard_analysis.Rd ├── LICENSE.md ├── README.md └── R └── standard_analysis.R /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^analysistemplates\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^LICENSE\.md$ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2023 2 | COPYRIGHT HOLDER: Max Planck Institute of Psychiatry 3 | -------------------------------------------------------------------------------- /tools/overview_folder_structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonas-hag/analysistemplates/HEAD/tools/overview_folder_structure.png -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(standard_analysis) 4 | importFrom(utils,installed.packages) 5 | -------------------------------------------------------------------------------- /tools/project_wizard_with_template.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonas-hag/analysistemplates/HEAD/tools/project_wizard_with_template.png -------------------------------------------------------------------------------- /analysistemplates.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | BuildType: Package 16 | PackageUseDevtools: Yes 17 | PackageInstallArgs: --no-multiarch --with-keep.source 18 | -------------------------------------------------------------------------------- /inst/rstudio/templates/project/standard_analysis.dcf: -------------------------------------------------------------------------------- 1 | Binding: standard_analysis 2 | Title: Standard analysis template 3 | Subtitle: A template for a standard data analysis geared towards scientific users. 4 | OpenFiles: README.md 5 | 6 | Parameter: include_analysis_for_publication 7 | Widget: CheckboxInput 8 | Label: Include Analysis for publication folder 9 | Default: Off 10 | Position: left 11 | 12 | Parameter: use_renv 13 | Widget: CheckboxInput 14 | Label: Use renv with this project 15 | Default: On 16 | Position: left 17 | 18 | Parameter: include_gitignore 19 | Widget: CheckboxInput 20 | Label: Create a .gitignore file 21 | Default: On 22 | Position: left 23 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: analysistemplates 2 | Title: Creates a standard folder structure for scientific data analysis in RStudio 3 | Version: 0.1.0 4 | Authors@R: 5 | c(person(given = "Jonas", 6 | family = "Hagenberg", 7 | role = c("aut", "cre"), 8 | email = "jonas_hagenberg@psych.mpg.de", 9 | comment = c(ORCID = "0000-0002-1849-1106")), 10 | person(given = "Max Planck Institute of Psychiatry", 11 | role = "cph")) 12 | Description: This package provides an RStudio template that starts a new RStudio 13 | project with a standard folder structure for scientific data analysis. 14 | License: MIT + file LICENSE 15 | Encoding: UTF-8 16 | Roxygen: list(markdown = TRUE) 17 | RoxygenNote: 7.1.1 18 | Suggests: 19 | renv 20 | -------------------------------------------------------------------------------- /man/standard_analysis.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/standard_analysis.R 3 | \name{standard_analysis} 4 | \alias{standard_analysis} 5 | \title{Create standard analysis template for RStudio project} 6 | \usage{ 7 | standard_analysis( 8 | path, 9 | include_analysis_for_publication, 10 | use_renv, 11 | include_gitignore, 12 | ... 13 | ) 14 | } 15 | \arguments{ 16 | \item{path}{path of the new project} 17 | 18 | \item{include_analysis_for_publication}{additional Analysis for publication folder} 19 | 20 | \item{use_renv}{should renv be used?} 21 | 22 | \item{include_gitignore}{should a gitingore file be created} 23 | 24 | \item{...}{additional parameters, currently not used} 25 | } 26 | \value{ 27 | no return values, just the folders and README are created 28 | } 29 | \description{ 30 | Create standard analysis template for RStudio project 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2023 Max Planck Institute of Psychiatry 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The analysistemplates package 2 | 3 | The RStudio projects of your colleagues all have a different structure? It is 4 | difficult to navigate and understand others' projects? Or even your own project 5 | from a few months/years ago? Therefore, it is useful 6 | if you all agree on a standard folder structure for your analysis. Templates can 7 | help to enforce this. Currently, this package provides one standard data analysis 8 | template for RStudio projects geared towards scientific use cases. Whenever you 9 | start a new project in RStudio, this package provides the standard folder structure 10 | directly in your code editor. Also, it can help you improving reproducibility by 11 | using git and `renv`. 12 | 13 | ![folder structure of the template](tools/overview_folder_structure.png) 14 | 15 | ## Installation 16 | 17 | ``` r 18 | remotes::install_github("jonas-hag/analysistemplates") 19 | ``` 20 | 21 | ## Usage 22 | 23 | To create a new project with the folder structure shown below, follow these steps: 24 | 25 | 1. Install the package 26 | 2. Restart RStudio 27 | 3. When creating a new RStudio project with the "New directory" option, choose "Standard analysis template" 28 | 29 | ![RStudio Project Wizard showing the "Standard analysis template" option](tools/project_wizard_with_template.png) 30 | 31 | 4. During initialization you can select if a folder called `06_Analysis_for_publication` is included (check "Include Analysis for publication folder") 32 | 5. You can select if you want to generate a `.gitignore` file (check "Create a .gitignore file") 33 | 6. You can select if you want to use `renv` (check "Use renv with this project") 34 | 7. Once you've created the project, you will be provided with the instructions how to create a git repository for your project and connect to github/gitlab 35 | 36 | ## Template overview 37 | 38 | A new project contains the following folder structure: 39 | 40 | ``` 41 | |-- 01_Data 42 | | |-- 01_Raw 43 | | `-- 02_Clean 44 | |-- 02_Analysis 45 | | |-- 01_Scripts 46 | | |-- 02_Results 47 | | |-- 03_Figures 48 | | `-- 04_Tables 49 | |-- 03_Manuscript 50 | | |-- 01_Text 51 | | `-- 02_Final_figures 52 | |-- 04_Presentation 53 | |-- 05_Misc 54 | |-- 06_Analysis_for_publication <-- optional 55 | | |-- 01_Scripts <-- optional 56 | | |-- 02_Results <-- optional 57 | | |-- 03_Figures <-- optional 58 | | `-- 04_Tables <-- optional 59 | |-- README.md 60 | |-- .gitignore <-- optional 61 | |-- renv <-- optional 62 | ``` 63 | ## Planned features 64 | 65 | - better integration with git/github 66 | 67 | ## I want a different template 68 | 69 | You can easily create your own template. Fork this repo and create a new pair of 70 | `.R` and `.dcf` files. `.dcf` defines 71 | how your template is named and which options are shown when you select the 72 | template in the project wizard. `.R` defines what happens when the 73 | template is used. Have a look at `standard_analysis.R` and `standard_analysis.dcf` 74 | for inspiration or check out the [RStudio Extensions documentation](https://rstudio.github.io/rstudio-extensions/rstudio_project_templates.html). 75 | -------------------------------------------------------------------------------- /R/standard_analysis.R: -------------------------------------------------------------------------------- 1 | #' Create standard analysis template for RStudio project 2 | #' 3 | #' @param path path of the new project 4 | #' @param include_analysis_for_publication additional Analysis for publication folder 5 | #' @param use_renv should renv be used? 6 | #' @param include_gitignore should a gitingore file be created 7 | #' @param ... additional parameters, currently not used 8 | #' 9 | #' @return no return values, just the folders and README are created 10 | #' @importFrom utils installed.packages 11 | #' @export 12 | standard_analysis <- function( 13 | path, 14 | include_analysis_for_publication, 15 | use_renv, 16 | include_gitignore, 17 | ...) { 18 | # ensure that the path exists 19 | dir.create(path, recursive = TRUE, showWarnings = FALSE) 20 | 21 | # create folder structure 22 | dir.create(paste0(path, "/01_Data")) 23 | dir.create(paste0(path, "/01_Data/01_Raw")) 24 | dir.create(paste0(path, "/01_Data/02_Clean")) 25 | 26 | dir.create(paste0(path, "/02_Analysis")) 27 | dir.create(paste0(path, "/02_Analysis/01_Scripts")) 28 | dir.create(paste0(path, "/02_Analysis/02_Results")) 29 | dir.create(paste0(path, "/02_Analysis/03_Figures")) 30 | dir.create(paste0(path, "/02_Analysis/04_Tables")) 31 | 32 | dir.create(paste0(path, "/03_Manuscript")) 33 | dir.create(paste0(path, "/03_Manuscript/01_Text")) 34 | dir.create(paste0(path, "/03_Manuscript/02_Final_figures")) 35 | 36 | dir.create(paste0(path, "/04_Presentation")) 37 | 38 | dir.create(paste0(path, "/05_Misc")) 39 | 40 | if (include_analysis_for_publication) { 41 | dir.create(paste0(path, "/06_Analysis_for_publication")) 42 | dir.create(paste0(path, "/06_Analysis_for_publication/01_Scripts")) 43 | dir.create(paste0(path, "/06_Analysis_for_publication/02_Results")) 44 | dir.create(paste0(path, "/06_Analysis_for_publication/03_Figures")) 45 | dir.create(paste0(path, "/06_Analysis_for_publication/04_Tables")) 46 | } 47 | 48 | # create a .gitignore file 49 | if (include_gitignore) { 50 | gitignore_content <- c( 51 | ".Rproj.user", 52 | ".Rhistory", 53 | ".RData", 54 | ".Ruserdata", 55 | "01_Data", 56 | "*/02_Results", 57 | "*/03_Figures", 58 | "*/04_Tables", 59 | "03_Manuscript/02_Final_figures", 60 | "04_Presentation", 61 | "*.html" 62 | ) 63 | 64 | gitignore_content <- paste0(gitignore_content, collapse = "\n") 65 | writeLines(gitignore_content, con = file.path(path, ".gitignore")) 66 | 67 | } 68 | 69 | # create a readme 70 | content <- c( 71 | "# Readme", 72 | "This is the template for a standard data analysis.", 73 | "Please give an overview what you do in this project and how to navigate it.", 74 | "", 75 | "## Git integration", 76 | "If you want to use git with your project (you should!), please do the following steps (replace `` with the actual name):", 77 | "", 78 | "1. Go to your git repository provider (github/gitlab) and create a new repository", 79 | "2. DON'T check 'Add a README file'", 80 | "3. Go to the Terminal within RStudio and type the following commands (for the URL, e.g. https://github.com):", 81 | "", 82 | "```bash", 83 | "git init", 84 | "git branch -M main", 85 | "git remote add origin /.git", 86 | "```", 87 | "", 88 | "4. Restart RStudio", 89 | "5. Select the files you want to commit in the git pane, click 'commit', in the pop-up write a commit message and click 'commit'", 90 | "6. In the terminal, execute the following command:", 91 | "", 92 | "```bash", 93 | "git push -u origin main", 94 | "```", 95 | "", 96 | "7. For the following commits, you can use the 'push' button in the git pane", 97 | "", 98 | "Please note that the following directories and files are not tracked by git by default (but you can change it in the .gitignore file):", 99 | "", 100 | "- 01_Data", 101 | "- all 02_Results folders", 102 | "- all 03_Figures folders", 103 | "- all 04_Tables folders", 104 | "- 03_Manuscript/02_Final_figures", 105 | "- 04_Presentation", 106 | "- all html files", 107 | "", 108 | "For more information about the integration of git and RStudio, check out https://happygitwithr.com." 109 | ) 110 | content <- paste0(content, collapse = "\n") 111 | writeLines(content, con = file.path(path, "README.md")) 112 | 113 | # initialise renv 114 | if (use_renv) { 115 | if (requireNamespace("renv", quietly = TRUE)) { 116 | renv::init( 117 | project = normalizePath(path), 118 | bare = TRUE 119 | ) 120 | } else { 121 | warning("renv couldn't be used as the `renv` package is not installed. If you want to use renv, please first install it with `install.packages('renv')`") 122 | } 123 | } 124 | } 125 | --------------------------------------------------------------------------------