create multiple tab in CSV file through R - r

I have a query in R, for loading data into .xlsx multiple tabs we use below code
write.xlsx(newtrain,
file = 'path/file.xlsx',
sheetName = 'sheet 1',append=TRUE, row.names=FALSE)
write.xlsx(newtrain,
file = 'path/file.xlsx',
sheetName = 'sheet 2',append=TRUE, row.names=FALSE)
same way I wanted to create for .csv file.

you can simply write csv using below code
write.csv(MyData, file = "MyData.csv",row.names=FALSE)
But if you want multiple sheet like xlsx so please refer below link.
how to write multiple dataframe to multiple sheet of one csv excel file in R?

Related

How modify numbers in Excel using R?

I have an Excel file test.xlsx on my desktop and have a dataframe in R. How can I overwrite numbers in Excel file sheet "Capitals"? Say table in excel starts at B6 and has the same size as table in R.
I tried to run following command, however, it creates a new excel file, however I need to make changes in the existing one.
write.xlsx(df, file = "//Desktop//test.xlsx",
sheetName = "Capitals", append = TRUE)

Adding a .CSV file to excel as a sheet in R

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")

How to convert xlsx files to csv files in RStudio? Need to convert multiple workbooks all with multiple spreadsheets

Trying to write an R script that will convert multiple xlsx workbook files within a folder while also converting the sheets within the workbook as separate csv files.
Looking for a single script to automatically apply code to all workbooks and their spreadsheets.
For reading Excel files, there are several packages.
I personally am happy with the xlsx package, which you can use to read Excel files, as well as their individual sheets. This article looks like it will give you the gist of it.
Each worksheet you read out you should then be able to export to CSV files by using R's built-in write.csv (or write.csv2) method.
Below is an example to convert a single xlsx workbook to multiple csv files.
Note that type conversions are not guaranteed to be correct.
xlsx_path <-"path_to_xlsx.xlsx"
sheet_names <- readxl::excel_sheets(xlsx_path)
# read from all sheets to a list of data frames
xlsx_data <- purrr::map(
sheet_names,
~readxl::read_excel(xlsx_path,.x,col_types = "text",col_names = FALSE)
)
# write a list of data frame to csv files
purrr::walk2(
xlsx_data,sheet_names,
~readr::write_csv(.x,paste0(xlsx_path,"-",.y,".csv"),col_names = FALSE)
)
# csv files will be saved as:
# path_to_xlsx-sheet1.xlsx, path_to_xlsx-sheet2.xlsx, ...
If you need to apply this function to many xlsx files. Use list.files() to get the path to all xlsx files. And write a for loop or use another map function to iterate this process.
If you are using Rstudio it is possible that you already have the package readxl installed. They have many workflows for common usecases explained here: https://readxl.tidyverse.org/articles/articles/readxl-workflows.html
They also provide this nice code snippet to do what you are asking for:
read_then_csv <- function(sheet, path) {
pathbase <- tools::file_path_sans_ext(basename(path))
df <- read_excel(path = path, sheet = sheet)
write.csv(df, paste0(pathbase, "-", sheet, ".csv"),
quote = FALSE, row.names = FALSE)
df
}
path <- readxl_example("datasets.xlsx")
sheets <- excel_sheets(path)
xl_list <- lapply(excel_sheets(path), read_then_csv, path = path)
names(xl_list) <- sheets
If you go to here and put "excel" and "xls" in the search bar, you 'll get a list of packages and functions which might help.

Creating a loop for creating multiple sheet from multiple excel files in R

I have multiple excel files with data. I wanted to split the data in each excel file into multiple sheets within that particular excel file. I have already managed to do that with the following code:
library(Openxlsx)
data<- read.xlsx(file.choose())
splitdata <- split(data, data$Assigned)
splitdata
workbook <- createWorkbook()
Map(function(data,name){
addWorksheet(workbook, name)
writeDataTable(workbook, name, data)
},splitdata, names(splitdata))
saveWorkbook(workbook, file = "WorkbookWithMultipleSheets.xlsx", overwrite = TRUE)
However, I have more than 50 excel files, for which I need to create multiple sheets using the code above. Is there any way to create a loop so that I won't have to write this data for each excel file that I have?
Any help is appreciated! Thank you!

Dynamically converting a list of Excel files to csv files in R

I currently have a folder containing all Excel (.xlsx) files, and using R I would like to automatically convert all of these files to CSV files using the "openxlsx" package (or some variation). I currently have the following code to convert one of the files and place it in the same folder:convert("team_order\\team_1.xlsx", "team_order\\team_1.csv")
I would like to automate the process so it does it to all the files in the folder, and also removes the current xlsx files, so only the csv files remain. Thanks!
You can try this using rio, since it seems like that's what you're already using:
library("rio")
xls <- dir(pattern = "xlsx")
created <- mapply(convert, xls, gsub("xlsx", "csv", xls))
unlink(xls) # delete xlsx files
library(readxl)
# Create a vector of Excel files to read
files.to.read = list.files(pattern="xlsx")
# Read each file and write it to csv
lapply(files.to.read, function(f) {
df = read_excel(f, sheet=1)
write.csv(df, gsub("xlsx", "csv", f), row.names=FALSE)
})
You can remove the files with the command below. However, this is dangerous to run automatically right after the previous code. If the previous code fails for some reason, the code below will still delete your Excel files.
lapply(files.to.read, file.remove)
You could wrap it in a try/catch block to be safe.

Resources