forked from mca91/EconometricsWithR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_setup.Rmd
81 lines (68 loc) · 2.88 KB
/
_setup.Rmd
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
```{r html, echo=FALSE}
# globally set chunk options
knitr::opts_chunk$set(fig.align='center', out.width='80%')
my_output <- knitr::opts_knit$get("rmarkdown.pandoc.to")
# function for escaping special LaTeX characters
escape_latex = function(x, newlines = FALSE, spaces = FALSE) {
x = gsub('\\\\', '\\\\textbackslash', x)
x = gsub('([#$%&_{}])', '\\\\\\1', x)
x = gsub('\\\\textbackslash', '\\\\textbackslash{}', x)
x = gsub('~', '\\\\textasciitilde{}', x)
x = gsub('\\^', '\\\\textasciicircum{}', x)
if (newlines) x = gsub('(?<!\n)\n(?!\n)', '\\\\\\\\', x, perl = TRUE)
if (spaces) x = gsub(' ', '\\\\ \\\\ ', x)
x
}
# function for output dependent inline code formatting
ttcode <- function(x, type = "tt") {
outputFormat <- knitr:::pandoc_to()
if (outputFormat %in% c('latex', 'beamer'))
paste0("\\texttt{", escape_latex(x), "}")
else if (outputFormat == 'html')
paste0("<", type, ">", x, "</", type, ">")
else
x
}
# function to add html title and tag below stargazer table
stargazer_html_title <- function(title, tag) {
cat(paste("<caption><p style='text-align:center'>(#tab:", tag, ")", " ", title, "</p></caption>", sep = ""))
}
# function that generates a html file from dc exercise code and includes an iframe
write_html <- function(html_body = NULL, label = NULL, write = T, ex = T, height = 320, playground = F) {
#out_type <- knitr::opts_knit$get("rmarkdown.pandoc.to")
if (playground) {
cat(paste("<iframe src='DCL/playground.html' frameborder='0' scrolling='no' style='width:100%;height:", height+20, "px'></iframe>", sep = ""))
}
else if (write) {
code <- paste(
"<!DOCTYPE html>
<head>
<meta charset='utf-8'/>
<link rel='stylesheet' href='exercise.css'/>
<script async src='https://cdn.datacamp.com/dcl-react-dev.js.gz'></script>
</head>
<body>
<div data-datacamp-exercise data-lang='r' data-height =", height, ">",
html_body,
"</div>
</body>
</html>", sep = "")
if (ex == T) {
writeLines(code, sep = "\n", paste("DCL/ex", label, ".html", sep = ""))
cat(paste("<iframe src='DCL/ex", label, ".html' frameborder='0' scrolling='no' style='width:100%;height:", height+20, "px'></iframe>", sep = ""))
}
if (ex == F) {
writeLines(code, sep = "\n", paste("DCL/", label, ".html", sep = ""))
cat(paste("<iframe src='DCL/", label, ".html' frameborder='0' scrolling='no' style='width:100%;height:", height+20, "px'></iframe>", sep = ""))
}
} else {
cat(paste("<iframe src='DCL/ex", label, ".html' frameborder='0' scrolling='no' style='width:100%;height:", height+20, "px'></iframe>", sep = ""))
}
}
```
```{r latex, eval=knitr::opts_knit$get("rmarkdown.pandoc.to") == "latex", echo=FALSE}
knitr::opts_chunk$set(fig.width=5.6, fig.height=3.7, fig.align='center', fig.pos = 'h')
```
```{r global, echo=FALSE}
knitr::opts_chunk$set(message=F, warning=F)
```