Skip to content

Instantly share code, notes, and snippets.

@kenjisato
Created March 1, 2021 07:53
Show Gist options
  • Save kenjisato/419c6c555b319870cfbe8c8139882b1d to your computer and use it in GitHub Desktop.
Save kenjisato/419c6c555b319870cfbe8c8139882b1d to your computer and use it in GitHub Desktop.
#' Include an image not produced in Rmd/Rnw file.
#'
#' @param image_file character. image file name. Typically, PNG JPEG or SVG file.
#' @param html_opts list. list of \code{alt} and \code{width}.
#' Used as alt/width attributes for the image tag.
#' @param latex_opts list. list of parameters passed to \\includegraphics[]{image_file}
#'
#' @return LaTeX/Markdown code embedded in Rmd/Rnw file.
#' @export
include_graphics <- function(image_file, html_opts = NULL, latex_opts = NULL) {
if (length(image_file) > 1) {
message("include_graphis supports a single file only. The first item will be used.")
image_file = image_file[[1]]
}
sys_call <- sys.calls()
sys_call <- sapply(sys_call, deparse)
use_knitr <- any(grepl('knitr::knit', sys_call))
use_pandoc <- any(grepl('pandocwrite', sys_call))
to_pdf <- exams::match_exams_device() == 'pdf'
converter <- exams:::.exams_internal$xexams_call[[1]]$converter
if (identical(converter, 'tex2image')) {
stop('converter = "tex2image" is currently not supported.')
}
pdf_file <- paste0(tools::file_path_sans_ext(image_file), '.pdf')
if (to_pdf && file.exists(pdf_file)) {
image_file <- tools::file_path_sans_ext(image_file)
}
# Options
# ==========
# latex_opts
# ----------
# (1) Rnw -> LaTeX -> PDF
# (2) Rmd -> MD -> (Pandoc) -> LaTeX -> PDF
# (3) Rnw -> LaTeX -> (Pandoc) -> HTML
#
# html_opts
# ---------
# (4) Rnw -> LaTeX -> (tth/ttm) -> HTML
# (5) Rmd -> MD -> (Pandoc) -> HTML
#
if (!is.null(latex_opts)) {
latex_before <- latex_opts$before
latex_after <- latex_opts$after
latex_opts$before <- latex_opts$after <- NULL
latex_options <- paste(paste(names(latex_opts), latex_opts, sep = '='), collapse = ', ')
} else {
latex_before <- latex_after <- NULL
latex_options <- NULL
}
if (!is.null(html_opts)) {
html_before <- html_opts$before
html_after <- html_opts$after
html_opts$before <- html_opts$after <- NULL
} else {
html_before <- html_after <- NULL
}
# ============================================
# (1) Rnw -> LaTeX -> PDF
# (2) Rmd -> MD -> (Pandoc) -> LaTeX -> PDF
# ============================================
if (to_pdf){
out <- paste('\\includegraphics[', latex_options, ']{', image_file ,'}', sep = '')
out <- paste(latex_before, out, latex_after, sep = '\n')
if (use_knitr) {
return(structure(out, class = c('knit_asis')))
} else {
cat(out)
return(invisible())
}
}
# ===========================================
# (3) Rnw -> LaTeX -> (Pandoc) -> HTML
# ===========================================
if (!use_knitr && (identical(converter, 'pandoc') || use_pandoc)){
out <- paste('\\includegraphics[', latex_options, ']{', image_file ,'}', sep = '')
out <- paste(latex_before, out, latex_after, sep = '\n')
cat(out)
return(invisible())
}
# ===========================================
# (4) Rnw -> LaTeX -> (tth/ttm) -> HTML
# (5) Rmd -> MD -> (Pandoc) -> HTML
# ===========================================
alt <- if(!is.null(html_opts$alt)) sprintf('alt="%s"', html_opts$alt) else ''
width <- if(!is.null(html_opts$width)) sprintf('width="%s"', html_opts$width) else ''
out <- sprintf('<img src="%s" %s %s\ />', image_file, alt, width)
out <- paste(html_before, out, html_after, sep = '\n')
# (4)
if (!use_knitr && (is.null(converter) || converter %in% c('ttm', 'tth'))) {
out <- paste('\\begin{html}', out, '\\end{html}', sep = '\n')
cat(out)
return(invisible())
}
# (5)
if (use_knitr) {
return(structure(out, class = c('knit_asis')))
}
# ===========================================
# (?) Don't know what to do (yet).
# ===========================================
cat("[[ Image Removed ]]")
return(invisible())
}
```{r, echo = FALSE, results = "hide"}
include_supplement("Rlogo.png",
dir = find.package("exams"), recursive = TRUE)
```
Question
========
What does the following logo stand for?
```{r, echo=FALSE}
include_graphics("Rlogo.png",
html_opts = list(before = "<div style = 'text-align: center;'>",
after = "</div>"),
latex_opts = list(before = "\\begin{center}", after = "\\end{center}"))
```
Answerlist
----------
* Programming language
* Letter in the alphabet
* Technology company
* Search engine
* Online retailer
* Pirate word
Solution
========
```{r, echo=FALSE}
include_graphics("Rlogo.png",
html_opts = list(width = "50%", align = "center",
before = "<div style = 'text-align: center;'>",
after = "</div>"),
latex_opts = list(width = "0.5\\linewidth",
before = "\\begin{center}", after = "\\end{center}"))
```
The logo stands for the R system for statistical computing and graphics (<http://www.R-project.org/>).
Answerlist
----------
* True. R is a programming language.
* False. Although R is (also) a letter in the alphabet, this is not what the logo represents.
* False. R is an open-source project, not a company.
* False. R is not a search engine.
* False. The R project does not sell anything.
* False. This is usually spelt _Arrr!_.
Meta-information
================
exname: R logo
extype: schoice
exsolution: 100000
exshuffle: 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment