-
Notifications
You must be signed in to change notification settings - Fork 107
/
knitr_utils.R
164 lines (143 loc) · 5.46 KB
/
knitr_utils.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
check_dep <- function(){
if( !requireNamespace("rmarkdown") )
stop("package rmarkdown is required to use this function", call. = FALSE)
if( !requireNamespace("knitr") )
stop("package knitr is required to use this function", call. = FALSE)
}
get_default_pandoc_data_file <- function(format = "pptx") {
outfile <- tempfile(fileext = paste0(".", format))
pandoc_exec <- rmarkdown::pandoc_exec()
if(.Platform$OS.type %in% "windows"){
pandoc_exec <- paste0(pandoc_exec, ".exe")
}
if(!rmarkdown::pandoc_available() || !file.exists(pandoc_exec)){
# Use officer template when no pandoc - stupid fallback as if o pandoc, no result
outfile <- system.file(package = "officer", "template", paste0("template.", format))
} else {
# note in that case outfile will be removed when session will end
ref_doc <- paste0("reference.", format)
system2(pandoc_exec,
args = c("--print-default-data-file", ref_doc),
stdout = outfile)
}
return(outfile)
}
#' @export
#' @title Get the document being used as a template
#' @description Get filename of the document
#' being used as a template in an R Markdown document
#' rendered as HTML, PowerPoint presentation or Word document. It requires
#' packages rmarkdown >= 1.10.14 and knitr.
#' @param format document format, one of 'pptx', 'docx' or 'html'
#' @return a name file
#' @importFrom utils compareVersion packageVersion
#' @family functions for officer extensions
#' @keywords internal
get_reference_value <- function(format = NULL) {
if( !is.null(format) && length(format) != 1 ){
stop("format must be a scalar character")
}
check_dep()
if( compareVersion(as.character(packageVersion("rmarkdown")), "1.10.14") < 0 )
stop("package rmarkdown >= 1.10.14 is required to use this function")
if( is.null(format)){
if( grepl( "docx", knitr::opts_knit$get("rmarkdown.pandoc.to") ) ){
format <- "docx"
} else if( grepl( "pptx", knitr::opts_knit$get("rmarkdown.pandoc.to") ) ){
format <- "pptx"
} else if( grepl( "html", knitr::opts_knit$get("rmarkdown.pandoc.to") ) ){
format <- "html"
} else if( grepl( "latex", knitr::opts_knit$get("rmarkdown.pandoc.to") ) ){
format <- "latex"
} else {
stop("Unable to determine the format that should be used")
}
}
if( !format %in% c("pptx", "docx", "html") ){
stop("format must be have value 'docx', 'pptx' or 'html'.")
}
output.dir <- knitr::opts_knit$get("output.dir")
if(is.null(output.dir)){
output.dir <- getwd()
}
pandoc_args <- knitr::opts_knit$get("rmarkdown.pandoc.args")
rd <- grep("--reference-doc", pandoc_args)
if (length(rd)) {
reference_data <- pandoc_args[rd + 1]
if(!file.exists(reference_data))
reference_data <- file.path(output.dir, reference_data)
} else {
reference_data <- get_default_pandoc_data_file(format = format)
}
return(normalizePath(reference_data, winslash = "/"))
}
knitr_opts_current <- function(x, default = FALSE){
check_dep()
x <- knitr::opts_current$get(x)
if(is.null(x)) x <- default
x
}
#' @export
#' @title Get table options in a 'knitr' context
#' @description Get options for table rendering.
#'
#' It should not be used by the end user.
#' The function is a utility to facilitate the retrieval of table
#' options supported by the 'flextable', 'officedown' and of
#' course 'officer' packages.
#' @return a list with following elements:
#'
#' * cap.style (default: NULL)
#' * cap.pre (default: "Table ")
#' * cap.sep (default: ":")
#' * id (default: NULL)
#' * cap (default: NULL)
#' * topcaption (default: TRUE)
#' * style (default: NULL)
#' * tab.lp (default: "tab:")
#' * table_layout (default: "autofit")
#' * table_width (default: 1)
#' * first_row (default: TRUE)
#' * first_column (default: FALSE)
#' * last_row (default: FALSE)
#' * last_column (default: FALSE)
#' * no_hband (default: TRUE)
#' * no_vband (default: TRUE)
#' @family functions for officer extensions
#' @keywords internal
opts_current_table <- function() {
tab.cap.style <- knitr_opts_current("tab.cap.style", default = NULL)
tab.cap.pre <- knitr_opts_current("tab.cap.pre", default = "Table ")
tab.cap.sep <- knitr_opts_current("tab.cap.sep", default = ":")
tab.cap <- knitr_opts_current("tab.cap", default = NULL)
tab.topcaption <- knitr_opts_current("tab.topcaption", default = TRUE)
tab.id <- knitr_opts_current("tab.id", default = NULL)
tab.lp <- knitr_opts_current("tab.lp", default = "tab:")
tab.style <- knitr_opts_current("tab.style", default = NULL)
tab.layout <- knitr_opts_current("tab.layout", default = "autofit")
tab.width <- knitr_opts_current("tab.width", default = 1)
first_row <- knitr_opts_current("first_row", default = TRUE)
first_column <- knitr_opts_current("first_column", default = FALSE)
last_row <- knitr_opts_current("last_row", default = FALSE)
last_column <- knitr_opts_current("last_column", default = FALSE)
no_hband <- knitr_opts_current("no_hband", default = TRUE)
no_vband <- knitr_opts_current("no_vband", default = TRUE)
list(
cap.style = tab.cap.style,
cap.pre = tab.cap.pre,
cap.sep = tab.cap.sep,
id = tab.id,
topcaption = tab.topcaption,
cap = tab.cap,
style = tab.style,
tab.lp = tab.lp,
table_layout = table_layout(type = tab.layout),
table_width = table_width(width = tab.width, unit = "pct"),
first_row = first_row,
first_column = first_column,
last_row = last_row,
last_column = last_column,
no_hband = no_hband,
no_vband = no_vband
)
}