-1

How to load a folder consisting of folders in R, for windows?

I have various text files in each sub-folder, all of which I want to load in one-go. Please suggest a convenient and easy method.

Thank you.

1 Answers1

2

I am assuming that by "upload" you mean "load into R". There are several ways to do this, below are two. Note that the first step is having the correct list of files with full path (or be working in the appropriate wd)

# 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)


# Load them in
#----------------------------#
  # Method 1:
  invisible(sapply(files, source, local=TRUE))

  #-- OR --#

  # Method 2:
  sapply(files, function(f) eval(parse(text=f)))
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
  • Yes I meant load. The code you gave worked for me. Thank you! – Nidhi Sanghavi Mar 25 '13 at 15:12
  • @Ricardo Saporta Hey Ricardo, not sure if you will read this, but when applying your answer I get a parsing error for both methods 1 and 2. Method 1: `Error in FUN(X[[i]], ...) : unexpected input`. Method 2: `Error in parse(text = f) : :1:3: unexpected '/' 1: C:/` Any idea why this would happen? – Tom Dec 04 '20 at 08:35