【发布时间】:2019-10-29 15:40:06
【问题描述】:
我想从 rtf 文件中的文件夹中解析 rtf 文件,该文件夹在 lapply 步骤期间导致错误。
我刚开始使用trycatch,那么如何将它合并到我的代码中(lapply 步骤)以忽略错误并继续解析下一个 rtf 文件?
【问题讨论】:
标签: r text-mining
我想从 rtf 文件中的文件夹中解析 rtf 文件,该文件夹在 lapply 步骤期间导致错误。
我刚开始使用trycatch,那么如何将它合并到我的代码中(lapply 步骤)以忽略错误并继续解析下一个 rtf 文件?
【问题讨论】:
标签: r text-mining
这对你有用吗?
yourFunction <- function(x) {
rtf <- read_rtf(x, verbose = FALSE, row_start = "*| ", row_end = "",
cell_end = " | ", ignore_tables = FALSE, check_file = TRUE)
text <- unlist(strsplit(rtf, "\\."))
toMatch <- c("bitcoin", "fund")
matches <- unique(grep(paste(toMatch,collapse="|"),
text, value=TRUE))
matches <- data.frame(matches)
}
results = lapply(files, function(x){
tryCatch(yourFunction(x),
error = function(e)print(paste(x, 'did not want')),
finally = 0)})
【讨论】:
function(e)-部分。现在应该可以工作了。
tryCatch-help-page 有点没用,你可以看看this,它解释得比我自己做的更好。
这是怎么回事?
foo <- function(x) tryCatch(yourFunction(x), error = function(e) e)
lapply(files, foo)
【讨论】: