I am using this answer to load in a folder of Excel Files:
# Get the list of files #----------------------------# folder <- "path/to/files" fileList <- dir(folder, recursive=TRUE) # grep through these, if you are not loading them all # use platform appropriate separator files <- paste(folder, fileList, sep=.Platform$file.sep) So far, so good.
# Load them in #----------------------------# # Method 1: invisible(sapply(files, source, local=TRUE)) #-- OR --# # Method 2: sapply(files, function(f) eval(parse(text=f))) But the source function (Method 1) gives me the error:
Error in source("C:/Users/Username/filename.xlsx") : C:/Users/filename :1:3: unexpected input 1: PK ^ For method 2 get the error:
Error in parse(text = f) : <text>:1:3: unexpected '/' 1: C:/ ^ EDIT: I tried circumventing the issue by setting the working directory to the directory of the folder, but that did not help.
Any ideas why this happens?
EDIT 2: It works when doing the following:
How can I read multiple (excel) files into R?
setwd("...") library(readxl) file.list <- list.files(pattern='*.xlsx') df.list <- lapply(file.list, read_excel)
dir(folder, recursive = TRUE, full.names = TRUE), it will remove the need of the followingpastesource("C:/Users/Username/filename.xlsx"), shouldn't you usesourcejust to run R script files? Try with this: try this:dir(folder, recursive=TRUE, full.names = TRUE, pattern = ".R$")Error in file(filename, "r", encoding = encoding) : cannot open the connectionSecond error stays the same. Could there be anything wrong with the naming of the file or the filepath (undesirable characters?) EDIT: for both of your commentssourceis to run external R code. If you need to load Excel files, try with one of this libraries:readxl(probably what you are looking for),openxlsx,tidyxl(this one is the most complicated but the best if your excel files are rather messy)