Skip to content

Commit

Permalink
plot_tt images seem to work
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 21, 2024
1 parent 2d216b7 commit 41df6eb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 57 deletions.
5 changes: 4 additions & 1 deletion R/plot_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ plot_tt_lazy <- function(x,
cell <- sprintf(cell, height, path)

} else if (meta(x)$output == "html") {
cell <- '<img src="%s" style="height: %sem;">'
cell <- ifelse(
grepl("^http", trimws(path)),
'<img src="%s" style="height: %sem;">',
'<img src="./%s" style="height: %sem;">')
cell <- sprintf(cell, path, height)

} else if (meta(x)$output == "markdown") {
Expand Down
104 changes: 48 additions & 56 deletions R/save_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,86 +43,78 @@ save_tt <- function(x, output, overwrite = FALSE) {
file_ext <- tools::file_ext(output)

output_format <- switch(file_ext,
"png" = "html",
"html" = "html",
"pdf" = "latex",
"tex" = "latex",
"md" = "markdown",
"Rmd" = "markdown",
"qmd" = "markdown",
"txt" = "markdown",
"docx" = "markdown",
stop("The supported file extensions are: .png, .html, .pdf, .tex, and .md.", call. = FALSE))
"png" = "html",
"html" = "html",
"pdf" = "latex",
"tex" = "latex",
"md" = "markdown",
"Rmd" = "markdown",
"qmd" = "markdown",
"txt" = "markdown",
"docx" = "markdown",
stop("The supported file extensions are: .png, .html, .pdf, .tex, and .md.", call. = FALSE))

# evaluate styles at the very end of the pipeline, just before writing
x <- build_tt(x, output = output_format)

if (file_ext %in% c("html", "tex", "md", "Rmd", "qmd", "txt")) {
write(x, file = output)

} else if (file_ext == "png") {
assert_dependency("webshot2")
# this doesn't work in tempdir() for some reason.
# probably webshot2's fault. we need to build in `output`
id <- get_id()
dir.create(id)
img <- meta(x)$path_image
if (!is.null(img)) {
for (a in img) {
if (!grepl("http", a)) {
file.copy(a, id)
}
}
}
f <- paste0(id, ".html")
write(x, file = f)
webshot2::webshot(f,
file = output,
selector = "body > div > table",
zoom = 4,
quiet = TRUE)
unlink(f, force = TRUE)
unlink(id, force = TRUE, recursive = TRUE)
} else if (file_ext == "png") {
assert_dependency("webshot2")
# this doesn't work in tempdir() for some reason.
# probably webshot2's fault. we need to build in `output`
tmp <- file.path(dirname(output), paste0(get_id(), ".html"))
write(x, file = tmp)
webshot2::webshot(tmp,
file = output,
selector = "body > div > table",
zoom = 4,
quiet = TRUE)
unlink(tmp)

} else if (file_ext == "pdf") {
d <- ttempdir()
assert_dependency("tinytex")
# \documentclass{standalone} does not support \begin{table}
tmp <- strsplit(x, "\\n")[[1]]
tmp <- tmp[!grepl("\\begin{table}", tmp, fixed = TRUE)]
tmp <- tmp[!grepl("\\end{table}", tmp, fixed = TRUE)]
tmp <- paste(tmp, collapse = "\n")
tmp <- sprintf("
\\documentclass{standalone}
\\usepackage{tabularray}
\\usepackage{graphicx}
\\usepackage{float}
\\usepackage{codehigh}
\\usepackage[normalem]{ulem}
\\UseTblrLibrary{booktabs}
\\newcommand{\\tinytableTabularrayUnderline}[1]{\\underline{#1}}
\\newcommand{\\tinytableTabularrayStrikeout}[1]{\\sout{#1}}
\\NewTableCommand{\\tinytableDefineColor}[3]{\\definecolor{#1}{#2}{#3}}
\\begin{document}
%s
\\end{document}",
tmp)
f <- file.path(d, "index.tex")
write(tmp, f)
tmp <- sprintf(latex_standalone, tmp)
# tinytex is fiddly with file paths, so we need to hack
# it by changing the working directory
wd <- getwd()
setwd(d)
setwd(dirname(output))
f <- paste0(get_id(), ".tex")
write(tmp, f)
tinytex::xelatex(f, pdf_file = output)
unlink(f)
setwd(wd)

} else if (file_ext == "docx") {
assert_dependency("pandoc")
pandoc::pandoc_convert(text = x, to = "docx", output = output)
}
} else if (file_ext == "docx") {
assert_dependency("pandoc")
pandoc::pandoc_convert(text = x, to = "docx", output = output)
}

return(invisible(TRUE))

}




latex_standalone <- "
\\documentclass{standalone}
\\usepackage{tabularray}
\\usepackage{graphicx}
\\usepackage{float}
\\usepackage{codehigh}
\\usepackage[normalem]{ulem}
\\UseTblrLibrary{booktabs}
\\newcommand{\\tinytableTabularrayUnderline}[1]{\\underline{#1}}
\\newcommand{\\tinytableTabularrayStrikeout}[1]{\\sout{#1}}
\\NewTableCommand{\\tinytableDefineColor}[3]{\\definecolor{#1}{#2}{#3}}
\\begin{document}
%s
\\end{document}
"
Binary file modified man/figures/spider.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/figures/spider_long.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 41df6eb

Please sign in to comment.