-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_project.R
120 lines (106 loc) · 2.37 KB
/
start_project.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#Setup
library(tidyverse)
library(lubridate)
#Get this Tuesday
i = wday(Sys.Date(), week_start = 1)
this_tuesday <- Sys.Date() + (2 - i)
#Ask user for date, set default to this Tuesday:
project_date <- readline(prompt = "Project Date (default is this Tuesday): ")
if (project_date == "") {
project_date = this_tuesday
} else {project_date = project_date
}
#Ask user for name of dataset
project_name <- readline(prompt = "Name: ")
# Clean project name
clean_name <- str_to_title(str_replace(project_name, "-", " "))
#Create new week folder
if(project_name != ""){
folder <- paste0(project_date,"-",project_name)
} else {
folder <- paste0(project_date)
}
dir.create(file.path(folder), recursive = TRUE)
# Create folder for figures to publish
dir.create(file.path(paste0(folder,"/","figs")))
# Create folder for data
#dir.create(file.path(paste0(folder,"/","data")))
#Create Rmarkdown file
script_file <- paste0(folder, "/", project_name, "-", "analysis.rmd")
file.create(script_file)
# Ask if the user wants to add data-download code:
data_download <- readline(prompt = "Add download code? (y/n, default, y)")
if (data_download == "y" || data_download == "") {
download_code <- paste0(
'```{r}',
'\n',
'tuesdata <- tidytuesdayR::tt_load("',
project_date,
'")',
'\n',
'```'
)
} else {
download_code <- ""
}
# Add default content to the script
script_text <- paste0(
## YAML heading
'---',
'\n',
'title: "',
project_date,
'-',
project_name,
'-analysis',
'"',
'\n',
'author: "Ben Leamon"',
'\n',
'date: ',
'"',
Sys.Date(),
'"',
'\n',
'output: html_document',
'\n',
'---',
'\n\n',
## Setup chunk
'```{r setup, include=FALSE}',
'\n',
'knitr::opts_chunk$set(echo = TRUE)',
'\n',
'```',
'\n\n',
'# ',
project_date,
' Tidy Tuesday: ',
clean_name,
'\n',
download_code,
'\n',
'```{r setup, include=FALSE}',
'\n',
'proj_path <- "',
project_date,
'-',
project_name,
'"',
'\n',
'entry_caption <- ""',
'\n',
'library(tidyverse)',
'\n',
'```',
''
# This is where to add any more code we want in the new script.
)
write(as.character(script_text), file(script_file))
# Update current-script
current_script <- file("current-script.txt", "wt")
text <- paste0(project_date, "-" , project_name)
writeLines(text, current_script)
close(current_script)
# Open the file
file.edit(script_file)