openxlsx not able to read from .xlsx file in R

前端 未结 2 1606
迷失自我
迷失自我 2020-12-19 23:08

I am trying to read value from a .xlsx file using openxlsx package in R. In simple words, I need to write a row of data, which then p

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 00:06

    ?read.xlsx
    

    Formulae written using writeFormula to a Workbook object will not get picked up by read.xlsx(). This is because only the formula is written and left to be evaluated when the file is opened in Excel. Opening, saving and closing the file with Excel will resolve this.

    So the file needs to be opened in Excel and then saved, I can verify that this does work. However this may not be suitable for you.

    XLConnect seems to have the desired functionality

    # rjava can run out of memory sometimes, this can help.
    options(java.parameters = "-Xmx1G")
    library(XLConnect)
    
    file_path = "test.xlsx"
    
    input_row <- c("c", 5)
    
    wb <- loadWorkbook(file_path, create=F)
    writeWorksheet(wb, 1, startRow = 1, startCol = 1, data = data.frame(input_row))
    setForceFormulaRecalculation(wb, 1, TRUE)
    saveWorkbook(wb)
    
    # checking
    wb <- loadWorkbook(file_path, create=F)
    readWorksheet(wb, 1)
    

提交回复
热议问题