I am using the below code to export a data frame from RStudio to excel file. I want to define the sheet name while exporting however despite of writing the correct code the exported file contains the default sheet name "Sheet1". Seeking help where I am going wrong? I am using "xlsx" package.
write.xlsx(SLIDE6FVOLT, "C:\\Users\\I0510906\\Desktop\\RAuto\\SLIDE6FVOLT.xlsx",
sheetname = "SLIDE6FVOLT", colnames = TRUE, rownames = FALSE)
Parameter is called sheetName= not sheetname=
Related
I am trying to output multiple small data frames to an Excel file. The data frames are residuals and predicted from mgcv models run from a loop. Each is a separate small data set that I am trying to output to a separate worksheet in the same Excel spreadsheet file.
The relevant line of code that is causing the error is from what I can tell this line of code
write.xlsx(resid_pred, parfilename, sheetName=parsheetname, append = TRUE)**
where resid_pred is the residuals predicted data frame, parfilename is the file name and path and
parsheetname is the sheet name.
The error message is
Error in save Workbook(wb, file = file, overwrite = overwrite) : File already exists!
Which makes no sense since the file would HAVE to exist if I am appending to it. Does anyone have a clue?
Amazingly the following code will work:
write.xlsx2(resid_pred, file=parfilename, sheetName= parsheetname, col.names =
TRUE, rowNames = FALSE, append = TRUE, overwrite = FALSE)
The only difference is it is write.xlsx2 not just xlsx.
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")
I am using the package xlsx to read and save the existing names of Excel sheets.
require(xlsx)
myworkbook <- loadWorkbook("myfile.xlsx")
list_of_sheets_names <- names(getSheets(myworkbook))
The problem that I have is that many of the names are written using special characters. I haven't had this problem when reading the content of the sheets, since I can - with the function read.xlsx - specify the encoding:
mydataframe <- read.xlsx(myfile, sheetIndex = 1, header = FALSE,
stringsAsFactors = FALSE, encoding = "UTF-8")
But this function retrieves data only from the cells of the sheet, it does not retrieve the name of Excel sheets.
Do you know any way in which I could use R to read the name of an Excel sheet specifying that it is encoded as UTF-8?
I don't need to use xlsx necessarily, I am open to other packages.
I am trying to copy some data from an R data frame (Shipments) to an excel file using writeWorkBook function in the XLConnect package. However, it is not copying anything to the excel file. The execution doesn't result in any error/warning appearing in the console. It just doesn't copy.
I have loaded the library XLConnect and made sure I am not loading the library xlsx. The column to be copied has been type-casted to dataframe as I thought that might be an issue.
wbnames is an additional thing. I directly wrote the sheet name in the writeWorkBook and it should have worked fine. Even with wbnames there hasn't been any change in the result.
I originally intended to copy the content to a macro file and then run the macro file from R itself but it wasn't working. So I thought it may be because of macro file but the function is not working on .xlsx itself.
So, not sure what is the issue. Would be grateful if I can get some help here. Am I missing something?
library(XLConnect)
library(RDCOMClient)
xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open(FILEPATH+FILENAME.XLSX)
xlWb <- loadWorkbook(FILEPATH+FILENAME.XLSX)
wbnames <- as.vector(getSheets(xlWb))
# Copy a column from the existing data frame and paste it to the first
# sheet of the FILENAME.XLSX, starting at Row#6, no headers and no rownames:
writeWorksheet(xlWb, as.data.frame(Shipments$SHIPMENT_ID),
sheet = wbnames[1], startRow = 6, header = F, rownames = NULL)
xlWb is the R-object that contains the workbook. It looks like the data has been written to the workbook, which is good. In order to view in Excel format, however, you still need to save the workbook to Excel. Add this line after your code and you should see a document called your_file_name.xlsx with your data in your working directory:
XLConnect::saveWorkbook(xlWb, "your_file_name.xlsx")
I am using "openxlsx" package to read and write excel files. I have a fixed file with a sheet called "Data" which is used by formulas in other sheets. I want to update this Data sheet without touching the other.
I am trying the following code:
write.xlsx(x = Rev_4, file = "Revenue.xlsx", sheetName="Data")
But this erases the excel file and creates a new one with just the new data in the "Data" sheet while all else gets deleted. Any Advice?
Try this:
wb <- loadWorkbook("Revenue.xlsx")
writeData(wb, sheet = "Data", Rev_4, colNames = F)
saveWorkbook(wb,"Revenue.xlsx",overwrite = T)
You need to load the complete workbook, then modify its data and then save it to disk. With writeData you can also specify the starting row and column. And you could also modify other sections before saving to disk.
I've found this package. It depends on openxlsx and helps to insert many sheets on a xlsx file. Maybe it makes easier:
Package documentation
library(xlsx2dfs)
# However, be careful, the function xlsx2dfs assumes
# that all sheets contain simple tables. If that is not the case,
# use the accepted answer!
dfs <- xlsx2dfs("Revenue.xlsx") # all sheets of file as list of dfs
dfs["Data"] <- Rev_4 # replace df of sheet "Data" by updated df Rev_4
dfs2xlsx(dfs, "Revenue.xlsx") # this overwrites the existing file! cave!