0

I am trying to make a function to loop through a vector of names and open a connection to a file that is related to that vector.

I am receiving an error and the connection is not being made. In the code below, where x[i] it should become blog, then news, then twitter. Can I not use the vector variable in this manner.

Is there a better way to do this?

**Error in UseMethod("close") :** 
  no applicable method for 'close' applied to an object of class "character"
dirPath <- "L:/Cousera/Capstone/week 2/data/"
fileNames <-  c("en_US.blogs.txt","en_US.news.txt", "en_US.twitter.txt")
datasetName <-c("blogs","news","twitter")


openDataFiles <- function (x, fn, dp) {

for (i in 1:length(fn)) {
   conn <- paste(dp,fn[i], sep = "")
   x[i] <- readLines(conn, encoding="UTF-8") 
   close(conn)
 }
   rm(conn)

}

openDataFiles(datasetName,fileNames,dirPath)
zx8754
  • 52,746
  • 12
  • 114
  • 209

0 Answers0