Bangyou
Bangyou

Reputation: 9816

How to create a tar.gz file in R?

I would create a tar.gz file from R. But the tar function doesn't work me. This is my example code.

writeLines('aaaa', 'tmp.txt')
tar('tmp.tar.gz', 'tmp.txt', compression = 'gzip')

This code creates a tmp.tar.gz, but tmp.txt doesn't include in the gz file.

Are there any mistakes in my code?

Thanks for any suggestions.

I run this code in windows and linux:

#Windows platform
sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

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

loaded via a namespace (and not attached):
[1] tools_3.1.0

#Linux platform
sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)

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

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

other attached packages:
[1] weaana_0.1.0.3874  abind_1.4-0        reshape2_1.4       ncdf4cf_0.1.0.3698 ncdf4_1.10        
[6] stringr_0.6.2      My_0.1.0.4086     

loaded via a namespace (and not attached):
[1] Rcpp_0.11.1 plyr_1.8.1  tools_3.1.0

Upvotes: 10

Views: 6946

Answers (1)

gagolews
gagolews

Reputation: 13046

I guess you do not have set the "tar" environmental variable. The default argument of tar() called tar is set by a call to Sys.getenv("tar"). If it is an empty string, then R's internal tar is used (it doesn't work on my system either).

So, you may either Sys.setenv the "tar" variable, or provide the name of external tar command/path manually:

tar('tmp.tar.gz', 'tmp.txt', compression = 'gzip', tar="tar")

Upvotes: 14

Related Questions