Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file is compressed in R

Tags:

r

What is the best way in R to determine if a file is or isn't compressed? Is there any particular function to check that? I am asking about something different than looking at filename extension e.g.

grepl("^.*(.gz|.bz2|.tar|.zip|.tgz|.gzip|.7z)[[:space:]]*$", filename)
like image 771
Katarzyna Wręczycka Avatar asked Apr 07 '15 13:04

Katarzyna Wręczycka


1 Answers

In R, do this:

filetype = summary( file('yourfile.gz') )$class

If it's gzipped, filetype will be gzfile


Note: you may also want to assign file to a variable and close the connection after

filetype <- function(path){
    f = file(path)
    ext = summary(f)$class
    close.connection(f)
    ext
}
like image 144
Omar Wagih Avatar answered Oct 13 '22 11:10

Omar Wagih