From 6383757dcae95292fdfcabd16c8016f8cb4c0800 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Wed, 28 Oct 2020 21:37:28 +0000 Subject: [PATCH] [R] allow xgb.plot.importance() calls to fill a grid (#6294) --- R-package/R/xgb.plot.importance.R | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/R-package/R/xgb.plot.importance.R b/R-package/R/xgb.plot.importance.R index dd3dfb29e..ab0951463 100644 --- a/R-package/R/xgb.plot.importance.R +++ b/R-package/R/xgb.plot.importance.R @@ -99,8 +99,12 @@ xgb.plot.importance <- function(importance_matrix = NULL, top_n = NULL, measure } if (plot) { - op <- par(no.readonly = TRUE) - mar <- op$mar + original_mar <- par()$mar + + # reset margins so this function doesn't have side effects + on.exit({par(mar = original_mar)}) + + mar <- original_mar if (!is.null(left_margin)) mar[2] <- left_margin par(mar = mar) @@ -109,11 +113,6 @@ xgb.plot.importance <- function(importance_matrix = NULL, top_n = NULL, measure importance_matrix[rev(seq_len(nrow(importance_matrix))), barplot(Importance, horiz = TRUE, border = NA, cex.names = cex, names.arg = Feature, las = 1, ...)] - grid(NULL, NA) - # redraw over the grid - importance_matrix[rev(seq_len(nrow(importance_matrix))), - barplot(Importance, horiz = TRUE, border = NA, add = TRUE)] - par(op) } invisible(importance_matrix)