From 545969faab81681981d1e76b1c846309d807ae40 Mon Sep 17 00:00:00 2001 From: david-cortes Date: Sat, 14 Dec 2024 17:55:04 +0100 Subject: [PATCH 1/3] run diagrammer examples --- R-package/DESCRIPTION | 3 +++ R-package/R/xgb.plot.multi.trees.R | 13 ++++--------- R-package/R/xgb.plot.tree.R | 12 ++++-------- R-package/man/xgb.plot.multi.trees.Rd | 13 ++++--------- R-package/man/xgb.plot.tree.Rd | 12 ++++-------- 5 files changed, 19 insertions(+), 34 deletions(-) diff --git a/R-package/DESCRIPTION b/R-package/DESCRIPTION index d1151ff8b874..d718bc2f72ce 100644 --- a/R-package/DESCRIPTION +++ b/R-package/DESCRIPTION @@ -51,6 +51,9 @@ Suggests: rmarkdown, ggplot2 (>= 1.0.1), DiagrammeR (>= 0.9.0), + DiagrammeRsvg, + rsvg, + htmlwidgets, Ckmeans.1d.dp (>= 3.3.1), vcd (>= 1.3), testthat, diff --git a/R-package/R/xgb.plot.multi.trees.R b/R-package/R/xgb.plot.multi.trees.R index 1c57dd84babd..56f766afe410 100644 --- a/R-package/R/xgb.plot.multi.trees.R +++ b/R-package/R/xgb.plot.multi.trees.R @@ -50,17 +50,12 @@ #' p <- xgb.plot.multi.trees(model = bst, features_keep = 3) #' print(p) #' -#' \dontrun{ #' # Below is an example of how to save this plot to a file. -#' # Note that for export_graph() to work, the {DiagrammeRsvg} and {rsvg} packages -#' # must also be installed. -#' -#' library(DiagrammeR) -#' -#' gr <- xgb.plot.multi.trees(model = bst, features_keep = 3, render = FALSE) -#' export_graph(gr, "tree.pdf", width = 1500, height = 600) +#' if (require("DiagrammeR") && require("DiagrammeRsvg") && require("rsvg")) { +#' fname <- file.path(tempdir(), "tree.pdf") +#' gr <- xgb.plot.multi.trees(bst, features_keep = 3, render = FALSE) +#' export_graph(gr, fname, width = 1500, height = 600) #' } -#' #' @export xgb.plot.multi.trees <- function(model, features_keep = 5, plot_width = NULL, plot_height = NULL, render = TRUE, ...) { diff --git a/R-package/R/xgb.plot.tree.R b/R-package/R/xgb.plot.tree.R index f18e17a9e1fe..97d21a318dd7 100644 --- a/R-package/R/xgb.plot.tree.R +++ b/R-package/R/xgb.plot.tree.R @@ -53,16 +53,12 @@ #' # plot the first tree #' xgb.plot.tree(model = bst, tree_idx = 1) #' -#' -#' \dontrun{ #' # Below is an example of how to save this plot to a file. -#' -#' library(DiagrammeR) -#' -#' gr <- xgb.plot.tree(model = bst, tree_idx = 1) -#' htmlwidgets::saveWidget(gr, 'plot.html') +#' if (require("DiagrammeR") && require("htmlwidgets")) { +#' fname <- file.path(tempdir(), "plot.html'") +#' gr <- xgb.plot.tree(bst, tree_idx = 1) +#' htmlwidgets::saveWidget(gr, fname) #' } -#' #' @export xgb.plot.tree <- function(model, tree_idx = 1, diff --git a/R-package/man/xgb.plot.multi.trees.Rd b/R-package/man/xgb.plot.multi.trees.Rd index 989096e60e42..398269e32679 100644 --- a/R-package/man/xgb.plot.multi.trees.Rd +++ b/R-package/man/xgb.plot.multi.trees.Rd @@ -84,15 +84,10 @@ bst <- xgb.train( p <- xgb.plot.multi.trees(model = bst, features_keep = 3) print(p) -\dontrun{ # Below is an example of how to save this plot to a file. -# Note that for export_graph() to work, the {DiagrammeRsvg} and {rsvg} packages -# must also be installed. - -library(DiagrammeR) - -gr <- xgb.plot.multi.trees(model = bst, features_keep = 3, render = FALSE) -export_graph(gr, "tree.pdf", width = 1500, height = 600) +if (require("DiagrammeR") && require("DiagrammeRsvg") && require("rsvg")) { + fname <- file.path(tempdir(), "tree.pdf") + gr <- xgb.plot.multi.trees(bst, features_keep = 3, render = FALSE) + export_graph(gr, fname, width = 1500, height = 600) } - } diff --git a/R-package/man/xgb.plot.tree.Rd b/R-package/man/xgb.plot.tree.Rd index ebcc8603fe98..1d0949d9effc 100644 --- a/R-package/man/xgb.plot.tree.Rd +++ b/R-package/man/xgb.plot.tree.Rd @@ -80,14 +80,10 @@ bst <- xgb.train( # plot the first tree xgb.plot.tree(model = bst, tree_idx = 1) - -\dontrun{ # Below is an example of how to save this plot to a file. - -library(DiagrammeR) - -gr <- xgb.plot.tree(model = bst, tree_idx = 1) -htmlwidgets::saveWidget(gr, 'plot.html') +if (require("DiagrammeR") && require("htmlwidgets")) { + fname <- file.path(tempdir(), "plot.html'") + gr <- xgb.plot.tree(bst, tree_idx = 1) + htmlwidgets::saveWidget(gr, fname) } - } From 40a8a0a95e16f552292899d85cde466f7c6c5965 Mon Sep 17 00:00:00 2001 From: david-cortes Date: Sat, 14 Dec 2024 18:13:04 +0100 Subject: [PATCH 2/3] install dependencies --- R-package/tests/helper_scripts/install_deps.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R-package/tests/helper_scripts/install_deps.R b/R-package/tests/helper_scripts/install_deps.R index 7a621798ab62..757475541fd4 100644 --- a/R-package/tests/helper_scripts/install_deps.R +++ b/R-package/tests/helper_scripts/install_deps.R @@ -13,6 +13,9 @@ pkgs <- c( "rmarkdown", "ggplot2", "DiagrammeR", + "DiagrammeRsvg", + "rsvg", + "htmlwidgets", "Ckmeans.1d.dp", "vcd", "lintr", From 0c242d6e916915813ecc6f586d54c2c39f5f3bd5 Mon Sep 17 00:00:00 2001 From: david-cortes Date: Sat, 14 Dec 2024 18:40:07 +0100 Subject: [PATCH 3/3] install system dependencies for rsvg --- .github/workflows/r_tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/r_tests.yml b/.github/workflows/r_tests.yml index fc0245f5752e..b3d5ad5b1f03 100644 --- a/.github/workflows/r_tests.yml +++ b/.github/workflows/r_tests.yml @@ -34,7 +34,7 @@ jobs: - name: Install system dependencies run: | sudo apt update - sudo apt install libcurl4-openssl-dev libssl-dev libssh2-1-dev libgit2-dev libglpk-dev libxml2-dev libharfbuzz-dev libfribidi-dev + sudo apt install libcurl4-openssl-dev libssl-dev libssh2-1-dev libgit2-dev libglpk-dev libxml2-dev libharfbuzz-dev libfribidi-dev librsvg2-dev librsvg2-2 if: matrix.os == 'ubuntu-latest' - uses: actions/checkout@v4 with: @@ -76,7 +76,7 @@ jobs: run: | # Must run before checkout to have the latest git installed. # No need to add pandoc, the container has it figured out. - apt update && apt install libcurl4-openssl-dev libssl-dev libssh2-1-dev libgit2-dev libglpk-dev libxml2-dev libharfbuzz-dev libfribidi-dev git -y + apt update && apt install libcurl4-openssl-dev libssl-dev libssh2-1-dev libgit2-dev libglpk-dev libxml2-dev libharfbuzz-dev libfribidi-dev git librsvg2-dev librsvg2-2 -y - name: Trust git cloning project sources run: | git config --global --add safe.directory "${GITHUB_WORKSPACE}"