Bug 16653

Summary: saveRDS fails with compress = "gzip"
Product: R Reporter: Alec Stephenson <alec_stephenson>
Component: I/OAssignee: R-core <R-core>
Status: CLOSED FIXED    
Severity: minor CC: maechler, thosjleeper
Priority: P5    
Version: R-devel (trunk)   
Hardware: x86_64/x64/amd64 (64-bit)   
OS: Windows 64-bit   

Description Alec Stephenson 2016-01-04 04:23:49 UTC
Contrary to the documentation, saveRDS fails with compress = "gzip" as there is no if (identical(compress, "gzip")) condition in the R code.

> xx <- 1:5
> saveRDS(xx, file = "xx.rds", compress = "gzip")
Error in if (compress) gzfile(file, mode) else file(file, mode) : 
  argument is not interpretable as logical


> version
               _                                                 
platform       x86_64-w64-mingw32                                
arch           x86_64                                            
os             mingw32                                           
system         x86_64, mingw32                                   
status         Under development (unstable)                      
major          3                                                 
minor          3.0                                               
year           2015                                              
month          12                                                
day            22                                                
svn rev        69809                                             
language       R                                                 
version.string R Under development (unstable) (2015-12-22 r69809)
nickname       Unsuffered Consequences
Comment 1 Thomas J. Leeper 2016-02-02 22:24:35 UTC
I can confirm this is still present in r70069:

> xx <- 1:5
> saveRDS(xx, file = "xx.rds", compress = TRUE)
> saveRDS(xx, file = "xx.rds", compress = FALSE)
> saveRDS(xx, file = "xx.rds", compress = "bzip2")
> saveRDS(xx, file = "xx.rds", compress = "xz")
> saveRDS(xx, file = "xx.rds", compress = "gzip")
Error in if (compress) gzfile(file, mode) else file(file, mode) : 
  argument is not interpretable as logical

The problem is that the final `else if()` construction needs to accommodate the possibly character argument. Here's the current problem:

        con <- if (identical(compress, "bzip2")) 
            bzfile(file, mode)
        else if (identical(compress, "xz")) 
            xzfile(file, mode)
        else if (compress) 
            gzfile(file, mode)
        else file(file, mode)
        on.exit(close(con))

And here's the fix:

        con <- if (identical(compress, "bzip2")) 
            bzfile(file, mode)
        else if (identical(compress, "xz")) 
            xzfile(file, mode)
        else if (identical(compress, "gzip") || compress)
            gzfile(file, mode)
        else file(file, mode)
        on.exit(close(con))
Comment 2 Martin Maechler 2016-02-03 09:06:07 UTC
Thank you, Alec and Thomas,

this bug report has been overlooked or "forgotten".
Now fixed in R-devel  and soon 'R 3.2.3 patched".