3

I have a password protected zip archive containing one SPSS system data file (*.sav). And I want to unpack it and read its content into R. From what I googled, Hmisc::getZip seems to be the best bet. And I've tried some different approaches.

First try was to just run the function, hoping that it would spit out the *.sav file in my getwd() location.

getZip(url = '/path/to/data.zip', 
  password = 'foo'
)

But this command returns:

"unzip -p -P foo data.zip" 
class 
"pipe" 
mode 
"r" 
text 
"text" 
opened 
"closed" 
can read 
"yes" 
can write 
"yes" 

And after reading the help file, the getZip() function seems to return a file O/I pipe. So my second try was to use getZip() inside read.spss(). Like this:

data <- read.spss(getZip(url = '/path/to/data.zip', 
  password = 'foo')
)

But no love:

Error in read.spss(getZip(url = '/path/to/data.zip', password = 'foo')) : 
  unable to open file: 'No such file or directory'

When I take the command from my first try "unzip -p -P foo data.zip" and run it (with an added "> data.sav") from the command line I get the SPSS file. So something is working. My third try was to use the connection:

file_connection <- getZip(url = '/path/to/data.zip', password = 'foo')
open(file_connection, 'rb')
data <- readBin(file_connection, raw())
close(file_connection)

I was hoping to save the data object to an *.sav file and read it back into R with the read.spss() function. But the data object is not an SPSS file:

> data
[1] 24

So... How should I successfully unpack and read the *.sav file from the password protected zip archive into R?

> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.4 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                 
 [3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8       
 [5] LC_MONETARY=sv_SE.UTF-8       LC_MESSAGES=en_US.UTF-8      
 [7] LC_PAPER=sv_SE.UTF-8          LC_NAME=sv_SE.UTF-8          
 [9] LC_ADDRESS=sv_SE.UTF-8        LC_TELEPHONE=sv_SE.UTF-8     
[11] LC_MEASUREMENT=sv_SE.UTF-8    LC_IDENTIFICATION=sv_SE.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gmailr_0.7.1 rj_2.0.5-1  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5         Formula_1.2-1       cluster_2.0.4      
 [4] magrittr_1.5        splines_3.3.0       munsell_0.4.3      
 [7] rj.gd_2.0.0-1       colorspace_1.2-6    lattice_0.20-33    
[10] R6_2.1.2            httr_1.1.0          plyr_1.8.3         
[13] tools_3.3.0         nnet_7.3-12         grid_3.3.0         
[16] data.table_1.9.6    gtable_0.2.0        latticeExtra_0.6-28
[19] openssl_0.9.3       survival_2.39-4     Matrix_1.2-6       
[22] gridExtra_2.2.1     RColorBrewer_1.1-2  ggplot2_2.1.0      
[25] base64enc_0.1-3     acepack_1.3-3.3     rpart_4.1-10       
[28] curl_0.9.7          scales_0.4.0        Hmisc_3.17-4       
[31] jsonlite_0.9.20     chron_2.3-47        foreign_0.8-66  

EDIT: OK. So I have a (not so elegant) workaround. I'm using a system() call with the wait = TRUE argument to unpack the file. Like this:

system(command = paste0('unzip -P foo ', data.zip), 
  wait = TRUE
)

Then I can read the *.sav file into R. But I still don't understand how to use Hmisc::getZip()

rafa.pereira
  • 13,251
  • 6
  • 71
  • 109
habana
  • 317
  • 1
  • 11

0 Answers0