[R] Use "append" in the function write.xlsx2 or write.xlsx (openxlsx package)

Ivan Calandra ivan.calandra at univ-reims.fr
Tue May 3 09:32:12 CEST 2016


Not sure this is what you need or the best way to do it, but you could 
do something along these lines:

#first create a file name that depends on which file names already 
exist. The big thing here is list.files() but you probably need to adapt 
the rest to your needs:
fileNumber <- list.files(pattern="xxx")
fileName <- paste0("xxx", length(fileNumber)+1 , ".xlsx")

#then write to xlsx in a loop:
for(i in ...){
   write.xlsx2(df_all2[[i]], file=fileName, sheetName=i, append=TRUE)
}

This works at least with xlsx::write.xlsx2(), with no error or warning 
even for when the file does not exist (first iteration)

HTH,
Ivan

--
Ivan Calandra, PhD
Scientific Mediator
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calandra at univ-reims.fr
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/

Le 03/05/2016 à 05:21, jpm miao a écrit :
> Hi,
>
>     I am trying to print several dataset in several different sheets of the
> same xlsx file.
> I sometimes run the codes more than once, and I need to erase all things in
> the target file, and I do not use "append" in the first function call.
> However I want to write it in a loop. Is there a way to initiate a blank
> xlsx file so that I can use "append = TRUE" whenever I call the function
> write.xlsx2?
>
> output_file2<-"data_xx.xlsx"
> write.xlsx2(x= df_all2[["a"]], file = output_file2, sheetName = "a")
> write.xlsx2(x= df_all2[["b"]], file = output_file2, sheetName = "b",
> append=TRUE)
> write.xlsx2(x= df_all2[["c"]], file = output_file2, sheetName = "c",
> append= TRUE)
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list