This question already has answers here:
Write data into a specific workbook sheet
(3 answers)
Closed 9 years ago.
So whenever I want to run my code seperately for different datasets...I want the output from my code to be saved in the same excel spreadsheet but at different sheets....So If I run my code for 20 different datasets...I would want all the output to saved in the same excel spreadsheet but different worksheets...so I would have 20 worksheets in a single excel spreadsheet...is there a special function in r that would let me do this?.....so lets say my existing spreadsheet is called 'Values.csv'....How would I append the rest of my output to this same spreadsheet.
I usually just use write.csv(data,'Values.csv') etc....But I'm not sure how to append my output to this same worksheet...
You can use library XLConnect to do this.
library(XLConnect)
#some sample data
your.data=data.frame(a=1:10,b=21:30)
#Create .xls file
wb <- loadWorkbook("newfile.xls", create = TRUE)
#Create Sheet in file
createSheet(wb,name="name_one")
#write data
writeWorksheet(wb,your.data,sheet="name_one")
#save .xls file
saveWorkbook(wb)
Related
I am a very basic R user and not into loops or advanced R. Challenge I am facing with an Excel Workbook with 50 worksheets and each worksheet is comprising of 1 Million rows. Loading into R this huge workbook of appx 5GB is not getting possible. I am looking forward for a fast method in R to get this workbook split into multiple CSVs of a single consolidated one
Tried to search lot of solutions and system is not responding for hours.
Please help me out of this
What about a function like this?
library(readxl)
csv_saver <- function(sheet_number){
csv <- read_xlsx(path = "yr_file_name.xlsx", sheet = sheet_number)
write.csv(csv, file = paste0("sheet_",sheet_number,".csv"))
}
lapply(1:50, csv_saver)
This reads in the sheet number specified by the variable sheet_number as a dataframe and then writes the dataframe out as csv file. You then apply that function to the vector of all the numbers between 1 and 50
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!
Let's say I have an excel column with 10 different cells with values. How do I create a variable in r that includes only the first four or first 6 cells in that column?
This question is very vague, please provide more information if you need specifics...
First of all, you'll want to use a library to import the contents of the excel file, I recommend using readxl (http://readxl.tidyverse.org)
You can then follow the documentation to read specific ranges from the excel file or just import all the contents and trim the resulting tibble.
Probably
# Install -readxl- package that loads in Excel spreadsheets
install.packages("readxl")
# Load -readxl- package for use
require(readxl)
# Change working directory to directory where spreadsheet is saved in
setwd("<Insert path here>")
# Save spreadsheet data to memory
myData <- read_excel("myData.xlsx", sheet = 1)
# Subset first four or six observations
firstFour <- myData[1:4,]
firstSix <- myData[1:6,]
Let me know if you don't understand.
This question already has answers here:
characters converted to dates when using write.csv
(3 answers)
Closed 5 years ago.
Within R, I have a character string ID formatted like XX-XX where XX is any integer number between 01 and 99.
However, when the numbers that make the character string could resemble a date, Excel is automatically forcing this change.
I am writing to a .csv file directly from within R using write.csv().
Unfortunately, I am not able to change the ID format convention and I also require this to be controlled from within R as it is a small part of a very large automated process where people using the software do not necessarily have any understanding of the mechanics. Furthermore, configuring excel on every person who uses this systems software is not desirable but will consider it as a last resort.
Is this possible? I am open to using a different writing option like the xlsx package if it can provide a solution.
MWE Provided:
# Create object with digits that will provoke the problem.
ID <- data.frame(x = '03-15')
# Write object to a csv file within the working directory.
write.csv(ID, file = 'problemFile.csv')
# Now open the .csv file in excel and view the result.
I recommend the openxlsx package. This worked for me:
ID <- data.frame(x = '03-15')
wb <- createWorkbook()
addWorksheet(wb, "Sheet 1")
writeData(wb, "Sheet 1", x = ID)
saveWorkbook(wb, "test.xlsx", overwrite = TRUE)
openXL("test.xlsx")
Check this:
characters converted to dates when using write.csv
I would like to convert an Excel file (say it's name is "Jimmy") that is saved as a macro enabled workbook (Jimmy.xlsm) to Jimmy.xlsx.
I need this to be done in a coding environment. I cannot simply change this by opening the file in Excel and assigning a different file-type. I am currently programming in R. If I use the function
file.rename("Jimmy.xlsm", "Jimmy.xlsx")
the file becomes corrupted.
In your framework you have to read in the sheet and write it back out. Suppose you have an XLSM file (with macros, I presume) called "testXLSM2X.xlsm" containing one sheet with tabular columns of data. This will do the trick:
library(xlsx)
r <- read.xlsx("testXLSMtoX.xlsm", 1) # read the first sheet
# provides a data frame
# use the first column in the spreadsheet to create row names then delete that column from the data frame
# otherwise you will get an extra column of row index numbers in the first column
r2w<-data.frame(r[-1],row.names=r[,1])
w <- write.xlsx(r2w,"testXLSMtoX.xlsx") # write the sheet
The macros will be stripped out, of course.
That's an answer but I would question what you are trying to accomplish. In general it is easier to control R from Excel than Excel from R. I use REXCEL from http://rcom.univie.ac.at/, which is not open source but pretty robust.
Here is a function that converts XLSM files to XLSX files with the R package RDCOMClient :
convert_XLSM_File_To_XLSX <- function(path_XLSM_File, path_XLSX_File)
{
xlApp <- COMCreate("Excel.Application")
xlApp[['Visible']] <- FALSE
xlApp[["DisplayAlerts"]] <- FALSE
xlWbk <- xlApp$Workbooks()$Open(path_XLSM_File)
xlWbk$SaveAs(path_XLSX_File, 51)
xlWbk$Close()
xlApp$Quit()
}
library(RDCOMClient)
convert_XLSM_File_To_XLSX(path_XLSM_File, path_XLSX_File)