Skip to content

Commit a8edea4

Browse files
committed
update(html_all_figs_tables.R) minor improvements to reduce redundancy in code
1 parent c942f08 commit a8edea4

File tree

1 file changed

+50
-96
lines changed

1 file changed

+50
-96
lines changed

R/html_all_figs_tables.R

Lines changed: 50 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,36 @@
1717
html_all_figs_tables <- function(rda_dir = getwd()) {
1818
if (!dir.exists(fs::path(rda_dir, "rda_files"))) {
1919
stop("'rda_files' folder not found. Did you enter the correct argument for rda_dir?")
20+
}
21+
# check if dir exists and present warning message/option message
22+
if (dir.exists(fs::path(getwd(), "all_tables_figures"))) {
23+
question1 <- readline(
24+
"The 'all_tables_figures' folder already exists within your working directory. Would you like to overwrite the files within this folder? (Y/N)"
25+
)
2026
} else {
21-
if (!dir.exists(fs::path(getwd(), "all_tables_figures"))) {
22-
asar::create_tables_doc(subdir = tempdir() ,
23-
include_all = TRUE,
24-
rda_dir = rda_dir)
27+
# indicate to proceed with function
28+
question1 <- "y"
29+
# create new folder for the html and qmd files
30+
dir.create(fs::path(rda_dir, "all_tables_figures"))
31+
doc_path <- fs::path(rda_dir, "all_tables_figures")
32+
}
2533

26-
asar::create_figures_doc(subdir = tempdir(),
27-
include_all = TRUE,
28-
rda_dir = rda_dir)
34+
if (regexpr(question1, "y", ignore.case = TRUE) == 1) {
35+
asar::create_tables_doc(
36+
subdir = tempdir() ,
37+
include_all = TRUE,
38+
rda_dir = rda_dir
39+
)
2940

30-
tabs_figs_text <- c(readLines(fs::path(tempdir(), "08_tables.qmd")), readLines(fs::path(tempdir(), "09_figures.qmd")))
41+
asar::create_figures_doc(
42+
subdir = tempdir(),
43+
include_all = TRUE,
44+
rda_dir = rda_dir
45+
)
3146

32-
yaml_text <-
47+
tabs_figs_text <- c(readLines(fs::path(tempdir(), "08_tables.qmd")), readLines(fs::path(tempdir(), "09_figures.qmd")))
48+
49+
yaml_text <-
3350
"---
3451
title: 'All Tables & Figures'
3552
format:
@@ -39,99 +56,36 @@ format:
3956
---
4057
"
4158

42-
new_html_qmd <- c(yaml_text, tabs_figs_text)
59+
new_html_qmd <- c(yaml_text, tabs_figs_text)
4360

44-
writeLines(new_html_qmd,
45-
fs::path(getwd(), "all_tables_figures.qmd"))
46-
quarto::quarto_render(
47-
input = fs::path(getwd(), "all_tables_figures.qmd"),
48-
output_file = "all_tables_figures.html"
49-
)
61+
writeLines(new_html_qmd,
62+
fs::path(rda_dir, "all_tables_figures.qmd"))
63+
quarto::quarto_render(
64+
input = fs::path(rda_dir, "all_tables_figures.qmd"),
65+
output_file = "all_tables_figures.html"
66+
)
5067

51-
# create new folder for the html and qmd files because these can't
52-
# be saved there before or while rendering
53-
dir.create(fs::path(getwd(), "all_tables_figures"))
54-
55-
file.rename(
56-
from = fs::path("all_tables_figures.html"),
57-
to = fs::path(
58-
getwd(),
59-
"all_tables_figures",
60-
"all_tables_figures.html"
61-
)
68+
file.rename(
69+
from = fs::path("all_tables_figures.html"),
70+
to = fs::path(
71+
doc_path,
72+
"all_tables_figures.html"
6273
)
74+
)
6375

64-
file.rename(
65-
from = fs::path("all_tables_figures.qmd"),
66-
to = fs::path(getwd(), "all_tables_figures", "all_tables_figures.qmd")
76+
file.rename(
77+
from = fs::path("all_tables_figures.qmd"),
78+
to = fs::path(
79+
doc_path,
80+
"all_tables_figures.qmd"
6781
)
82+
)
6883

69-
message("The html and qmd with all tables and figures were newly created.")
70-
71-
} else {
72-
question1 <- readline(
73-
"The 'all_tables_figures' folder already exists within your working directory. Would you like to overwrite the files within this folder? (Y/N)"
74-
)
84+
message(
85+
"The html and qmd with all tables and figures were generated and overwrote the previous versions."
86+
)
7587

76-
if (regexpr(question1, "y", ignore.case = TRUE) == 1) {
77-
asar::create_tables_doc(
78-
subdir = tempdir() ,
79-
include_all = TRUE,
80-
rda_dir = rda_dir
81-
)
82-
83-
asar::create_figures_doc(
84-
subdir = tempdir(),
85-
include_all = TRUE,
86-
rda_dir = rda_dir
87-
)
88-
89-
tabs_figs_text <- c(readLines(fs::path(tempdir(), "08_tables.qmd")), readLines(fs::path(tempdir(), "09_figures.qmd")))
90-
91-
yaml_text <-
92-
"---
93-
title: 'All Tables & Figures'
94-
format:
95-
html:
96-
toc: true
97-
embed-resources: true
98-
---
99-
"
100-
101-
new_html_qmd <- c(yaml_text, tabs_figs_text)
102-
103-
writeLines(new_html_qmd,
104-
fs::path(getwd(), "all_tables_figures.qmd"))
105-
quarto::quarto_render(
106-
input = fs::path(getwd(), "all_tables_figures.qmd"),
107-
output_file = "all_tables_figures.html"
108-
)
109-
110-
file.rename(
111-
from = fs::path("all_tables_figures.html"),
112-
to = fs::path(
113-
getwd(),
114-
"all_tables_figures",
115-
"all_tables_figures.html"
116-
)
117-
)
118-
119-
file.rename(
120-
from = fs::path("all_tables_figures.qmd"),
121-
to = fs::path(
122-
getwd(),
123-
"all_tables_figures",
124-
"all_tables_figures.qmd"
125-
)
126-
)
127-
128-
message(
129-
"The html and qmd with all tables and figures were regenerated and overwrote the previous versions."
130-
)
131-
132-
} else {
133-
warning("The html and qmd with all tables and figures were not regenerated.")
134-
}
135-
}
88+
} else {
89+
warning("The html and qmd with all tables and figures were not generated.")
13690
}
13791
}

0 commit comments

Comments
 (0)