Skip to content
Prev 46148 / 63461 Next

relist() is broken when the skeleton is a list with empty list elements

Hi,
On 06/07/2013 03:08 PM, Herv? Pag?s wrote:
This bug has actually been noticed. Just found out it was already
reported by Michael Lawrence 2 years ago:

 
https://stat.ethz.ch/pipermail/r-devel/attachments/20110811/4bbaa84f/attachment.pl

Any chance it can be fixed?

Below is the patch against current R-devel if that helps (it fixes
relist.list and relist.matrix which are both broken in the same way).

Thanks,
H.


hpages at thinkpad:~/svn/R/R-devel$ svn diff
Index: src/library/utils/R/relist.R
===================================================================
--- src/library/utils/R/relist.R	(revision 63132)
+++ src/library/utils/R/relist.R	(working copy)
@@ -119,13 +119,13 @@

  relist.list <- function(flesh, skeleton=attr(flesh, "skeleton"))
  {
-    ind <- 1L
+    offset <- 0L
      result <- skeleton
      for (i in seq_along(skeleton)) {
  	size <- length(unlist(result[[i]]))
  	result[[i]] <-
-	    relist(flesh[ind:(ind + size - 1L)], result[[i]])
-	ind <- ind + size
+	    relist(flesh[seq_len(size) + offset], result[[i]])
+	offset <- offset + size
      }
      result
  }
@@ -139,13 +139,13 @@
      n <- nrow(skeleton)
      m <- ncol(skeleton)
      result <- skeleton
-    ind <- 1L
-    for (j in 1L:m)
-	for (i in 1L:n) {
+    offset <- 0L
+    for (j in seq_len(m))
+	for (i in seq_len(n)) {
  	    size <- length(unlist(skeleton[[i, j]]))
-	    result[[i, j]] <- relist(flesh[ind:(ind + size - 1)],
+	    result[[i, j]] <- relist(flesh[seq_len(size) + offset],
  				     skeleton[[i, j]])
-	    ind <- ind + size
+	    offset <- offset + size
  	}
      result
  }