Search code examples
rimportlapplyreadxl

lapply and readxl Error


I am using readxl and lapply to import multiple .xlsx files into my environment. The following worked perfectly before but now when I try to re-run it, it gives me the following error:

Error in read_fun(path = path, sheet = sheet, limits = limits, shim = shim, : Evaluation error: zip file 'data.xlsx' cannot be opened.

Code:

setwd("./Data Folder") #set path in order to avoid lapply error (This is what solved it last time I got errors)

Load all "Data Folder" datasets

library(readxl)

file.list <- list.files(path = "./Data Folder", pattern = '*.xlsx') df.list <- lapply(file.list, read_excel)

I have checked if the path I entered is still correct and I didn't alter it by mistake. I have also tried to open the documents in the folder using excel and there is no problem with the files. Any ideas?


Solution

  • I have figured out the problem. I had two different tabs opened in RStudio, one was a R markdown and the other an R Script. I was trying to run the code in R markdown without realising and so I got the lapply error as the setwd was not saved in R's system.

    If anyone has this problem at any point:

    • check if you are in an R Script
    • set the folder you are taking the data out from as your home folder
    • run the entire chunk in markdown at once