Skip to content

Commit

Permalink
Do not open any plot devices during calculation of dendro_data #25
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Apr 15, 2016
1 parent 7723846 commit 28379ca
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 82 deletions.
6 changes: 3 additions & 3 deletions R/dendro_rpart.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ dendro_data.rpart <- function(model, uniform = FALSE, branch = 1, compress = FAL

if (compress & missing(nspace)) nspace <- branch
if (!compress) nspace <- -1L # means no compression
if(!interactive()) if (dev.cur() == 1L) dev.new() # not needed in R
# if(!interactive()) if (dev.cur() == 1L) dev.new() # not needed in R

parms <- list(uniform = uniform,
branch = branch,
nspace = nspace,
minbranch = minbranch)

## define the plot region
temp <- rpartco(x, parms)
temp <- rpartco(x, parms = parms)
xx <- temp$x
yy <- temp$y
# temp1 <- range(xx) + diff(range(xx)) * c(-margin, margin)
# temp2 <- range(yy) + diff(range(yy)) * c(-margin, margin)
# plot(temp1, temp2, type = "n", axes = FALSE, xlab = "", ylab = "", ...)
## Save information per device, once a new device is opened.
assign(paste0("device", dev.cur()), parms, envir = rpart_ggdendro_env)
# assign(paste0("device", dev.cur()), parms, envir = rpart_ggdendro_env)

# Draw a series of horseshoes or V's, left son, up, down to right son
# NA's in the vector cause lines() to "lift the pen"
Expand Down
42 changes: 22 additions & 20 deletions R/dendro_tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#


tree_ggdendro_env <- new.env()
# tree_ggdendro_env <- new.env()

#' Extract data from regression tree object for plotting using ggplot.
#'
Expand All @@ -43,14 +43,14 @@ dendro_data.tree <- function(model, type = c("proportional", "uniform"), ...){
type <- match.arg(type)
uniform <- type == "uniform"

dev <- dev.cur()
if (dev == 1L) dev <- 2L # as device will be opened.
# dev <- dev.cur()
# if (dev == 1L) dev <- 2L # as device will be opened.

assign(paste0("device", dev), uniform, envir = tree_ggdendro_env)
# assign(paste0("device", dev), uniform, envir = tree_ggdendro_env)

labels <- tree_labels(model, ...)
labels <- tree_labels(model, uniform = uniform, ...)
as.dendro(
segments = tree_segments(model, ...),
segments = tree_segments(model, uniform, ...),
labels = labels$labels,
leaf_labels = labels$leaf_labels,
class="tree"
Expand All @@ -66,9 +66,10 @@ dendro_data.tree <- function(model, type = c("proportional", "uniform"), ...){
#' @seealso \code{\link{ggdendrogram}}
#' @family tree functions
#' @author Code modified from original by Brian Ripley
tree_segments <- function(model, ...){
tree_segments <- function(model, uniform, ...){
if(missing(uniform)) stop("specify the uniform argument")
# Uses tree:::treeco to extract data frame of plot locations
xy <- treeco(model)
xy <- treeco(model, uniform = uniform)
n <- model$frame$n

# Lines copied from tree:::treepl
Expand All @@ -92,9 +93,9 @@ tree_segments <- function(model, ...){
#' @seealso \code{\link{ggdendrogram}}
#' @family tree functions
#' @author Code modified from original by Brian Ripley
tree_labels <- function(model, ...){
tree_labels <- function(model, uniform, ...){
# Uses tree:::treeco to extract data frame of plot locations
xy <- treeco(model)
xy <- treeco(model, uniform = uniform)
label <- model$frame$var
yval <- model$frame$yval
sleft <- model$frame$splits.cutleft
Expand Down Expand Up @@ -138,9 +139,9 @@ tree_labels <- function(model, ...){
#' @seealso \code{\link{ggdendrogram}}
#' @family tree functions
#' @author Code modified from original by Brian Ripley
get_data_tree_leaf_labels <- function(model, ...){
get_data_tree_leaf_labels <- function(model, uniform, ...){
# Uses tree:::treeco to extract data frame of plot locations
xy <- treeco(model)
xy <- treeco(model, uniform = uniform)
label <- model$frame$var
yval <- model$frame$yval
sleft <- model$frame$splits.cutleft
Expand All @@ -167,14 +168,15 @@ get_data_tree_leaf_labels <- function(model, ...){
#' @param tree tree object
#' @param uniform ???
#' @keywords internal
treeco <- function (tree, uniform)
{
if (missing(uniform)) {
pn <- paste0("device", dev.cur())
uniform <- if (exists(pn, envir = tree_ggdendro_env, inherits = FALSE))
get(pn, envir = tree_ggdendro_env, inherits = FALSE)
else FALSE
}
treeco <- function (tree, uniform) {
# if (missing(uniform)) {
# pn <- paste0("device", dev.cur())
# uniform <- if (exists(pn, envir = tree_ggdendro_env, inherits = FALSE))
# get(pn, envir = tree_ggdendro_env, inherits = FALSE)
# else FALSE
# }
if(missing(uniform)) stop("specify uniform argument")

frame <- tree$frame
node <- as.integer(row.names(frame))
depth <- tree.depth(node)
Expand Down
99 changes: 50 additions & 49 deletions R/rpart.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ labels.rpart <- function(object, digits = 4, minlength = 1L, pretty,

# rpart_ggdendro_env -------------------------------------------------------------------

rpart_ggdendro_env <- new.env()
# rpart_ggdendro_env <- new.env()


# rpart.branch ------------------------------------------------------------
Expand All @@ -139,13 +139,14 @@ rpart_ggdendro_env <- new.env()
## most likely this could simply default to branch = 1
rpart.branch <- function(x, y, node, branch)
{
if (missing(branch)) {
pn <- paste0("device", dev.cur())
if (!exists(pn, envir = rpart_ggdendro_env, inherits = FALSE))
stop("no information available on parameters from previous call to plot()")
parms <- get(pn, envir = rpart_ggdendro_env, inherits = FALSE)
branch <- parms$branch
}
# if (missing(branch)) {
# pn <- paste0("device", dev.cur())
# if (!exists(pn, envir = rpart_ggdendro_env, inherits = FALSE))
# stop("no information available on parameters from previous call to plot()")
# parms <- get(pn, envir = rpart_ggdendro_env, inherits = FALSE)
# branch <- parms$branch
# }
if(missing(branch)) branch <- 1

## Draw a series of horseshoes, left son, up, over, down to right son
## NA's in the vector cause lines() to "lift the pen"
Expand Down Expand Up @@ -179,12 +180,12 @@ tree.depth <- function (nodes)
#' @keywords internal
rpartco <- function(tree, parms)
{
if (missing(parms)) {
pn <- paste0("device", dev.cur())
if (!exists(pn, envir = rpart_ggdendro_env, inherits = FALSE))
stop("no information available on parameters from previous call to plot()")
parms <- get(pn, envir = rpart_ggdendro_env, inherits = FALSE)
}
# if (missing(parms)) {
# pn <- paste0("device", dev.cur())
# if (!exists(pn, envir = rpart_ggdendro_env, inherits = FALSE))
# stop("no information available on parameters from previous call to plot()")
# parms <- get(pn, envir = rpart_ggdendro_env, inherits = FALSE)
# }

frame <- tree$frame
node <- as.numeric(row.names(frame))
Expand Down Expand Up @@ -386,41 +387,41 @@ text.rpart <- function(x, splits = TRUE, label, FUN = text, all = FALSE,
ylevel = ylevels, digits = digits,
n = frame$n[leaves], use.n = use.n)

if (fancy) {
if (col2rgb(bg, alpha = TRUE)[4L, 1L] < 255) bg <- "white"
oval <- function(middlex, middley, a, b)
{
theta <- seq(0, 2 * pi, pi/30)
newx <- middlex + a * cos(theta)
newy <- middley + b * sin(theta)
polygon(newx, newy, border = TRUE, col = bg)
}

## FIXME: use rect()
rectangle <- function(middlex, middley, a, b)
{
newx <- middlex + c(a, a, -a, -a)
newy <- middley + c(b, -b, -b, b)
polygon(newx, newy, border = TRUE, col = bg)
}

## find maximum length of stat
maxlen <- max(string.bounding.box(stat)$columns) + 1L
maxht <- max(string.bounding.box(stat)$rows) + 1L

a.length <- if (fwidth < 1) fwidth * cxy[1L] * maxlen else fwidth * cxy[1L]

b.length <- if (fheight < 1) fheight * cxy[2L] * maxht else fheight * cxy[2L]

## create ovals and rectangles here
## sqrt(2) creates the smallest oval that fits around the
## best fitting rectangle
for (i in parent)
oval(xy$x[i], xy$y[i], sqrt(2) * a.length/2, sqrt(2) * b.length/2)
child <- match(node[frame$var == "<leaf>"], node)
for (i in child)
rectangle(xy$x[i], xy$y[i], a.length/2, b.length/2)
}
# if (fancy) {
# if (col2rgb(bg, alpha = TRUE)[4L, 1L] < 255) bg <- "white"
# oval <- function(middlex, middley, a, b)
# {
# theta <- seq(0, 2 * pi, pi/30)
# newx <- middlex + a * cos(theta)
# newy <- middley + b * sin(theta)
# polygon(newx, newy, border = TRUE, col = bg)
# }
#
# ## FIXME: use rect()
# rectangle <- function(middlex, middley, a, b)
# {
# newx <- middlex + c(a, a, -a, -a)
# newy <- middley + c(b, -b, -b, b)
# polygon(newx, newy, border = TRUE, col = bg)
# }
#
# ## find maximum length of stat
# maxlen <- max(string.bounding.box(stat)$columns) + 1L
# maxht <- max(string.bounding.box(stat)$rows) + 1L
#
# a.length <- if (fwidth < 1) fwidth * cxy[1L] * maxlen else fwidth * cxy[1L]
#
# b.length <- if (fheight < 1) fheight * cxy[2L] * maxht else fheight * cxy[2L]
#
# ## create ovals and rectangles here
# ## sqrt(2) creates the smallest oval that fits around the
# ## best fitting rectangle
# for (i in parent)
# oval(xy$x[i], xy$y[i], sqrt(2) * a.length/2, sqrt(2) * b.length/2)
# child <- match(node[frame$var == "<leaf>"], node)
# for (i in child)
# rectangle(xy$x[i], xy$y[i], a.length/2, b.length/2)
# }

##if FUN=text then adj=1 puts the split label to the left of the
## split rather than centered
Expand Down
17 changes: 10 additions & 7 deletions inst/doc/ggdendro.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion man/get_data_tree_leaf_labels.Rd

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

2 changes: 1 addition & 1 deletion man/tree_labels.Rd

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

2 changes: 1 addition & 1 deletion man/tree_segments.Rd

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

0 comments on commit 28379ca

Please sign in to comment.