I have a set of csv files in different directories, I would like to put them all in one excel file, each table in one excel sheet.
I am using R and xlsx package.
# loading the library
library(xlsx)
rm(list = ls())
# getting the path of all reports (they are in csv format)
restab = system("ls /home/ubuntu/ibasruns/control/*/report",intern = TRUE)
# creating work book
wb <- createWorkbook()
# going through each csv file
for (item in restab)
{
# making each as a sheet
sheet <- createSheet(wb, sheetName=strsplit(item,"/")[[1]][6])
addDataFrame(read.csv(item), sheet)
# saving the workbook
saveWorkbook(wb, "AliceResultSummary.xlsx")
}
# finally writing it.
write.xlsx(wb, "AliceResultSummary.xlsx")
However, in the last line, I am getting the following error,
Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot
coerce class "structure("jobjRef", package = "rJava")" to a data.frame
Is there any thing that I am missing ?
You're close:
# creating work book
wb <- createWorkbook()
# going through each csv file
for (item in restab)
{
# create a sheet in the workbook
sheet <- createSheet(wb, sheetName=strsplit(item,"/")[[1]][6])
# add the data to the new sheet
addDataFrame(read.csv(item), sheet)
}
# saving the workbook
saveWorkbook(wb, "AliceResultSummary.xlsx")
The write.xlsx is not needed here; it's just used to create a workbook from a single data frame.
Related
I have an Excel spreadsheet with 8 worksheets. Each worksheet has a different data and I have a R code to update them.
My question is: Is there a way to export a .xlsx file in R to update only one specific worksheet in my Excel file without replacing the entire spreadsheet for a new file?
In my previous attempts, with the openxlsx package, I just created a whole new file, which doesn't solve my problem.
You can achieve it using the following workflow:
# load environment
library(xlsx)
# define file path
file_path = '/home/user/Downloads/my_sheet.xlsx'
# load file as a workbook
file = loadWorkbook(file_path)
# check the sheets in the file
tbls = getSheets(file)
# remove the sheet you want to change
removeSheet(file, sheetName = "Sheet1")
# start a new from scratch
new_sheet = createSheet(file, sheetName = "Sheet1")
# create a dataframe
df = data.frame()
# link the dataframe to the new sheet
addDataFrame(df, new_sheet, row.names = FALSE)
# save the workbook/file
saveWorkbook(file, file_path)
Instead of creating a new dataframe from scratch, you can import an old sheet as a dataframe, with read.xlsx(file, sheetIndex, sheetName).
Attention: an error is thrown if you use short file paths, such as '~/Downloads/my_sheet.xlsx'.
You can solve this problem using the code below.
library(migrittr)
library(openxlsx)
file_path = '/home/user/Downloads/my_sheet.xlsx'
df <- data.frame()
file_path %>%
loadWorkbook() %T>%
removeWorksheet(sheet = "Sheet1") %T>%
addWorksheet(sheetName = "Sheet1") %T>%
writeData(sheet = "Sheet1", x = df) %T>%
saveWorkbook(., file = '/home/user/Downloads/my_sheet.xlsx', overwrite = T)
I am trying to use openxlsx::write.xlsx to write results into Excel spreadsheet in R.
if the file exists and a new sheet is to be added, I can use append=T. Instead of using if to check the file, are there any ways to automatically check?
if the file and sheet both exist and this sheet is to be updated, how should I do to overwrite the results? Thanks.
Here is an openxlsx answer. In order to demonstrate, we need some data.
## Create a simple test file
library(openxlsx)
hs <- createStyle(textDecoration = "Bold")
l <- list("IRIS" = iris, "MTCARS" = mtcars)
write.xlsx(l, file = "TestFile.xlsx", borders = "columns", headerStyle = hs)
Question 1
You can check whether or not the file exists with
## Check existence of file
file.exists("TestFile.xlsx")
You can check if the tab (sheet) exists within the workbook
## Check available sheets
getSheetNames("TestFile.xlsx")
Steps for question 2:
1. Read the file into a Workbook object.
2. Pull the data from the sheet you want to modify into a data.frame.
3. Modify the data.frame to taste
4. Save the data back into the Workbook
5. Save the Workbook out to disk
In order to have a simple example to work with, let's create a simple test file.
## Load existing file
wb = loadWorkbook("TestFile.xlsx")
## Pull all data from sheet 1
Data = read.xlsx(wb, sheet=1)
## Change a single element for demonstration
## ** Beware!! ** Because of the header,
## the 2,2 position in the data
## is row 3 column 2 in the spreadsheet
Data[2,2] = 1492
## Put the data back into the workbook
writeData(wb, sheet=1, Data)
## Save to disk
saveWorkbook(wb, "TestFile.xlsx", overwrite = TRUE)
You can open up the spreadsheet and check that the change has been made.
If you want to completely change the sheet (as in your comment),
you can just delete the old sheet and replace it with a new one
using the same name.
removeWorksheet(wb, "IRIS")
addWorksheet(wb, "IRIS")
NewData = data.frame(X1=1:4, X2= LETTERS[1:4], X3=9:6)
writeData(wb, "IRIS", NewData)
saveWorkbook(wb, "TestFile.xlsx", overwrite = TRUE)
You can check if sheet exists before and if so remove it, if not append it to existing file this command also will create file if it does not exist.
library(xlsx)
path <- "testing.xlsx"
sheet_name = "new_sheet"
data <-
data.frame(
B = c(1, 2, 3, 4)
)
if(sheet_name %in% names(getSheets(loadWorkbook(path)))){
wb <- loadWorkbook(path)
removeSheet(wb, sheetName = sheet_name)
saveWorkbook(wb, path)
}
write.xlsx(data, path, sheetName = sheet_name, append = TRUE)
I have multiple excel files having different names e.g USA.xlsx, India.xlsx etc. Each file has only one sheet. I want to rename the sheet of each file as Sheet 1.
Desired output USA.xlsx should have sheet 1, India.xlsx should have sheet 1 and so on. I have 1800 excel files. I know renameWorksheet(wb, sheet, newName) will work for one file. I have 1800 excel files
I think this should get the job done:
library(openxlsx)
list.files(pattern = '.xlsx')
for(file in list.files(pattern = '.xlsx')){
wb <- loadWorkbook(file, xlsxFile = NULL)
names(wb)[1] <- 'Sheet1'
saveWorkbook(wb, file, overwrite = TRUE)
}
I have a list of data.frame's that I would like to output to their own worksheets in excel. I can easily save a single data frame to it's own excel file but I'm not sure how to save multiple data frames to the their own worksheet within the same excel file.
library(xlsx)
write.xlsx(sortedTable[1], "c:/mydata.xlsx")
Specify sheet name for each list element.
library(xlsx)
file <- paste("usarrests.xlsx", sep = "")
write.xlsx(USArrests, file, sheetName = "Sheet1")
write.xlsx(USArrests, file, sheetName = "Sheet2", append = TRUE)
Second approach as suggested by #flodel, would be to use addDataFrame. This is more or less an example from the help page of the said function.
file <- paste("usarrests.xlsx", sep="")
wb <- createWorkbook()
sheet1 <- createSheet(wb, sheetName = "Sheet1")
sheet2 <- createSheet(wb, sheetName = "Sheet2")
addDataFrame(USArrests, sheet = sheet1)
addDataFrame(USArrests * 2, sheet = sheet2)
saveWorkbook(wb, file = file)
Assuming you have a list of data.frames and a list of sheet names, you can use them pair-wise.
wb <- createWorkbook()
datas <- list(USArrests, USArrests * 2)
sheetnames <- paste0("Sheet", seq_along(datas)) # or names(datas) if provided
sheets <- lapply(sheetnames, createSheet, wb = wb)
void <- Map(addDataFrame, datas, sheets)
saveWorkbook(wb, file = file)
Here's the solution with openxlsx:
## create data;
dataframes <- split(iris, iris$Species)
# create workbook
wb <- createWorkbook()
#Iterate the same way as PavoDive, slightly different (creating an anonymous function inside Map())
Map(function(data, nameofsheet){
addWorksheet(wb, nameofsheet)
writeData(wb, nameofsheet, data)
}, dataframes, names(dataframes))
## Save workbook to excel file
saveWorkbook(wb, file = "file.xlsx", overwrite = TRUE)
.. however, openxlsx is also able to use it's function openxlsx::write.xlsx for this, so you can just give the object with your list of dataframes and the filepath, and openxlsx is smart enough to create the list as sheets within the xlsx-file. The code I post here with Map() is if you want to format the sheets in a specific way.
The following code works perfectly, which is from:https://rpubs.com/gbganalyst/RdatatoExcelworkbook
packages <- c("openxlsx", "readxl", "magrittr", "purrr", "ggplot2")
if (!require(install.load)) {
install.packages("install.load")
}
install.load::install_load(packages)
list_of_mydata
write.xlsx(list_of_mydata, "Excel workbook.xlsx")
lets say your list of data frames is called Lst and that the workbook you want to save to is called wb.xlsx. Then you can use:
library(xlsx)
counter <- 1
for (i in length(Lst)){
write.xlsx(x=Lst[[i]],file="wb.xlsx",sheetName=paste("sheet",counter,sep=""),append=T)
counter <- counter + 1
}
G
I think the simplest solution is still missing. Using the writexl package you can write a list of data frames easily:
list_of_dfs <- list(iris, iris)
writexl::write_xlsx(list_of_dfs, "output.xlsx")
Also, if you have a named list then those names become the sheet names:
names(list_of_dfs) <- c("a", "b")
writexl::write_xlsx(list_of_dfs, "output.xlsx")
Alternatively, the rio package allows for more export control and the syntax and handling of named lists is similar:
rio::export(list_of_dfs, "output.xlsx")
You can also easily output them to their own workbooks as well.
I am trying to use the xlsx package to put different csv files into one excel workbook with multiple sheets. I found a routine that should work but it is not working for me.
So I have different csv files:
S:/productivity/R/Results/2008.csv
S:/productivity/R/Results/2009.csv
S:/productivity/R/Results/2010.csv
S:/productivity/R/Results/2011.csv
S:/productivity/R/Results/2012.csv
My R codes look like:
# loading the library
library(xlsx)
rm(list = ls())
# getting the path of all csv files
myfiles = system("S:/productivity/R/Results",intern = TRUE)
wb <- createWorkbook()
# going through each csv file
for (item in myfiles) {
# create a sheet in the workbook
sheet <- createSheet(wb, sheetName=strsplit(item,"/")[[1]][5])
# add the data to the new sheet
addDataFrame(read.csv(item), sheet)
}
# saving the workbook
saveWorkbook(wb, "2008_2012.xlsx")
I receive the following error:
myfiles = system('"S:/productivity/R/Results"',intern = TRUE)
Error in system("\"S:/productivity/R/Results\"", intern = TRUE) :
'"S:/productivity/R/Results"' not found
Personally, I use XLConnect for these tasks.
The steps for writing to multiple sheets are:
create a new workbook
create the sheet
output the data to the sheet
save the sheet
--
SAMPLE CODE:
library(data.table) ## for fast fread() function
library(XLConnect)
folder <- "folder/where/CSV_files_are_located"
f.out <- "path/to/file.xlsx"
## load in file
wb <- loadWorkbook(f.out, create=TRUE)
## get all files
pattern.ext <- "\\.csv$"
files <- dir(folder, full=TRUE, pattern=pattern.ext)
## Grab the base file names, you can use them as the sheet names
files.nms <- basename(files)
files.nms <- gsub(pattern.ext, "", files.nms)
## set the names to make them easier to grab
names(files) <- files.nms
for (nm in files.nms) {
## ingest the CSV file
temp_DT <- fread(files[[nm]])
## Create the sheet where the file will be outputed to
createSheet(wb, name=nm)
## output the csv contents
writeWorksheet(object=wb, data=temp_DT, sheet=nm, header=TRUE, rownames=NULL)
}
saveWorkbook(wb)
if you would like to see your file
system(sprintf("open %s", dirname(f.out))) ## For the containing folder
system(sprintf("open %s", f.out)) ## for opening the file with default app, ie excel