2017-08-02 62 views
-1

我的電腦中有一個文件夾中有多個HTML文件。我想在R中閱讀它們,試圖儘可能保持原始格式。順便說一句,只有文字。我嘗試了兩種方法,其中錯誤地失敗了:在R中的文件夾中讀取多個本地html文件

##first approach 
library (tm) 
cname <- file.path("C:", "Users", "usuario", "Desktop", "DEADataset", "The Phillipines", "gazzetes.presihtml") 
    docs <- Corpus(DirSource(cname)) 
## second approach 
list_files_path<- list.files(path = './gazzetes.presihtml') 
a<- paste0(list_files_path, names) # vector names contain the names of the file with the .HTML extension 
rawHTML <- readLines(a) 

任何猜測?所有最好的

回答

1

你的第二種方法接近工作,除了readLines只接受一個連接,但你給它一個具有多個文件的向量。您可以使用lapplyreadLines來實現此目的。這裏是一個例子:

# generate vector of html files 
files <- c('/path/to/your/html/file1', '/path/to/your/html/file2') 

# readLines for each file and put them in a list 
lineList <- lapply(files, readLines) 

# create a character vector that contains all lines from all files 
lineVector <- unlist(lineList) 

# collapse the character vector into a single string 
html <- paste(lineVector , collapse = '\n') 

# print the string with original formatting 
cat(html) 
+0

謝謝!我完全忘記了對這類案件使用「lapply」。只是爲了記錄:一旦我使用貓,我得到「炸彈」(會議中止) –

+0

回覆:貓炸彈......可能超過了字符串字符大小的限制?當我測試這個時,我使用了兩個小的html文件 - 相對較短的字符串。 – jdbcode

+0

呵呵貓炸彈。你是對的。 –

相關問題