Hao Zhu | db04e30 | 2015-11-15 16:57:38 -0500 | [diff] [blame^] | 1 | #' Magic mirror that returns kable's attributes |
| 2 | #' |
| 3 | #' @param input The output of kable |
| 4 | #' |
| 5 | #' @export |
| 6 | |
| 7 | magic_mirror <- function(input){ |
| 8 | if(!"knitr_kable" %in% attr(input, "format")){ |
| 9 | warning("magic_mirror may not be able to produce correct result if the", |
| 10 | " input table is not rendered by knitr::kable. ") |
| 11 | } |
| 12 | kable_format <- attr(input, "format") |
| 13 | if (kable_format == "latex"){ |
| 14 | magic_mirror_latex(input) |
| 15 | } |
| 16 | if (kable_format == "html"){ |
| 17 | magic_mirror_html(input) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | #' Magic mirror for latex tables |
| 22 | magic_mirror_latex <- function(input){ |
| 23 | # kable will put a begin{table} shell if caption is not NULL |
| 24 | caption <- ifelse( |
| 25 | str_detect(input, "\\\\caption\\{.*?\\}"), |
| 26 | str_match(input, "caption\\{(.*?)\\}")[2], NULL |
| 27 | ) |
| 28 | } |