Чтение данных .txt из одного файла архива .zip работает очень хорошо.
testDir <- paste0(getwd(), "/xllnzoiu")
dir.create(testDir)
write.table(mtcars, file=paste0(testDir, "/test.txt"))
zip(paste0(testDir, "/testZip"), paste0(testDir, "/", "test.txt"),
flags="-j")
r <- read.table(unz(paste0(testDir, "/testZip.zip"), "test.txt"))
head(r, 3)
# mpg cyl disp hp drat wt qsec vs am gear carb
# Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Это также работает с файлами .csv.
Но когда я делаю то же самоес .dta (формат данных Stata) происходит сбой.
foreign::write.dta(mtcars, file=paste0(testDir, "/test.dta"))
zip(paste0(testDir, "/testZip"), paste0(testDir, "/", "test.dta"),
flags="-j")
foreign::read.dta(unz(paste0(testDir, "/testZip.zip"), "test.dta"))
# Error in file.exists(file) : invalid 'file' argument
readstata13::read.dta13(unz(paste0(testDir, "/testZip.zip"), "test.dta"))
# Error in file.exists(file) : invalid 'file' argument
rio::import(unz(paste0(testDir, "/testZip.zip"), "test.dta"))
# Error in file.exists(file) : invalid 'file' argument
Как можно прочитать один файл .dta из zip-архива, используя unz()
?
Редактировать
Я понял, что unzip()
работает.
r <- foreign::read.dta(unzip(paste0(testDir, "/testZip.zip"), "test.dta"))
head(r, 3)
# mpg cyl disp hp drat wt qsec vs am gear carb
# 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
# 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Итак, новый вопрос : почему unz()
терпит неудачу?