Skip to content

Instantly share code, notes, and snippets.

@kenjisato
Created February 2, 2020 08:33
Show Gist options
  • Save kenjisato/9b1384016465d666f2327c0fa9e1f714 to your computer and use it in GitHub Desktop.
Save kenjisato/9b1384016465d666f2327c0fa9e1f714 to your computer and use it in GitHub Desktop.
PDF/HTML graphics
include_graphics <- function(path, image_opts = NULL, pdf_opts = NULL) {
sys_call <- sys.calls()
produce_pdf <- sapply(c('exams2pdf', 'exams2nops'),
function(x) any(grepl(x, sapply(sys_call, deparse))))
if (any(produce_pdf)){
path_to_pdf <- paste0(tools::file_path_sans_ext(path), '.pdf')
if (file.exists(path_to_pdf)){
pdf_options <- if (!is.null(pdf_opts)) {
paste(paste(names(pdf_opts), pdf_opts, sep = '='), collapse = ', ')
}
cat('\\includegraphics[', pdf_options, ']{', path_to_pdf ,'}', sep = '')
}
} else {
alt <- if (!is.null(image_opts$alt)) image_opts$alt else ''
width <- if (!is.null(image_opts$width))
sprintf('{width=%s}', image_opts$width) else ''
cat(sprintf('![%s](%s)%s', alt, path, width))
}
}
```{r, echo=FALSE, results='asis'}
examtools::include_graphics('gbpusd.png',
pdf_opts = list(width = '0.6\\textwidth'),
image_opts = list(width = '12cm'))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment