Skip to content

Commit

Permalink
plot can output to pdf, png, svg!
Browse files Browse the repository at this point in the history
* NEW `plot` can   output to pdf, png, svg!
* NEW `umx_set_plot_format` can  pdf, png, svg output (in addition to DiagrammeR and graphviz)
  • Loading branch information
tbates committed Jan 24, 2021
1 parent 8bb2f4a commit c6bd074
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Depends:
Imports:
cowplot,
DiagrammeR,
DiagrammeRsvg,
rsvg,
ggplot2,
kableExtra,
knitr,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ export(xmu_twin_get_var_names)
export(xmu_twin_upgrade_selDvs2SelVars)
import(OpenMx)
importFrom(DiagrammeR,DiagrammeR)
importFrom(DiagrammeR,grViz)
importFrom(DiagrammeRsvg,export_svg)
importFrom(MASS,mvrnorm)
importFrom(MuMIn,Weights)
importFrom(cowplot,draw_label)
Expand Down Expand Up @@ -361,6 +363,8 @@ importFrom(methods,setClass)
importFrom(methods,slotNames)
importFrom(nlme,intervals)
importFrom(polycor,hetcor)
importFrom(rsvg,rsvg_pdf)
importFrom(rsvg,rsvg_png)
importFrom(stats,AIC)
importFrom(stats,C)
importFrom(stats,aggregate)
Expand Down
4 changes: 4 additions & 0 deletions R/build_run_modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
packageStartupMessage("For an overview type '?umx'")
}

# #' @importFrom Base::charToRaw
#' @importFrom DiagrammeR DiagrammeR grViz
#' @importFrom DiagrammeRsvg export_svg
#' @importFrom rsvg rsvg_png rsvg_pdf
#' @importFrom graphics plot abline
#' @importFrom methods as getSlots is slotNames setClass
# methods::setClass is called during build not package source code.
Expand Down
8 changes: 5 additions & 3 deletions R/misc_and_utility.R
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,15 @@ umx_set_plot_file_suffix <- function(umx.plot.suffix = NULL, silent = FALSE) {
#' old = umx_set_plot_format(silent = TRUE) # store current value
#' umx_set_plot_format("graphviz")
#' umx_set_plot_format("DiagrammeR")
#' umx_set_plot_format("png")
#' umx_set_plot_format("pdf")
#' umx_set_plot_format(old) # reinstate
umx_set_plot_format <- function(umx.plot.format = NULL, silent = FALSE) {
if(is.null(umx.plot.format)) {
if(!silent){
message("Current format is",
message("Current format is ",
omxQuotes(getOption("umx.plot.format")),
". Valid options are 'graphviz' or 'DiagrammeR'. Use TRUE to toggle"
". Valid options are 'DiagrammeR', 'pdf', 'png', 'svg', or 'graphviz'. TRUE toggles between DiagrammeR and graphviz"
)
}
invisible(getOption("umx.plot.format"))
Expand All @@ -490,7 +492,7 @@ umx_set_plot_format <- function(umx.plot.format = NULL, silent = FALSE) {
umx.plot.format = "graphviz"
}
} else {
umx_check(umx.plot.format %in% c("graphviz", "DiagrammeR"), "stop", "valid options are 'graphviz' or 'DiagrammeR'. Use TRUE to toggle)")
umx_check(umx.plot.format %in% c("DiagrammeR", "pdf", "png", 'svg', "graphviz"), "stop", "valid options are 'DiagrammeR', 'pdf', 'png', 'svg', 'graphviz'. TRUE toggles between DiagrammeR and graphviz)")
}
options("umx.plot.format" = umx.plot.format)
}
Expand Down
28 changes: 22 additions & 6 deletions R/xmu.R
Original file line number Diff line number Diff line change
Expand Up @@ -1810,17 +1810,33 @@ xmu_dot_maker <- function(model, file, digraph, strip_zero= TRUE){

if(!is.na(file)){
if(file == "name"){
# maybe:
if (umx_set_plot_format(silent = TRUE) == "DiagrammeR"){
if (umx_set_plot_format(silent = TRUE) %in% c("DiagrammeR", "pdf", "png")){
# tempfile
file = tempfile(fileext = paste0(".", umx_set_plot_file_suffix(silent = TRUE)) )
} else { # leave in the users current directory?
} else {
# leave in the users current directory for graphviz
file = paste0(model$name, ".", umx_set_plot_file_suffix(silent = TRUE))
}
}
cat(digraph, file = file) # write to file
if(umx_set_plot_format(silent = TRUE) == "DiagrammeR"){
print(DiagrammeR::DiagrammeR(diagram = file, type = "grViz"))
cat(digraph, file = file) # write dot file
if(umx_set_plot_format(silent=TRUE) == "DiagrammeR"){
print(DiagrammeR(diagram = file, type = "grViz"))
}else if(umx_set_plot_format(silent=TRUE) %in% c("pdf", "svg", "png")){
tmp = export_svg(grViz(file)) #export as SVG
raw = charToRaw(tmp) # flatten
if(umx_set_plot_format(silent=TRUE) == "pdf"){
fileName = paste0(model$name, ".pdf")
rsvg_pdf(raw, fileName) # save as pdf
} else if(umx_set_plot_format(silent=TRUE) == "png"){
fileName = paste0(model$name, ".png")
rsvg_png(raw, fileName) # save as png in current working directory
} else if(umx_set_plot_format(silent=TRUE) == "svg"){
fileName = paste0(model$name, ".svg")
cat(tmp, file = fileName)
}
umx_open(fileName)
} else {
# graphviz
if(umx_check_OS("OSX")){
umx_open(file);
} else if(umx_check_OS("Windows")){
Expand Down
2 changes: 2 additions & 0 deletions man/umx_set_plot_format.Rd

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

0 comments on commit c6bd074

Please sign in to comment.