From 9dc5721c58471b1c427fc838f386a47e195f1d6a Mon Sep 17 00:00:00 2001 From: alexg9010 Date: Thu, 17 Jan 2019 12:54:19 +0100 Subject: [PATCH] fread.gzipped: update function to check compression based on file content and allow to decompress .bgz files --- R/backbone.R | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/R/backbone.R b/R/backbone.R index 82a5678b..91187818 100644 --- a/R/backbone.R +++ b/R/backbone.R @@ -10,20 +10,26 @@ fread.gzipped<-function(filepath,...){ # decompress first, fread can't read gzipped files - if (R.utils::isGzipped(filepath)){ + if (R.utils::isGzipped(filepath, method = "content")){ if(.Platform$OS.type == "unix") { - filepath=paste("gunzip -c",filepath) + # being on unix we can pass comand + cmd = paste("gunzip -c",filepath) + ## Read in the file + fread(cmd = cmd,...) } else { + # on windows we have to decompress first + ext = if( endsWith(filepath,".bgz") ) "bgz" else "gz" filepath <- R.utils::gunzip(filepath,temporary = FALSE, overwrite = TRUE, - remove = FALSE) + remove = FALSE, ext = ext, FUN = gzfile) + ## Read in the file + fread(file = filepath,...) } } - ## Read in the file - fread(filepath,...) + } # reads a table in a fast way to a dataframe