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)
Related
I have merged two tables together and I want to write them to a .txt file. I have managed to do this, however when I open the .txt file in excel the symbols   have been added to some values. How do I stop this from happening? I have used the following code:
ICU_PPS <- merge(ICU, PPS, by=c("Study","Lane","Isolate_ID","Sample_Number","MALDI_ID", "WGS","Source"),all=TRUE)
write.table(ICU_PPS,"ICUPPS2.txt", sep="\t", row.names = FALSE)
An example of some values in a column that I get:
100_1#175
100_1#176
100_1#177
100_1#179
100_1#18 
100_1#19 
100_1#20 
What I want to achieve:
100_1#175
100_1#176
100_1#177
100_1#179
100_1#18
100_1#19
100_1#20
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?
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 have four csv files with different formats and variables, combining these 4 CSV files into one excel file using below code
library(rJava)
library(xlsx)
rm(list = ls())
# getting the path of all reports (they are in csv format)
files <- list.files(pattern = "\\.csv$")
# creating work book
wb <- createWorkbook()
# going through each csv file
for (item in files)
{
# create a sheet in the workbook
sheet <- createSheet(wb, sheetName=strsplit(item,"[.]")[[1]][1])
# add the data to the new sheet
addDataFrame(read.csv(item), sheet,row.names=FALSE)
}
# saving the workbook
saveWorkbook(wb, "crosstabs of data.xlsx")
In csv file one sheet the variable name is source / Medium But it is appeared in output excel file as Source...Medium,
% New Sessions variable is appeared as X..New.Sessions
and all variables delimited space occupied with . in output excel file
How to overcome this i need what ever the variable names in CSV files same as in output Excel file
This problem is due to read.csv changing names of headers. Column headers like gi/joe will be converted in gi.joe if we do read.csv with header=T. So one need to convert just the header names again using:
names(df) <- gsub("\\.","/",names(df))
OR if acceptable do simply (read headers as data):
addDataFrame(read.csv(item,header=F), sheet,row.names=FALSE)
On a separate note looks like names like gi/joe are not allowed as excel sheet names. Now to validate limitation in excel end open excel and try to name a sheet hi/5. One should get error saying The sheet name contains invalid characters: : \ / ? * [ ]. [I am testing this on mac excel 15.19.1]
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!