Skip to content

Commit 25394a8

Browse files
Add venue subcategory functionality
1 parent e3d40ed commit 25394a8

File tree

3 files changed

+207
-39
lines changed

3 files changed

+207
-39
lines changed

R/utils_render_reigster_html.r

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ generate_html_document_yml <- function(output_dir) {
5959
#' @param register_table_name The register table name. If this is NULL we are generating list of venues/ codecheckers
6060
#'
6161
#' @importFrom whisker whisker.render
62-
create_index_postfix_html <- function(output_dir, filter, register_table_name = NULL){
62+
create_index_postfix_html <- function(output_dir, filter, register_table_name, is_reg_table){
6363

6464
# When we have register table names, we are handling the case of reg tables
65-
if (!is.null(register_table_name)){
65+
if (is_reg_table){
6666
postfix_template <- readLines(CONFIG$TEMPLATE_DIR[["reg"]][["postfix"]], warn = FALSE)
6767
# Render the template with the correct hrefs
6868
hrefs <- generate_html_postfix_hrefs_reg(filter, register_table_name)
@@ -168,8 +168,8 @@ generate_href <- function(filter, register_table_name, href_type) {
168168
#' @param output_dir The output directory of the section files
169169
#' @param filter The filter name
170170
#' @param register_table_name The register table name
171-
create_index_section_files <- function(output_dir, filter, register_table_name = NULL) {
172-
create_index_postfix_html(output_dir, filter, register_table_name)
171+
create_index_section_files <- function(output_dir, filter, register_table_name, is_reg_table) {
172+
create_index_postfix_html(output_dir, filter, register_table_name, is_reg_table)
173173
create_index_prefix_html(output_dir)
174174
create_index_header_html(output_dir)
175175
}
@@ -185,7 +185,7 @@ render_register_html <- function(filter, register_table, register_table_name){
185185
register_table <- add_repository_links_html(register_table)
186186

187187
# Dynamically create the index header, prefix and postfix files
188-
create_index_section_files(output_dir, filter, register_table_name)
188+
create_index_section_files(output_dir, filter, register_table_name, is_reg_table = TRUE)
189189
generate_html_document_yml(output_dir)
190190

191191
# Capture the HTML output from a markdown file
Lines changed: 141 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,59 @@
1+
render_non_register_tables_html <- function(list_reg_tables, page_type){
2+
3+
output <- switch(page_type,
4+
"codecheckers" = render_table_codecheckers_html(list_reg_tables),
5+
"venues" = render_tables_venues_html(list_reg_tables),
6+
stop("Unsupported non-register table page type")
7+
)
8+
9+
# Ensuring output is a list, wrapping it if necessary
10+
# This is needed when the render function returns a single table which is the
11+
# case when there are not subcategory tables such as the case for codecheckers
12+
if (is.data.frame(output)){
13+
output <- list(page_type = output)
14+
}
15+
return(output)
16+
}
17+
118
#' Renders non-register pages such as codecheckers or venues page.
219
#'
320
#' @param list_reg_tables The list of register tables to link to in this html page
421
render_non_register_htmls <- function(list_reg_tables, page_type){
5-
output_dir <- paste0("docs/", page_type, "/")
6-
extra_text <- ""
22+
list_tables <- render_non_register_tables_html(list_reg_tables, page_type)
723

8-
if (page_type == "codecheckers"){
9-
table <- render_table_codecheckers_html(list_reg_tables)
10-
# Counting number of codecheckers based of number of codechecker reg tables
11-
# The table is a kable table and hence we cannot count rows
12-
no_codecheckers <- length(list_reg_tables)
13-
# Using number of codechecks from CONFIG instead of "no. of codechecks" column to avoid double count
14-
subtext <- paste("In total,", no_codecheckers, "codecheckers contributed", CONFIG$NO_CODECHECKS, "codechecks*")
15-
16-
# Extra text to explain why total_codechecks != SUM(no.of codechecks)
17-
extra_text <- "<i>\\*Note that the total codechecks is less than the collective sum of
18-
individual codecheckers' number of codechecks.
19-
This is because some codechecks involved more than one codechecker.</i>"
20-
}
24+
for (table_name in names(list_tables)){
25+
table <- list_tables[[table_name]]
2126

22-
else if (page_type == "venues") {
23-
table <- render_table_venues_html(list_reg_tables)
27+
# Case where we are dealing with venue subcategories
28+
if (page_type == "venues" & table_name != "all_venues"){
29+
output_dir <- paste0("docs/", page_type, "/", table_name, "/")
30+
}
31+
32+
else{
33+
output_dir <- paste0("docs/", page_type, "/")
34+
}
2435

25-
no_venues <- length(list_reg_tables)
26-
subtext <- paste("In total,", CONFIG$NO_CODECHECKS, "codechecks were completed for", no_venues, "venues")
36+
html_header <- generate_html_header(table, page_type, table_name)
37+
generate_non_reg_html(table, table_name, page_type, html_header, output_dir)
2738
}
39+
}
2840

29-
# Creating and adjusting the markdown table
41+
generate_non_reg_html <- function(table, table_subcategory, page_type, html_header, output_dir){
3042
table <- kable(table)
43+
44+
# Creating and adjusting the markdown table
3145
md_table <- load_md_template(CONFIG$TEMPLATE_DIR[["non_reg"]][["md_template"]])
32-
title <- paste0("CODECHECK List of ", page_type)
33-
md_table <- gsub("\\$title\\$", title, md_table)
34-
md_table <- gsub("\\$subtitle\\$", subtext, md_table)
46+
md_table <- gsub("\\$title\\$", html_header[["title"]], md_table)
47+
md_table <- gsub("\\$subtitle\\$", html_header[["subtext"]], md_table)
3548
md_table <- gsub("\\$content\\$", paste(table, collapse = "\n"), md_table)
36-
md_table <- gsub("\\$extra_text\\$", extra_text, md_table)
49+
md_table <- gsub("\\$extra_text\\$", html_header[["extra_text"]], md_table)
3750

3851
# Saving the table to a temp md file
3952
temp_md_path <- paste0(output_dir, "temp.md")
4053
writeLines(md_table, temp_md_path)
4154

4255
# Creating the correct html yaml and index files
43-
create_index_section_files(output_dir, page_type)
56+
create_index_section_files(output_dir, page_type, table_subcategory, is_reg_table = FALSE)
4457
generate_html_document_yml(output_dir)
4558
yaml_path <- normalizePath(file.path(getwd(), paste0(output_dir, "html_document.yml")))
4659

@@ -68,19 +81,115 @@ render_non_register_htmls <- function(list_reg_tables, page_type){
6881
#'
6982
#' @param list_reg_tables The list of register tables needed for the information.
7083
render_non_register_jsons <- function(list_reg_tables, page_type){
71-
output_dir <- paste0("docs/", page_type, "/")
84+
if (page_type == "codecheckers"){
85+
list_tables <- list("codecheckers" = render_table_codecheckers_json(list_reg_tables))
86+
}
87+
88+
else if (page_type == "venues") {
89+
list_tables <- render_tables_venues_json(list_reg_tables)
90+
}
91+
92+
for (table_name in names(list_tables)){
93+
table <- list_tables[[table_name]]
94+
output_dir <- paste0("docs/", page_type, "/")
95+
96+
# Case where we are dealing with venue subcategories
97+
if (page_type == "venues" & table_name != "all_venues"){
98+
output_dir <- paste0("docs/", page_type, "/", table_name, "/")
99+
}
100+
101+
jsonlite::write_json(
102+
table,
103+
path = paste0(output_dir, "index.json"),
104+
pretty = TRUE
105+
)
106+
}
107+
}
72108

109+
generate_html_title_non_registers <- function(page_type, table_name){
110+
title_base <- "CODECHECK List of"
111+
112+
# Adjusting title for venues subcategory
113+
if (page_type == "venues" & table_name != "all_venues"){
114+
# Replacing the word with plural
115+
plural_subcategory <- switch (table_name,
116+
"conference" = "conferences",
117+
"journal" = "journals",
118+
"community" = "communities"
119+
)
120+
title <- paste(title_base, plural_subcategory)
121+
}
122+
123+
else{
124+
# The base title is "CODECHECK List of venues/ codecheckers"
125+
title <- paste(title_base, page_type)
126+
}
127+
128+
return(title)
129+
}
130+
131+
generate_html_extra_text_non_register <- function(page_type){
132+
extra_text <- ""
133+
134+
# Extra text to explain why total_codechecks != SUM(no.of codechecks) in the codechecker table
73135
if (page_type == "codecheckers"){
74-
table <- render_table_codecheckers_json(list_reg_tables)
136+
extra_text <- "<i>\\*Note that the total codechecks is less than the collective sum of
137+
individual codecheckers' number of codechecks.
138+
This is because some codechecks involved more than one codechecker.</i>"
139+
}
140+
141+
return(extra_text)
142+
}
143+
144+
145+
generate_html_subtext_non_register <- function(table, page_type, table_name){
146+
147+
# Setting the codecheck word to be plural or singular
148+
total_codechecks <- CONFIG$NO_CODECHECKS
149+
codecheck_word <- if (total_codechecks == 1) "codecheck" else "codechecks"
150+
extra_text <- ""
151+
152+
if (page_type == "codecheckers"){
153+
no_codecheckers <- nrow(table)
154+
# Adding asterik to refer to the extra text at the bottom of the page
155+
codecheck_word <- paste0(codecheck_word, "*")
156+
subtext <- paste("In total,", no_codecheckers, "codecheckers contributed", total_codechecks, codecheck_word)
75157
}
76158

77159
else if (page_type == "venues"){
78-
table <- render_table_venues_json(list_reg_tables)
160+
# For the general venues list
161+
if (table_name == "all_venues"){
162+
no_venues <- nrow(table)
163+
subtext <- paste("In total,", total_codechecks, codecheck_word, "were completed for", no_venues, "venues")
164+
}
165+
166+
else{
167+
no_venues_subcat <- nrow(table)
168+
venue_name_subtext <- table_name
169+
total_codechecks <- CONFIG$NO_CODECHECKS_VENUE_SUBCAT[[venue_name_subtext]]
170+
codecheck_word <- if (total_codechecks == 1) "codecheck" else "codechecks"
171+
172+
if (no_venues_subcat > 1){
173+
venue_name_subtext <- switch (table_name,
174+
"conference" = "conferences",
175+
"journal" = "journals",
176+
"community" = "communities"
177+
)
178+
}
179+
subtext <- paste("In total,", total_codechecks, codecheck_word, "were completed for", no_venues_subcat, venue_name_subtext)
180+
}
79181
}
80182

81-
jsonlite::write_json(
82-
table,
83-
path = paste0(output_dir, "index.json"),
84-
pretty = TRUE
183+
return(subtext)
184+
}
185+
186+
generate_html_header <- function(table, page_type, table_name){
187+
188+
html_header <- list(
189+
"title" = generate_html_title_non_registers(page_type, table_name),
190+
"subtext" = generate_html_subtext_non_register(table, page_type, table_name),
191+
"extra_text" = generate_html_extra_text_non_register(page_type)
85192
)
193+
194+
return(html_header)
86195
}

R/utils_render_table_venues.R

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
render_tables_venues_json <- function(list_venue_reg_tables){
2+
all_venues_table <- render_table_all_venues_json(list_venue_reg_tables)
3+
list_venues_subcat_tables <- render_table_venues_subcat(all_venues_table)
4+
5+
list_tables <- list("all_venues" = all_venues_table)
6+
list_tables <- c(list_tables, list_venues_subcat_tables)
7+
return(list_tables)
8+
}
9+
10+
111
#' Renders venues table in JSON format.
212
#'
313
#' @param list_venue_reg_tables The list of venue register tables. The indices are the venue names.
4-
render_table_venues_json <- function(list_venue_reg_tables){
14+
render_table_all_venues_json <- function(list_venue_reg_tables){
515
col_names <- CONFIG$NON_REG_TABLE_COL_NAMES[["venues_table"]]
616
list_venue_names <- names(list_venue_reg_tables)
717

@@ -47,12 +57,21 @@ render_table_venues_json <- function(list_venue_reg_tables){
4757
return(table_venues)
4858
}
4959

60+
render_tables_venues_html <- function(list_venue_reg_tables){
61+
all_venues_table <- render_table_all_venues_html(list_venue_reg_tables)
62+
list_venues_subcat_tables <- render_table_venues_subcat(all_venues_table)
63+
64+
list_tables <- list("all_venues" = all_venues_table)
65+
list_tables <- c(list_tables, list_venues_subcat_tables)
66+
return(list_tables)
67+
}
68+
5069
#' Renders venues table in HTML format.
5170
#' Each venue name links to the register table for that specific
5271
#' venue. The ORCID IDs link to their ORCID pages.
5372
#'
5473
#' @param list_venue_reg_tables The list of venue register tables. The indices are the ORCID IDs.
55-
render_table_venues_html <- function(list_venue_reg_tables){
74+
render_table_all_venues_html <- function(list_venue_reg_tables){
5675
col_names <- CONFIG$NON_REG_TABLE_COL_NAMES[["venues_table"]]
5776

5877
list_venue_names <- names(list_venue_reg_tables)
@@ -110,3 +129,43 @@ render_table_venues_html <- function(list_venue_reg_tables){
110129

111130
return(table_venues)
112131
}
132+
133+
# This function can be used for both the html and json table since it works on the
134+
# table for all venues. If the all_venues table is of html format then this creates
135+
# a html table and if it is of json format, this creates a json table.
136+
render_table_venues_subcat <- function(venues_table){
137+
CONFIG$NO_CODECHECKS_VENUE_SUBCAT <- c()
138+
list_tables <- c()
139+
140+
# The venues subcat table will be made from the venues_table.
141+
# Hence we set the column "venue type" to be NULL as it is redundant for the venues subcategory table
142+
col_names <- CONFIG$NON_REG_TABLE_COL_NAMES[["venues_table"]]
143+
144+
# The "venue name" column is later replaced with "{venue_subcat} name"
145+
venues_table_venue_name_col <- col_names[["venue_name"]]
146+
old_venue_name_col <- venues_table_venue_name_col
147+
148+
for (venue_subcat in CONFIG$FILTER_SUBCATEGORIES[["venues"]]){
149+
# Filtering and keeping the rows of venue_type == venue_subcat
150+
table_venue_subcategory <- venues_table[grepl(venue_subcat, venues_table$`Venue type`, ignore.case = TRUE), ]
151+
# Removing the column showing the row numbers and removing the column "Venue type"
152+
rownames(table_venue_subcategory) <- NULL
153+
table_venue_subcategory[col_names[["venue_type"]]] <- NULL
154+
155+
# Replace the column "Venue name" with "{venue_subcat} name" e.g "Journal/Conference name"
156+
# Capitalizing the first letter of the subcat name using str_to_title
157+
new_venue_name_col <- paste(stringr::str_to_title(venue_subcat), "name")
158+
colnames(table_venue_subcategory)[colnames(table_venue_subcategory) == old_venue_name_col] <- new_venue_name_col
159+
160+
# Extract the numeric part from the "no.of codechecks" column and convert to integers
161+
list_no_codechecks <- as.numeric(sub(" .*", "", table_venue_subcategory[[col_names[["no_codechecks"]]]]))
162+
no_codechecks_venue_subcat <- sum(list_no_codechecks)
163+
164+
# Noting down the number of codechecks for each venue subcategory. This is to be used in the subtext of the
165+
# html pages
166+
CONFIG$NO_CODECHECKS_VENUE_SUBCAT[[venue_subcat]] <- no_codechecks_venue_subcat
167+
168+
list_tables[[venue_subcat]] <- table_venue_subcategory
169+
}
170+
return(list_tables)
171+
}

0 commit comments

Comments
 (0)