使用tm.plugin.factiva-package,我想创建一个可以读取Factiva-html文件并将其作为数据帧返回的函数。到目前为止,我已经设法创建了一个可以读取这些文件并将其转换为数据帧列表的函数,每个df对应于一个html文件。但是,我很难找到一种将它们合并为一个df的方法,因为tidy()
函数将某些列作为列表而不是字符向量返回。使用bind_rows()
f.ex.返回错误Error: Column `company` can't be converted from list to character
。从理论上讲,这是有问题的所有列都可以选择,因为我对它们没有真正的需求,但是鉴于例如,我无法找到一种方法对整个列表进行处理select()
无法处理列表(而我本人还是很陌生的。)
现在的功能如下:
Factiva_Reader <- function(File_Path){
pac <- c("tidyverse", "tidytext", "tm", "tm.plugin.factiva")
sapply(pac, require, character.only = TRUE) #Loading required packages,
Filer <- list.files(File_Path)
Filer <- str_extract(Filer, "\\w*\\.html")
Filer <- Filer[!is.na(Filer)] #Creates a list of all valid files in folder
Data <- sapply(Filer, FactivaSource)
Data <- sapply(Data, Corpus, readerControl = list(language = NA))
Data <- lapply(Data, tidy)
}
由于数据在技术上是受版权保护的,因此我对于如何显示数据还不确定,但是以奇怪的格式,这至少应该显示结构。到目前为止,该函数会为文件夹中的每个文件返回一个包含df的列表,该列表采用这种格式:
$ Factiva3.html: tibble [100 x 20] (S3: tbl_df/tbl/data.frame)
..$ author : chr [1:100] ...
..$ datetimestamp: POSIXct[1:100], format: ...
..$ description : logi [1:100] NULL ...
..$ heading : chr [1:100] ...
..$ id : chr [1:100] ...
..$ language : chr [1:100] ...
..$ origin : chr [1:100] ...
..$ edition : chr [1:100] ...
..$ section : chr [1:100] ...
..$ subject :List of 100
..$ coverage :List of 100
..$ company : chr [1:100] ...
..$ industry :List of 100
..$ infocode :List of 100
..$ infodesc :List of 100
..$ page : chr [1:100] ...
..$ wordcount : int [1:100] NULL ...
..$ publisher : chr [1:100] ...
..$ rights : chr [1:100] ...
..$ text : Named chr [1:100] ...
.. ..- attr(*, "names")= chr [1:100] ...
答案 0 :(得分:0)
对那些感兴趣的人迟到的回应。
以下使用 purrr::map
的代码应返回所需的 tibble。
files_list <- list.files(Files_Path)
is_html_file <- str_extract(files_list, "\\w*\\.html")
html_files <- is_html_file[!is.na(is_html_file)] #Creates a list of all valid files in folder
html_files_path <- str_c(Files_Path, html_files)
html_files_path %>%
map(FactivaSource) %>%
map(Corpus, readerControl = list(language = NA)) %>%
map_dfr(tidy)