Hao Zhu | e0a36a8 | 2015-11-23 15:35:20 -0500 | [diff] [blame] | 1 | #' Rmarkdown Format |
| 2 | #' |
| 3 | #' @description If the export format of the Rmarkdown document exist, |
| 4 | #' |
| 5 | #' @importFrom rmarkdown metadata |
| 6 | #' |
| 7 | #' @export |
| 8 | |
| 9 | rmd_format <- function(){ |
| 10 | rmd_output_metadata <- metadata$output |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 11 | # rmd_fmt <- ifelse( |
| 12 | # is.null(rmd_output_metadata), |
| 13 | # "markdown", ifelse( |
| 14 | # rmd_output_metadata %in% c("html_document", "rmarkdown::html_vignette"), |
| 15 | # "html",ifelse( |
| 16 | # rmd_output_metadata %in% c("pdf_document", "rmarkdown::tufte_handout"), |
| 17 | # "latex", "markdown" |
| 18 | # ))) |
| 19 | return(names(rmd_output_metadata)) |
| 20 | } |
| 21 | |
| 22 | #' Load a LaTeX package |
| 23 | #' |
| 24 | #' @description Load a LaTeX package using R code. Just like `\\usepackage{}` |
| 25 | #' in LaTeX |
| 26 | #' |
| 27 | #' @param name The LaTeX package name |
| 28 | #' @param options The LaTeX options for the package |
Hao Zhu | 78e6122 | 2017-05-24 20:53:35 -0400 | [diff] [blame] | 29 | #' |
| 30 | #' @examples usepackage_latex("xcolor") |
Hao Zhu | c1f3841 | 2017-02-23 12:13:48 -0500 | [diff] [blame] | 31 | #' @export |
| 32 | usepackage_latex <- function(name, options = NULL) { |
| 33 | invisible(knit_meta_add(list(latex_dependency(name, options)))) |
Hao Zhu | e0a36a8 | 2015-11-23 15:35:20 -0500 | [diff] [blame] | 34 | } |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 35 | |
| 36 | # Find the right xml section. Since xml_child + search name will result in a |
| 37 | # crash (at least on my machine), here is a helper function. |
| 38 | xml_tpart <- function(x, part) { |
| 39 | xchildren <- xml_children(x) |
| 40 | children_names <- xml_name(xchildren) |
Hao Zhu | fc14c9b | 2017-05-22 14:03:22 -0400 | [diff] [blame] | 41 | if(!part %in% children_names) return(NULL) |
Hao Zhu | 62cdde5 | 2017-05-20 22:16:03 -0400 | [diff] [blame] | 42 | return(xchildren[[which(children_names == part)]]) |
| 43 | } |
| 44 | |
| 45 | positions_corrector <- function(positions, group_header_rows, n_row) { |
| 46 | pc_matrix <- data.frame(row_id = 1:n_row) |
| 47 | pc_matrix$group_header <- pc_matrix$row_id %in% group_header_rows |
| 48 | pc_matrix$adj <- cumsum(pc_matrix$group_header) |
| 49 | pc_matrix$old_id <- cumsum(!pc_matrix$group_header) |
| 50 | pc_matrix$old_id[duplicated(pc_matrix$old_id)] <- NA |
| 51 | adjust_numbers <- pc_matrix$adj[pc_matrix$old_id %in% positions] |
| 52 | return(adjust_numbers + positions) |
| 53 | } |
Hao Zhu | 7360428 | 2017-06-11 22:08:48 -0400 | [diff] [blame] | 54 | |
| 55 | latex_row_cells <- function(x) { |
| 56 | strsplit(x, " \\& ") |
| 57 | } |
Hao Zhu | 2a87e8e | 2017-06-14 15:49:33 -0400 | [diff] [blame] | 58 | |
Hao Zhu | 2ce42b9 | 2017-06-15 17:15:33 -0400 | [diff] [blame] | 59 | regex_escape <- function(x, double_backslash = FALSE) { |
| 60 | if (double_backslash) { |
| 61 | x <- gsub("\\\\", "\\\\\\\\", x) |
| 62 | } |
| 63 | x <- gsub("\\$", "\\\\\\$", x) |
| 64 | x <- gsub("\\(", "\\\\(", x) |
| 65 | x <- gsub("\\)", "\\\\)", x) |
| 66 | x <- gsub("\\[", "\\\\]", x) |
| 67 | x <- gsub("\\[", "\\\\]", x) |
| 68 | x <- gsub("\\{", "\\\\{", x) |
| 69 | x <- gsub("\\}", "\\\\}", x) |
| 70 | x <- gsub("\\*", "\\\\*", x) |
| 71 | return(x) |
| 72 | } |
Hao Zhu | 96a50b5 | 2017-06-14 18:09:35 -0400 | [diff] [blame] | 73 | |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 74 | as_kable_xml <- function(x) { |
| 75 | tmp <- tempfile(fileext = ".xml") |
| 76 | write_xml(x, tmp, options = "no_declaration") |
Hao Zhu | 938b657 | 2017-07-24 17:34:46 -0400 | [diff] [blame] | 77 | out <- read_file(tmp) |
Hao Zhu | f2dfd14 | 2017-07-24 14:43:28 -0400 | [diff] [blame] | 78 | out <- structure(out, format = "html", class = "knitr_kable") |
| 79 | return(out) |
| 80 | } |
Hao Zhu | 9bab153 | 2017-07-24 15:08:41 -0400 | [diff] [blame] | 81 | |
| 82 | read_kable_as_xml <- function(x) { |
| 83 | kable_html <- read_html(as.character(x)) |
| 84 | xml_child(xml_child(kable_html, 1), 1) |
| 85 | } |