I would like to output data from R to an excel file, and have been using the openxlsx package.
writeDataTable is the function I have been using. The problem is that I want the entries of one of the columns to be a mixture of numbers and text. As a column of a dataframe in R must have all the same type of entries, everything gets converted to character entries.
When I open the excel file that is outputted I get the little green triangle in the corner of the number cells, telling me that they are a number stored as text and giving me the option to convert to number.
I'm wondering is it possible to get R to convert this for me in the Workbook Object that is created before saving the file.
I've spent a lot of time googling the problem, looking for different libraries that might help, but haven't been able to find anything so far. I have an example below. Note that this isn't the exact data frame I am using, but is for demonstrative purposes.
df = data.frame(A = c('Dog', '5', '7.04'), B = c('Cat', '12', '1.23'))
wb = createWorkbook()
addWorksheet(wb, "Sheet2")
writeDataTable(wb, "Sheet2", df)
output_file = "C:\\Users\\johndoe\\documents\\excel_file.xlsx"
saveWorkbook(wb, output_file)
Any help would be much appreciated.
library(xlsx) \ write.xlsx(mydata, "c:/mydata.xlsx")
. – DisruptiveData
tab where youwriteData
into and another tab sayFormula
that has excel formulas toData
tab, e.g. cell A2=Data!A2
. Not ideal (but will get rid of green triangles) as you will first need the formulas to be copied down in template.xlsx and also file size will double as you are keeping two tabs of same data – Flintlock