Skip to content

Instantly share code, notes, and snippets.

@apoorv74
Last active February 1, 2018 15:06
Show Gist options
  • Save apoorv74/4295a9b85e96fa8ec074d5534a9bb865 to your computer and use it in GitHub Desktop.
Save apoorv74/4295a9b85e96fa8ec074d5534a9bb865 to your computer and use it in GitHub Desktop.
Create a new R project following the best practices
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly = TRUE)
# What is this: A script that assists in following best practices when starting a new project
# What are these best practices:
# - Creating a project is the first step, managing individual scripts is a mess
# - Always have a git setup at the root of the project
# - A README.md file for documentation
# - A .gitignore file for ignoring files and directories, remember .DS_Store
# What is a .Rproj file
# The file is created by Rstudio when starting new projects. The contents are as follows:
proj_contents <- "Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX"
gitignore_contents <- "# History files
.Rhistory
.Rapp.history
# Session Data files
.RData
# Example code in package build process
*-Ex.R
# Output files from R CMD build
/*.tar.gz
# Output files from R CMD check
/*.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
.DS_Store
"
project_setup <- function(project_root, project_name, open_flag=NULL){
if(length(project_root) != 1 | length(project_name) != 1 | length(open_flag) != 1){
stop("Please check your arguments")
}
# Where the project resides
file_path <- paste0(project_root,'/',project_name)
if(!dir.exists(file_path)){
dir.create(file_path)
}
project_file <- paste0(file_path,'/',project_name,'.Rproj')
readme_file <- paste0(file_path,'/','README.md')
gitignore_file <- paste0(file_path,'/','.gitignore')
print( "=> Creating files ... ")
# Creating files
file.create(project_file)
file.create(readme_file)
file.create(gitignore_file)
print( "=> Writing meta contents ... ")
# Writing project contents
con <- file(project_file,"w")
writeLines(con = con,text = proj_contents)
# Closing connection
close(con)
# Writing gitignore contents
con <- file(gitignore_file,"w")
writeLines(con = con,text = gitignore_contents, sep = "\n")
# Closing connection
close(con)
print( "=> Creating a git connection ... ")
# Creating a git connection at the root
system(paste0("git init ", file_path))
# If open_flag is set to TRUE, then open the project
if(open_flag == 1) {
print("=> Opening a project ... ")
system(paste0("open -a RStudio ", project_file))
}
}
project_setup(args[1], args[2], args[3])
@apoorv74
Copy link
Author

  • You can create an alias for the above script as alias analyse= "RScript <script_path> $1 $2 $3"
  • Then just execute analyse project_root project_name 1 and let RStudio take over

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment