Gunzip file stream in R?

I am trying to create an R API for StackOverflow . The output is gzipped. For example:

readLines("http://api.stackoverflow.com/0.9/stats/", warn=F) [1] "\037‹\b" [2] "\030\002úØÛy°óé½\036„iµXäË–[<üt—Zu[\\VmÎHî=ÜÛݹ×ýz'Í.äûû÷>ý´\a\177Ýh÷\017îÝÛÙwßÚáÿþ«¼þý\027ÅrÝæÔlgüÀëA±\017›ìŽï{M¤û.\020\037 Ë\"¿'\006³ì\032„Úß9¸ÿ`¼ç÷³*~ÿKêˆð¡\006v¦ð²ýô£ ñà ì+ôU _\026滽 ]êt¼·?ÞûÈ4ù%\016~S0^>àe¶ÀG\037½n³éÛôKê缬®‚\016Êê¢úý×u‰fó¶]=º{·aÎšŽ—y{·©î\026‹‹»h5^-/‚W1 |9[UŲõ^§ Ç" [3] ":¬´¿1M\177ð\"0íö¹ñ…YÞLëbÕ*!~â\027\036§çU ®êê¢ÎˆµhòýæÅ´Zn\036S¶Z•ùv[§óm´î " [4] "Í™t˪^d¥£·üÂ?¾ÿ\033'¿$ù\177" 

Is there a good way to destroy this in R without writing the output to a file, without destroying it, and without reading?

+6
r zip gunzip
source share
3 answers

You can do:

 conn <- gzcon(url("http://api.stackoverflow.com/0.9/stats/")) data <- readLines(conn) 
+11
source share

Try:

 p <- gzcon(url("http://api.stackoverflow.com/0.9/stats/")) readLines(p) 
+5
source share

Ideally, we should tell the server that we can process gzipped content, find out from the HTTP headers that the content is actually gzip encoded, and then unpack it only if it exists. The Rcurl library can do this:

 library(Rcurl) getURL("http://api.stackoverflow.com/0.9/stats/", .opts=list(encoding="identity,gzip") 
+4
source share

All Articles