【问题标题】:break condition in if condition in rr中的if条件中的中断条件
【发布时间】:2017-09-22 10:28:07
【问题描述】:

我正在尝试从我从 OCR 格式的 pdf 文件中提取的文本中搜索一个单词。这个 pdf 文件有多个页面,所以对于每一页我都在搜索一个单词,如果找到了那个单词,那么我不希望 for 循环继续,我使用了代码,但它只是停在第一页。我在这段代码中缺少什么。 这是代码

for(i in 1:8){
  img_file <- pdftools::pdf_convert("D:/Files_OCR/test.pdf", format = 'tiff', pages = i, dpi = 400)
  text <- ocr(img_file)
  ocr_text <- capture.output(cat(text))
  check=sapply(ocr_text, paste0, collapse="")
  if(length(which(stri_detect_fixed(tolower(check),tolower("school")))) <= 0){ print("Not Present") } else {print("Present")}
  if(br=="present")
break

}

任何建议都是可以理解的。

谢谢

【问题讨论】:

  • 这适用于以下示例:for(i in ssss) {if(i == "helo") break; print(i)}.

标签: r for-loop if-statement dataframe break


【解决方案1】:

stopifnot 是函数 - 将它与 grepl 结合应该可以帮助您打破循环

> ssss <- c('hi','helo','confusion','india')
> ssss
[1] "hi"        "helo"      "confusion" "india"    
> for(n in ssss){stopifnot(grepl('confusion',n)); print(n)}
Error: grepl("confusion", n) is not TRUE
> ssss[1] <- 'confusion'
> for(n in ssss){stopifnot(grepl('confusion',n)); print(n)}
[1] "confusion"
Error: grepl("confusion", n) is not TRUE
> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2022-06-21
    • 2020-11-19
    • 1970-01-01
    • 2023-02-16
    • 2016-11-24
    相关资源
    最近更新 更多