wight now I'having an issue with R. I could not use library openxlsx in my PC. When I try to import, these errors show me that:
Error: package or namespace load failed for ‘openxlsx’:
object ‘zipr’ is not exported by 'namespace:zip'
Any one suggest me some different method to save file in xlsx format. I tried writexl package, however I don't know how to save file in seperate worksheets, the description of the function write_xlsx in this package only show me this:
write_xlsx(
x,
path = tempfile(fileext = ".xlsx"),
col_names = TRUE,
format_headers = TRUE
)
Can you use the WriteXLS package?
WriteXLS(x, ExcelFileName = paste0(tempfile, ".xlsx"), SheetNames = NULL, row.names = FALSE, col.names = TRUE)
Edit:
If you're trying to save data to different sheets, turn your data into a list:
mySheets = c("data1", "data2")
In case you would like to continue using openxlsx library in order to save in xlsx format and with different worksheets, you can solve the namespace error by upgrading the zip package to latest version v2.1.0, same issue as posted here. The zip package is a dependency of openxlsx.
Related
I'm able to add a dataframe to excel as a separate sheet. However, I want to be able to add a .CSV file that is already created as a sheet.
Code that works to add dataframe as a sheet:
library(xlsx)
write.xlsx(dataframe, file = excelFileName,
sheetName=excelsheetname, append=TRUE,row.names = FALSE)
I need to be able to replicate the same thing as above. However, instead of a dataframe, it is a .CSV file. Is there a solution?
Thanks
It seems like the only step missing in your solution is to first read the CSV file into a dataframe using read.csv or read.table:
library(xlsx)
dataframe <- read.csv(csv)
write.xlsx(dataframe, file = excelFileName,
sheetName=excelsheetname, append=TRUE,row.names = FALSE)
If you specifically want to add a csv to an excel sheet without first reading it in then that is another story, and you should clarify it in your question.
The following works and suits my needs.
csvDF = read.csv(file = csvFileName, as.is = 1,stringsAsFactors = FALSE, header = FALSE)
write.xlsx(csvDF , file = excelFileName,sheetName=sheetNameInfo, append=TRUE,row.names = FALSE, col.names = FALSE)
To start, here is a template that you can use to import an Excel file into R:
library("readxl")
read_excel("Path where your Excel file is stored\\File Name.xlsx")
And if you want to import a specific sheet within the Excel file, then you may use this template:
library("readxl")
read_excel("Path where your Excel file is stored\\File Name.xlsx",sheet = "Your sheet name")
Note: In the R Console, type the following command to install the readxl package:
install.packages("readxl")
when i export excel file in R using open xlsx package
if i open excel file it's shown these message:
"We found a problem with some content <nnnn>.xlsx. Do you want us to try and
recover as much as we can?"
i think this issue come from Openxlsx package,
this is my code
openxlsx::write.xlsx( df, file = "NG.xlsx" , sheetName = "df" , col.names =
TRUE , row.names = FALSE )
how can i solve this problem in R ?
I am using ‘openxlsx’ package in R. ٰI want to add some data in xlsx file. I have used following code to create the workbook and add worksheet in it.
wb=createWorkbook()
addWorksheet(wb,"sheet 1")
writeData(wb,sheet = 1,"From",startCol = 1,startRow = 1)
writeData(wb,sheet = 1,"To",startCol = 2,startRow = 1)
writeData(wb,sheet = 1,"From",startCol = 1,startRow = 2)
writeData(wb,sheet = 1,"From",startCol = 1,startRow = 2)
saveWorkbook(wb,"file.xlsx",overwrite = TRUE)
This code was working well for a long time, But recently, I am facing this error
Error in addWorksheet(wb, "sheet 1") : First argument must be a
Workbook.
How this error will be resolved?
I had the same issue with this. I did the followings and it fixed the issue. Maybe it can solve yours.
Close R or RStudio.
Make sure that your current working directory does not have any other file or folder. In other words, the path in which you would like to save the xlsx is empty prior to running createWorkbook(). If you have already saved any file in there, just copy and paste it somewhere else.
Run your code again from the beginning.
I had the same problem and I just unloaded the package, reinstalled it and reloaded it, and it worked (without having to close R studio) :
detach("package:openxlsx", unload=TRUE)
install.packages("openxlsx")
library(openxlsx)
I was able to fix this error by disabling XLSX package
detach("package:xlsx", unload = TRUE)
Works for me unload it and re-install (or uninstall) library rio
detach("package:rio", unload=TRUE)
detach("package:openxlsx", unload=TRUE)
install.packages("openxlsx", "rio")
library(openxlsx)
Encountered the same issue, removed library(xlsx) & library(readxl) from script and now runs without issue. Using the below libraries now in script for context:
library(openxlsx)
library(rio)
library(rJava)
I experienced a similar issue when I tried to re-run code to export an XLSX file. To have my code working well, I just made sure the workbook (wb) was removed from my global environment. I think inserting the following line right before your code may help.
rm(list=deparse(substitute(wb)),envir=.GlobalEnv)
The readr package in the tidyverse has the option to automatically unpack a zip file and convert it to a tibble. But I have a zip file that holds multiple csv files. In the line of code below, SSPdataZip has three files in it. When I run it I get a warning "Multiple files in zip ..." and the name of the one it chooses. I know the name of the one I want but can't figure out how to tell read_csv what it is. Is there an option I'm missing?
temp <- readr::read_csv(SSPdataZip, col_names = TRUE, guess_max = 2000)
I believe you can use unz to achieve this:
readr::read_csv(unz(description = "SSPdataZip", filename = "FileName.csv"), col_names = TRUE, guess_max = 2000)
I'm trying to open a 163MB .xlsx file in R.
library(openxlsx)
df <- read.xlsx(xlsxFile = "df.xlsx", sheet = 1, colNames = T)
Doing this this small file (relatively small) uses all the 8GB of RAM I have on my laptop.
I have a CSV version of this file but due to the use of , and ; in one of the columns using a CSV is not an option How does this happen, knowing that I recently loaded a kaggle file (a 0.5GB csv) into R and still used my laptop for browsing internet ?
Edit : the RAM usage + output of pryr::object_size(df)
did you try readxl package https://blog.rstudio.org/2017/04/19/readxl-1-0-0/
read_xlsx(path, sheet = NULL, range = NULL, col_names = TRUE,
col_types = NULL, na = "", trim_ws = TRUE, skip = 0, n_max = Inf,
guess_max = min(1000, n_max))
You can also read it as tab delimited (read.csv(..., sep="\t")) or save it as a .txt file and read it as tab delimited.
It looks like this is (or at least was) a problem with openxlsx. This Github issue describes the problem of inflated file sizes and suggests a solution (use the development version): https://github.com/awalker89/openxlsx/issues/161
So, potential solutions:
Try the development version of openxlsx (devtools::install_github("awalker89/openxlsx")
As suggested by #Ajay Ohri, try the readxl package instead.
Load the file and save it as a binary R file with save() or saveRDS()
Try again with the .csv file with readr::read_csv() or data.table::fread(); both are faster than base R's read.csv()