From 0b940ec8e6264ff3a6aec2960eb234c8c29d2ec4 Mon Sep 17 00:00:00 2001
From: David <david.dorchies@inrae.fr>
Date: Wed, 31 Jul 2024 09:57:04 +0200
Subject: [PATCH] fix(plot.GRiwrm): no crash with single node

Refs #153
---
 R/plot.GRiwrm.R | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/R/plot.GRiwrm.R b/R/plot.GRiwrm.R
index 1c2f2f8..8ac8eab 100644
--- a/R/plot.GRiwrm.R
+++ b/R/plot.GRiwrm.R
@@ -49,13 +49,17 @@ plot.GRiwrm <- function(x,
   x <- sortGRiwrm4plot(x)
   nodes <- unlist(sapply(unique(x$donor), plotGriwrmCluster, x = x, with_donors = with_donors))
   g2 <- x[!is.na(x$down),]
-  links <- paste(
-    sprintf("id_%1$s", g2$id),
-    "-->|",
-    round(g2$length, digits = 0),
-    "km|",
-    sprintf("id_%1$s", g2$down)
-  )
+  if (nrow(g2) > 0) {
+    links <- paste(
+      sprintf("id_%1$s", g2$id),
+      "-->|",
+      round(g2$length, digits = 0),
+      "km|",
+      sprintf("id_%1$s", g2$down)
+    )
+  } else {
+    links <- ""
+  }
   x$nodeclass <- sapply(x$id, getNodeClass, griwrm = x)
   node_class <- lapply(unique(x$nodeclass), function(nc) {
     x$id[x$nodeclass == nc]
-- 
GitLab