-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpdf_document.R
More file actions
121 lines (95 loc) · 3.91 KB
/
pdf_document.R
File metadata and controls
121 lines (95 loc) · 3.91 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#' HTML paged version for utilitR document
#'
#' This function presents an output format necessary
#' to build a PDF version of the documentation. This
#' is a paged HTML document which means an HTML with
#' usueal features for PDFs (page number, A4 formatting, etc.)
#'
#' @seealso [html_paged](pagedown::html_paged)
#' which is used as a customizable
#' basis to render R Markdown documents
#' @param extra_dependencies Additional HTML dependencies
#' @inheritParams pagedown::html_paged
#' @export
html_paged <- function(..., extra_dependencies = NULL){
extra_dependencies <- c(
utilitr_dependencies(output = "pagedown"),
# rmarkdown::html_dependency_font_awesome(),
extra_dependencies
)
extra_dependencies <- c(extra_dependencies,
rmarkdown::html_dependency_font_awesome())
of <- pagedown::html_paged(
..., #extra_dependencies = extra_dependencies,
css = utilitr_dependencies(output = "pagedown"),
toc = TRUE,
toc_depth = 2,
copy_resources = TRUE,
# self_contained = FALSE,
pandoc_args = c("--lua-filter",
pkg_resource("rmarkdown/resources/scripts/nbsp.lua")))
# do not show code
of$knitr$opts_chunk <- list(out.width='75%', fig.align='center')
of
}
#' Front-user function to build PDF
#'
#' This function must be used in the `R` console. This will generate
#' the documentation in a `_pagedown_output` directory
#' @inheritParams bookdown::render_book
#' @inheritParams pagedown::chrome_print
#' @param asHTML Logical indicating wether we prefer a HTML or a PDF (default)
#' file
#'
#' @importFrom bookdown render_book
#' @importFrom pagedown chrome_print html_paged
#' @import callr
#' @export
pdf_document <- function(extra_args = c('--disable-gpu', '--no-sandbox'),
timeout = 660,
verbose = 1,
asHTML = FALSE){
# css_default <- c(
# pkg_resource("/rmarkdown/resources/css/default.css"),
# pkg_resource("/rmarkdown/resources/css/style.css"),
# pkg_resource("/rmarkdown/resources/css/default-fonts.css"),
# pkg_resource("/rmarkdown/resources/css/default-page.css")
# )
#
# css <- c(css_default, css)
#
# bookdown::render_book("index.Rmd", ..., css = css,
# output_format = 'pagedown::html_paged',
# output_file = '_pagedown_output/index.html')
if (!dir.exists("_pagedown_output")) dir.create("_pagedown_output")
if (!file.exists("index.Rmd") && (file.exists("skeleton.Rmd"))){
message("Renaming skeleton.Rmd into index.Rmd to match bookdown rules")
file.rename("skeleton.Rmd", "index.Rmd")
}
if (!file.exists("index.Rmd") && (file.exists("Untitled.Rmd"))){
message("Renaming Untitled.Rmd into index.Rmd to match bookdown rules")
file.rename("Untitled.Rmd", "index.Rmd")
}
message("--------------------------------------------------------")
message("Knitting document in background. This may take some time")
callr::r(function(){
bookdown::render_book('index.Rmd',
# envir = new.env(),
config_file = "_bookdown.yml",
# self_contained = FALSE,
output_format = 'utilitr::html_paged',
output_file = '_pagedown_output/index.html')
}, timeout = timeout)
if (isTRUE(asHTML)){
message("Only compiling in HTML since asHTML is TRUE")
return(NULL)
}
message("--------------------------------------------------------")
message("Converting into PDF. This may also take some time")
pagedown::chrome_print('_pagedown_output/index.html',
'_pagedown_output/DocumentationR.pdf',
extra_args = c('--disable-gpu', '--no-sandbox'),
timeout = timeout,
options = list(transferMode = 'ReturnAsStream'),
verbose = verbose)
}