-
Notifications
You must be signed in to change notification settings - Fork 2
/
process_source_excel_taxonomy.R
65 lines (45 loc) · 2.89 KB
/
process_source_excel_taxonomy.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
# install.packages("readxl")
library("readxl")
# read raw excel file
raw.taxonomy <- read_excel("source/Taxonomie ShareStats versie 17 maart 2021.xlsx", sheet = 1)
# select only the levels to work with
taw.taxonomy.levels <- raw.taxonomy[,1:4]
# Restructure file list view
n.cols <- dim(taw.taxonomy.levels)[1]
terms <- vector()
level.vector <- vector()
level1 = level2 = level3 = level4 = character()
htmlList <- file("taxonomy_list.html", open = "w+")
cat("<ul>", file = htmlList, append=TRUE, sep = "\n")
for(row in 1:n.cols) {
for(col in 1:4) {
# Only print if cels are not empty
if(!is.na(taw.taxonomy.levels[row, col])) {
value <- as.character(taw.taxonomy.levels[row, col])
if(col == 1 && !is.na(taw.taxonomy.levels[row, col])) { level1 = value; level2 = level3 = level4 = character(); }
if(col == 2 && !is.na(taw.taxonomy.levels[row, col])) { level2 = value; level3 = level4 = character(); }
if(col == 3 && !is.na(taw.taxonomy.levels[row, col])) { level3 = value; level4 = character(); }
if(col == 4 && !is.na(taw.taxonomy.levels[row, col])) level4 = value;
# Determine level per line
if(length(level2) == 0 && length(level3) == 0 && length(level4) == 0) level = 1
if(length(level2) != 0 && length(level3) == 0 && length(level4) == 0) level = 2
if(length(level2) != 0 && length(level3) != 0 && length(level4) == 0) level = 3
if(length(level2) != 0 && length(level3) != 0 && length(level4) != 0) level = 4
print(paste(level1, level2, level3, level4))
level.vector <- append(level.vector, level)
# Add new sub list for new level
if(row > 1 && level.vector[row] - level.vector[row - 1] == 1) cat("<ul>", file = htmlList, sep = "\n")
# Close sublists for previous levels.
if(row > 1 && level.vector[row] - level.vector[row - 1] == -1) cat("</ul>", file = htmlList, sep = "\n")
if(row > 1 && level.vector[row] - level.vector[row - 1] == -2) cat("</ul></ul>", file = htmlList, sep = "\n")
if(row > 1 && level.vector[row] - level.vector[row - 1] == -3) cat("</ul></ul></ul>", file = htmlList, sep = "\n")
# Add list elements per level
if(level == 1) cat(paste0('<li><span class="tag" onclick="copy_data(c',row,')" id="c',row,'">',level1,'</span></li>'), file = htmlList, sep = "\n")
if(level == 2) cat(paste0(' <li><span class="tag" onclick="copy_data(c',row,')" id="c',row,'">',level1,'/',level2,'</span></li>'), file = htmlList, sep = "\n")
if(level == 3) cat(paste0(' <li><span class="tag" onclick="copy_data(c',row,')" id="c',row,'">',level1,'/',level2,'/',level3,'</span></li>'), file = htmlList, sep = "\n")
if(level == 4) cat(paste0(' <li><span class="tag" onclick="copy_data(c',row,')" id="c',row,'">',level1,'/',level2,'/',level3,'/',level4,'</span></li>'), file = htmlList, sep = "\n")
}
}
}
cat("</ul>", file = htmlList)
close(htmlList)