Skip to content

Commit

Permalink
write_fig showfig works for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Sep 16, 2021
1 parent 2b2c4ae commit 8ab59ed
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ Suggests:
grid,
gridExtra
VignetteBuilder: knitr
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
Roxygen: list(markdown = TRUE)
38 changes: 26 additions & 12 deletions R/write_fig.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' The default type is pdf.
#' @inheritParams grDevices::svg
#' @inheritParams grDevices::png
#' @inheritParams showfig
#' @param show Boolean. Whether show file after finished writing?
#' @param use.cairo_pdf This parameter is for pdf type. whether to use `grDevices::cairo_pdf`?
#' `cairo_pdf` supports self defined font, but can not create multiple page pdf.
Expand All @@ -19,7 +20,7 @@
#' @importFrom grDevices svg tiff
#' @export
write_fig <- function (p, file = "Rplot.pdf", width = 10, height = 5,
devices = NULL, res = 300, show = TRUE, use.cairo_pdf = TRUE)
devices = NULL, res = 300, show = TRUE, use.cairo_pdf = TRUE, pdf.viewer = FALSE)
{
# open device for writing
dev_open <- function(file, width, height, res, use.cairo_pdf = FALSE) {
Expand Down Expand Up @@ -89,25 +90,38 @@ write_fig <- function (p, file = "Rplot.pdf", width = 10, height = 5,
}
}

#' showfig in external app
#' @param pdf.viewer boolean, if set true, pdf viewer will be used mandatorily.
#'
#' @export
showfig <- function(file) {
showfig <- function(file, pdf.viewer = FALSE) {
pdf_view <- function() {
cmd = sprintf('"%s" "%s"', app, file)
check_dir(dirname(file))
tryCatch({
status <- suppressWarnings(shell(cmd, intern = FALSE, wait = FALSE))
}, error = function(e) {
message(sprintf("[e] %s", e$message))
})
}

file_ext = file_ext(file)
app = ""
if (.Platform$OS.type == "windows")
app = '"C:/Program Files/RStudio/bin/sumatra/SumatraPDF.exe"'
if (.Platform$OS.type == "unix") app <- "evince"

# if linux system
is_server_pdf = file.exists("/usr/sbin/rstudio-server") && file_ext == "pdf"
if (file_ext %in% c("svg", "emf", "jpg") || is_server_pdf) {
file.show(file)
if (pdf.viewer) {
pdf_view()
} else {
cmd = sprintf('"%s "%s" "', app, file)
check_dir(dirname(file))
tryCatch({
status <- suppressWarnings(shell(cmd, intern = FALSE, wait = FALSE))
}, error = function(e) {
message(sprintf("[e] %s", e$message))
})
# if `is_wsl_rserver` is true, `file.show` will be called.
is_wsl_rserver = dir.exists("/mnt/c") &&
file.exists("/usr/sbin/rstudio-server") && file_ext == "pdf"
if (file_ext %in% c("svg", "emf", "jpg") || is_wsl_rserver) {
file.show(file)
} else {
pdf_view()
}
}
}
5 changes: 5 additions & 0 deletions man/examples/test-write_fig.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library(Ipaper)

write_fig({
plot(0)
})
14 changes: 14 additions & 0 deletions man/showfig.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions man/write_fig.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8ab59ed

Please sign in to comment.