[英]How to zip multiple CSV files in R?
我正在嘗試在 R 中壓縮多個 CSV 文件。以下是參考代碼。
# Create two dataframes using inbuilt datasets for reproducible code
df1 <- head(mtcars)
df2 <- head(iris)
# Write the files as CSV into working directory
write.csv(df1, file = "Test_File1.csv", row.names = FALSE, quote = FALSE)
write.csv(df2, file = "Test_File2.csv", row.names = FALSE, quote = FALSE)
# Read the 2 CSV file names from working directory
Zip_Files <- list.files(path = getwd(), pattern = ".csv$")
# Zip the files and place the zipped file in working directory
zip(zipfile = "TestZip", files = Zip_Files)
我收到以下警告消息。 尚未創建 Zip 文件。
Warning message:
running command '"zip" -r9X "TestZip" "Test_File1.csv" "Test_File2.csv" ' had status 127
我什至嘗試使用此命令來讀取 CSV 文件名: Zip_Files <- list.files(path = getwd(), pattern = ".csv$", full.names = TRUE)
但我仍然收到上面顯示的警告消息。 我已經在我的電腦上安裝了WinRAR
和7-Zip
。 我正在使用最新版本的 R(3.4.2 64 位)以及最新版本的 RStudio。 我有一個 Windows 7 x64 操作系統。 對此的任何幫助將不勝感激。
問題是 R 的zip
實際上沒有壓縮(壓縮)文件的代碼。 它調用一個外部程序來做到這一點。 您必須讓zip
知道要使用什么程序以及為該程序提供什么參數。 你應該能夠像這樣完成這項工作:
zip(zipfile = "TestZip", files = Zip_Files, flags = " a -tzip",
zip = "C:\\Program Files\\7-Zip\\7Z")
如果您的 7Z 路徑(7Zip 的命令行版本)不同,請進行調整以匹配您的安裝。
一些解釋:
zip = "C:\\\\Program Files\\\\7-Zip\\\\7Z"
參數告訴 R 使用什么程序來執行壓縮。 在這種情況下,我將其指向 7Z,即 7Zip 的命令行版本,但您可以通過將其更改為指向不同的程序來使用其他命令行程序。
flags = " a -tzip"
參數取決於您使用的程序。 我為 7Z 設置了這個。 閱讀7Z 文檔,你會發現你需要給 7Z 一個命令(“a”)和標志(“-tzip”)。 “a”命令表示將這些文件添加到存檔中。 -tzip 標志意味着使它成為 zip 存檔而不是 7Z 存檔。 對於不同的程序,您需要閱讀文檔並為該程序構建適當的標志。
更新:如果您需要在不同的客戶機器上擁有此功能,您應該考慮查看zip 包它不需要任何外部程序並提供類似的功能。
您可以安裝zip包並在您的代碼中使用它。 這樣,任何使用您的代碼的人都可以在不安裝或搜索配置的情況下壓縮文件,這適用於任何操作系統。
library(zip)
# Create two dataframes using inbuilt datasets for reproducible code
df1 <- head(mtcars)
df2 <- head(iris)
# Write the files as CSV into working directory
write.csv(df1, file = "Test_File1.csv", row.names = FALSE, quote = FALSE)
write.csv(df2, file = "Test_File2.csv", row.names = FALSE, quote = FALSE)
# Read the 2 CSV file names from working directory
Zip_Files <- list.files(path = getwd(), pattern = ".csv$")
# Zip the files and place the zipped file in working directory
zip::zip(zipfile = "TestZip", files = Zip_Files)
zip 庫中的 zip 函數已被棄用。 如果要使用絕對路徑壓縮多個文件,則需要使用 zipr。 以下為我工作。
# Install the zip package and call it
install.packages("zip")
library(zip)
# Create two dataframes using prebuilt datasets for reproducible code
df1 <- head(mtcars)
df2 <- head(iris)
# Write the files as CSV into working directory
write.csv(df1, file = "\\path\\to\\your_working_directory\\Test_File1.csv", row.names = FALSE, quote = FALSE)
write.csv(df2, file = "\\path\\to\\your_working_directory\\Test_File2.csv", row.names = FALSE, quote = FALSE)
# Read the 2 CSV file names from working directory
Zip_Files <- list.files(path = "\\path\\to\\your_working_directory\\", pattern = ".csv$", full.names=TRUE)
# Zip the files and place the zipped file in working directory
zip::zipr(zipfile = "\\path\\to\\your_working_directory\\Test.Zip", files = Zip_Files)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.