-1

I am new to R and would like to have it read all the 10-K financial reports which are in .txt format and I want it to read all these files at once?

I have tried

dir<- "/Users/Documents/Edgar filings"
text = readtext(paste0(dir, "/ALL_2016/*.txt") 

However, nothing happens, I mean no output is produced at the console section, and also there is no errors.

I tried to have it read only one 10-K report through

dir <- "/Users/Documents/Edgar filings"
text = readtext(paste0(dir, "/ALL_2016/10254_10-K_2016-03-11*.txt") 

but again neither any output nor any error messages.

I tried it through quanteda

mycorpus <- corpus(textfile("~/ALL_10-K_2016/*.txt"))  

but this time it says "textfile function is not available for 3.4.3" even though my R version is 3.4.4 which I updated a couple of weeks ago.

I also tried using list.files() but I do not know how to create a loop coding, so I just got stuck!

I highly appreciate if someone please help.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
xantos
  • 1
  • search for read folder CSV r – MichaelChirico Mar 23 '18 at 01:46
  • Have a look at my answer to [How to make a list of data frames](https://stackoverflow.com/a/24376207/903061). It will tell you how to read the files into a list using `list.files` and then (if applicable) combine them into a single data frame. – Gregor Thomas Mar 23 '18 at 01:57

1 Answers1

1

Here is a solution using readChar with vapply:

  1. store the folder path (note the \ before the space)

path <- "/Users/Documents/Edgar\ filings"

  1. store the fully specified file paths

files <- file.path(path, list.files(path))

  1. read in all of the files

documents <- vapply(X=1:length(files), FUN=function(i){ readChar(files[i], file.info(files[i])$size) }, FUN.VALUE = character(1))

  • Hi Kevin, Thanks for your response but for the 3rd code I am having the following error: Error in file(con, "rb") : cannot open the connection In addition: Warning message: In file(con, "rb") : Show Traceback Rerun with Debug Error in file(con, "rb") : cannot open the connection do you have any suggestion for that please? Thanks again. – xantos Mar 23 '18 at 16:56
  • When I also run the second code, at the environment section under the values I get files character (empty). This might be the reason of not getting a result from the third code? – xantos Mar 23 '18 at 17:08
  • Make sure path in step 1 refers to the folder with the .txt files in it (and there are no other files in that folder). Then step 2 creates a vector with the fully specified file paths. After that, step 3 should run without error. – Kevin Gardner Mar 24 '18 at 00:43
  • I re-installed R & Rstudio now the codes are working. However, when I run the codes I do not see the text documents that are read on my console. Is it ok or do I have to do sthg for that please? Thanks much for your help. – xantos Mar 24 '18 at 18:08
  • Step 3 stores the text documents in a vector named documents. To print the contents did you try either typing `print(documents)` or `documents`? Please do accept if the above answers your question. – Kevin Gardner Mar 27 '18 at 01:43
  • It does process when I put "print(documents)" but R aborts the session by notifying that "fatal error occurred". Then ends the session automatically without showing me the content of the text. I think I will try another software coz whatever I do does not help. Thanks much for your time. I Appreciate. – xantos Apr 05 '18 at 15:09