-
Notifications
You must be signed in to change notification settings - Fork 1
/
1_create_yaml.R
88 lines (70 loc) · 3.06 KB
/
1_create_yaml.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
library(stringr)
print('Creating yaml files in data/abstracts/')
file_path <- "https://docs.google.com/spreadsheets/d/1-jN59YPubMhsE0lKNl4d4HdIuO_sViKmjbDLIsyWuSI/pub?gid=0&single=true&output=tsv"
schedule <- read.csv(file = file_path,
sep = "\t", stringsAsFactors = F,
na.strings = "")
#use "" instead of NAs
scheduleBlank <- schedule #make copy
scheduleBlank[is.na(scheduleBlank)] <- ""
## datetime to character
scheduleBlank$time <- sub("^.+ (.+):00", "\\1", scheduleBlank$time)
output_dir <- "data/abstracts/"
if (!file.exists(output_dir)) {
dir.create(output_dir)
} else {
## Overwrite whatever output was generated before
unlink(output_dir, recursive=TRUE)
dir.create(output_dir)
}
iCount = 1
while(iCount <= nrow(scheduleBlank)) {
#print(paste("iCount=", iCount))
oneRow <- scheduleBlank[iCount, ] #one row
## get file name ready
fileNameSpace <- paste(oneRow$day, "_",
oneRow$time, "_",
oneRow$session_type,"_",
oneRow$paper,
".yaml",
sep = "" )
#replace space and semi-colon by underscore
fileNameFinal <- str_replace_all(string = fileNameSpace,
pattern = c(" |:"),
replacement = '')
## There are some trailing underscores in names without the paper field
## that should be removed
fileNameFinal <- sub("_.yaml$", ".yaml", fileNameFinal)
#loop through each columns to create the yaml file
iCount2 = 1
while(iCount2 <= ncol(oneRow)) {
#print(paste("iCount2=", iCount2))
# write key value pair into file
oneValueName <- names(oneRow)[iCount2]
oneValue <- paste("\"",unlist(oneRow[iCount2]),"\"", sep ="")
# Remove quotes form talks if it contains one or more papers
if (oneValueName == "talks" && any(grepl("paper", oneValue))) {
oneValue <- sub("^\"(.+)\"$", "\\1", oneValue)
}
#write to file for the first time. Create new file
if (iCount2 == 1) {
#create file , no append
line <- paste(oneValueName, ": ", oneValue, sep = "")
write.table(line, col.names = FALSE, row.names = FALSE,
file = paste(output_dir,
fileNameFinal, sep=""),
append = FALSE,
quote = FALSE, )
} else {
#append to existing file
line <- paste(oneValueName, ": ", oneValue, sep = "")
write.table(line, col.names = FALSE, row.names = FALSE,
file = paste(output_dir,
fileNameFinal, sep=""),
append = TRUE,
quote = FALSE)
} # end of else
iCount2 <- iCount2 + 1
} #end of inside loop
iCount <- iCount + 1
} # end of outside loop