forked from yihui/r-ninja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knit
executable file
·48 lines (41 loc) · 1.69 KB
/
knit
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
#!/usr/bin/Rscript
## ./knit [epub|github|html|pdf] to generate different types of output
library(knitr)
## global chunk options: use high quality Cairo PNG device
pdf.options(pointsize = 10) # smaller pointsize for recording plots
opts_chunk$set(cache=TRUE, cache.path = 'cache-local/',
fig.width=5, fig.height=5, par=TRUE)
## xtable options (generate HTML by default)
options(xtable.type = 'html', xtable.html.table.attributes = '',
table.placement = '', xtable.comment = FALSE)
## chunk hooks
knit_hooks$set(par=function(before, options, envir){
if (before && options$fig.show!='none')
par(mar=c(4,4,.1,.1), cex.lab=.95, cex.axis=.9, mgp=c(2,.7,0), tcl=-.3)
})
local({
fmt = commandArgs(TRUE)
if (length(fmt) == 0L) fmt = 'epub'
if (fmt %in% c('github', 'html')) {
# upload to Imgur when output is for github or html
opts_knit$set(upload.fun = imgur_upload)
opts_chunk$set(cache.path = 'cache-upload/')
} else if (fmt == 'pdf') {
options(xtable.type = 'latex')
opts_chunk$set(cache.path = 'cache-pdf/')
# use high quality tikz graphics for PDF output
opts_chunk$set(dev = 'tikz', fig.width=4, fig.height=4)
x = readLines('template.tex')
options(tikzDocumentDeclaration = x[1], tikzDefaultEngine = 'xetex')
assign('tikzPackages', c(x[2:13], '\n'), envir = knitr:::.knitEnv)
}
# knit all md files under ./source
for (f in list.files('./source/', '\\.md$', full.names = TRUE)) {
unlink(basename(f))
knit(f, output = basename(f))
# make sure you do not do stupid things (edit output instead of source)
Sys.chmod(basename(f), mode = '0444')
}
})
## convert to an ebook (EPUB) or other formats
source('mdconvert.sh')