-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathknit
More file actions
executable file
·96 lines (79 loc) · 2.74 KB
/
knit
File metadata and controls
executable file
·96 lines (79 loc) · 2.74 KB
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
#!/usr/bin/Rscript
## Original Source: github.com/yihui/knitr-book/knit.sh
## Author : Yihui Xie
## Modified : Ramnath Vaidyanathan
## LOAD KNITR LIBRARY -----------------------------------------------------
library(knitr)
library(ascii)
options(asciiType = 'rest')
## INPUT PATTERN FOR CODE CHUNKS --------------------------------
## `ro [global options] or`
## ``` {r code begin} [block code] ````
## `ri [inline code] ir`
pat_md()
## SET RST AS OUTPUT FORMAT -------------------------------------
render_rst()
## SET KNITR OPTIONS --------------------------------------------
opts_knit$set(
out.format = 'gfm',
upload.fun = function(x){ gsub('png', '*', x, fixed = T) }
)
## SET CHUNK OPTIONS --------------------------------------------
opts_chunk$set(
dev = c('png', 'pdf'),
fig.path = "_downloads/",
cache.path = ".cache/",
fig.width = 4,
fig.height = 4,
fig.align = "center",
tidy = FALSE
)
hook_out = knit_hooks$get('output')
knit_hooks$set(output = function(x, options){
if (options$results == 'html'){
relpath = ifelse(is.null(options$relpath), "../_downloads", options$relpath)
x = gsub("_downloads", relpath, x, fixed = TRUE)
content = knitr:::make_directive('raw', 'html', '', paste(x, collapse = '\n'))
paste(c('\n\n.. only:: html\n', knitr:::indent_block(content), ''), collapse = '\n')
} else {
hook_out(x, options)
}
})
knit_hooks$set(document = function(x, options){
gsub('``x', "```", x, fixed = TRUE)
})
make_image <- function(r1, path){
r1$save(path, cdn = TRUE)
img <- gsub(".html$", "", path)
invisible(take_screenshot(path, img))
cat(knitr:::make_directive('image', sprintf('_knit/%s.png', img), ""))
}
make_iframe <- function(r1){
content = paste(capture.output(r1$show('iframesrc', cdn = TRUE)), collapse = '\n')
cat(knitr:::make_directive('raw', 'html', '', content))
}
latex_image <- function(img){
cat(sprintf(":download:`Standalone <_downloads/%s.html>`, `Play <../playground.html#?n=%s>`_", img, img), sep = " ")
cat('\n')
cat(knitr:::make_directive('only', 'latex', '',
content = knitr:::make_directive('image', paste0(img, "-html.png"), '')
))
}
make_edit_button <- function(opt){
content = sprintf('
<a href="../playground.html#?n=%s" class="button icon edit big">
<span>Edit</span>
</a><br/>
', opt)
content2 = sprintf('
<a href="../playground.html#?n=%s" class="btn btn-success btn-mini float-right" target="_blank">
<span class="fa fa-edit"></span> Edit
</a><br/>
', opt)
cat(knitr:::make_directive('raw', 'html', '', content2))
}
## KNIT DOCUMENT ------------------------------------------------
args = commandArgs(trailingOnly = TRUE)
library(methods)
knit(args[1])
purl(args[1], documentation = 1L)