-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmake_functions.R
109 lines (89 loc) · 3.17 KB
/
make_functions.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
# make_functions.r
# These functions functions to be used to stitch together all the .md files for the Phases of OHI Assessments.
## load libraries ----
library(knitr) # install.packages("knitr")
library(rmarkdown)
library(stringr)
# concatenate md files together ----
cat_md = function(
files_md = setdiff(list.files(getwd(), glob2rx('*.md')), out_md),
out_md = '_all_.md'){
if (file.exists(out_md)) unlink(out_md)
cat('---\n', 'title: ', title_header, '\n---\n\n', sep='', file=out_md, append=T)
for (md in files_md){
cat(paste(c(readLines(md),'',''), collapse='\n'), file=out_md, append=T)
}
}
# render pdf for OHI----
ohi_pdf = function(out_md) {
cat('creating .pdf file...\n')
render(
out_md,
pdf_document(
toc = T, toc_depth = 3, number_sections = T,
fig_width = 6.5, fig_height = 4.5, fig_crop = TRUE,
fig_caption = T, highlight = "default", template = "default",
keep_tex = F, latex_engine = "pdflatex",
includes = NULL, pandoc_args = NULL),
clean=T, quiet=F,
output_file = paste0(pfx, '.pdf'))
}
# render docx ----
ohi_docx = function(out_md) {
cat('creating .doc file...\n')
render(
out_md,
word_document(
fig_caption = T, fig_width = 7, fig_height = 5,
highlight='default', reference_docx='default'),
clean=T, quiet=F,
output_file = paste0(pfx, '.docx'))
}
# render html for OHI in local ohimanual repo----
ohi_html = function(out_md) {
cat('creating local .html file...\n')
render(
out_md,
html_document(
number_sections=T, fig_width = 3, fig_height = 2, fig_retina = 2, fig_caption = T, smart=T,
self_contained=F, theme=NULL,
highlight=NULL, mathjax='default',
toc=T, toc_depth=3),
clean=T, quiet=F,
output_file = paste0(pfx, '.html'))
}
## commit and push to master branch: ohi-science.github.io
push_to_web = function(out_md) {
## read filename
fn = str_split(out_md, "\\.");
f = fn[[1]][1]
dir_phases = '~/github/ohi-science.github.io/_includes/themes/OHI/phases'
dir_downloads = '~/github/ohi-science.github.io/assets/downloads/other'
## copy .html and .pdf files to ohi-science.github.io
file.copy(sprintf('%s.html', f), dir_phases, overwrite=T, recursive=T)
file.copy(sprintf('%s.pdf', f), dir_downloads, overwrite=T, recursive=T)
## copy figs for ohi-manual
if (f == 'ohi-manual') {
dir.create(dir_phases, showWarnings=F)
file.copy('fig', dir_phases, overwrite=T, recursive=T)
}
## git push ohi-science.github.io
system(sprintf(
'cd ~/github/ohi-science.github.io;
git checkout master;
git pull;
git add .;
git commit -m "%s.html pushed from ohimanual/make_*.r";
git push',
f, f))
message(sprintf('\nohi-science.org/%s was updated\n', f))
}
# little copy_archive to copy pdf files
copy_archive = function(dir_fn,
path_in = '~/github/ohimanual',
path_out = '~/github/ohi-science.github.io/assets/downloads/other') {
fn = str_split(dir_fn, '/')[[1]][2]
file.copy(sprintf('%s/%s%s', path_in, dir_fn, '.pdf'),
sprintf('%s/%s%s', path_out, fn, '.pdf'),
overwrite=T)
}