0
votes

I would like to copy a data frame from my .csv file into an xlsx template file. I am using this code:

#here, I have opened the csv file of interest and selected the dataframe I want to copy into the template
data <-read.csv("153L_GONOGO.csv", header = T)
datatoexport <- data[,1:6]
datatoexport <- as.data.frame(data[,1:6])

#I have loaded my xlsx template file
wb <- loadWorkbook("GNG template.xlsx")
sheets <- getSheets(wb)
sheets

#I want to copy my csv data frame into sheet 1 of my xlsx template starting from row 2 and column 1 
addDataFrame(datatoexport, sheets$Sheet1, row.names = F, col.names = F, startRow = 2, startColumn = 1)
saveWorkbook(wb, file = "153L_Gonogoprova.xlsx")

This code has worked perfectly file with excel files previously and it is the first time I am using on a csv file. The error that I get is "Error in addDataFrame(datatoexport, sheets$Sheet1, row.names = F, col.names = F, : attempt to apply non-function". Probably the "addDataFrame" is not the right command I need to use but I don't what else could work. Thank you for your help.

1
Perhaps you find some help calling ?xlsx::addDataFrame?jay.sf
Hi, thank you for your answer. I used addDataFrame before on excel files only and it works, the problem that I am having is that it doesn't recognise the dataframe from my csv files and I am not sure whether I need to use another code to manage files of different extensions, as in ?xlsx: :addDataFrame it doesn't say.PsychSilvia

1 Answers

0
votes

In case this is useful to someone, the error I got was referring to the sheet name of my excel file. I thought that by saying "sheet 1", it would automatically select the right excel sheet, regardless of its name. However sheet 1 was called "GO_AD_AS" and once I specified it in my code, it worked perfectly fine.