【问题标题】:Skip the error and go through with the loop to get the output跳过错误并执行循环以获取输出
【发布时间】:2018-03-07 12:32:10
【问题描述】:

当我尝试在以下代码中运行时,我收到错误消息“as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) 中的错误:无法强制类”“try-错误"" 到 data.frame "

我正在使用 try 函数跳过不工作的 LINKS 并继续循环,但这没有发生。有人可以帮我解决这个问题吗

base_url <- c("https://www.sec.gov/Archives/edgar/data/1409916/000162828017002570/exhibit211nobilishealthcor.htm",
              "https://www.sec.gov/Archives/edgar/data/1300317/000119312507128181/dex211.htm",
              "https://www.sec.gov/Archives/edgar/data/1453814/000145381417000063/subsidiariesoftheregistran.htm",
              "https://www.sec.gov/Archives/edgar/data/25743/000138713117001111/ex21-1.htm",
              "https://www.sec.gov/Archives/edgar/data/880631/000119312517065534/d280058dex211.htm",
              "https://www.sec.gov/Archives/edgar/data/1058290/000105829017000008/ctshexhibit21112312016.htm",
              "https://www.sec.gov/Archives/edgar/data/1031927/000141588916005383/ex21-1.htm",
              "https://www.sec.gov/Archives/edgar/data/1358071/000135807118000008/cxoexhibit211.htm",
              "https://www.sec.gov/Archives/edgar/data/904979/000090497918000006/exhibit211q4fy17listofsubs.htm",
              "https://www.sec.gov/Archives/edgar/data/41296/000094420901500099/dex21.txt",
              "https://www.sec.gov/Archives/edgar/data/808461/000080846117000024/gciexhibit21-1123116.htm",
              "https://www.sec.gov/Archives/edgar/data/1101026/000107878213000519/f10k123112_ex21.htm",
              "https://www.sec.gov/Archives/edgar/data/932372/000141588915000759/ex21-1.htm"
              )

  df <- lapply(base_url,function(u){
  try({

  html_obj <- read_html(u)
  draft_table <- html_nodes(html_obj,'table')
  cik <- substr(u,start = 41,stop = 47)
  draft1 <- html_table(draft_table,fill = TRUE)
  final <- c(cik,draft1)
  },silent = TRUE)
})


require(reshape2)
data <- melt(df)
data <- as.data.frame(data,row.names = NULL)
data <- data[,1:2]
names(data) <- c("CIK","Company")

data2 <- transform(data, CIK = na.locf(CIK ))

【问题讨论】:

  • 尝试使用 tryCatch 并在出错时返回例如空数据框或 null。你的melt 也不起作用,我猜你必须先dplyr::bind_rows()
  • 你能帮我写代码吗?

标签: r


【解决方案1】:

您可以使用 purrr 的 safely 函数。它为每个 url 创建一个列表,其中包含来自以下函数的结果以及错误消息(如果存在但不退出循环)。

library(tidyverse)

checklinks <- function(url) {
  cik <- url %>% 
    str_extract("[:digit:]+")
  table <- read_html(url) %>% 
    html_nodes("table") %>%
    html_table() %>% 
    bind_rows() %>% 
    na_if("") %>% 
    filter(rowMeans(is.na(.)) < 1) %>% 
    mutate(cik = cik) %>% 
    select(cik, everything())
  return(table)
}

final <- base_url %>% 
  map(safely(checklinks)) %>% 
  transpose() %>% 
  .$result %>% 
  bind_rows() 

【讨论】:

  • 在'links_t'中获得输出后,我应该如何将输出放入数据框中?因为如果我使用下面的代码,它会抛出错误: require(reshape2) data
  • 我想在数据框中得到输出
  • 你能帮我把数据放入数据框吗?
  • 我已经编辑了上面的代码以帮助您将数据放入数据框中。但是,考虑到您提供的网站,这并不是一件容易的事。它是不同表的混合,甚至是一个 txt 文件。上面的代码适用于除两个网址之外的所有网址
【解决方案2】:

try 不会让你跳过,而是在出现问题时返回 try-error 类的错误。

所以之后,您仍然可以添加如下内容:

check <- sapply(df, class) != "try-error"
df <- df[check]

或直接使用tryCatch

df <- lapply(base_url, function(u) {
  tryCatch({
    html_obj <- read_html(u)
    draft_table <- html_nodes(html_obj,'table')
    cik <- substr(u,start = 41,stop = 47)
    draft1 <- html_table(draft_table,fill = TRUE)
    final <- c(cik,draft1)
  }, error = function(x) NULL)
})

【讨论】:

  • 有没有其他方法可以做到这一点。我想继续循环以从站点抓取数据,而不是抛出错误并且循环停止。 ?
  • 我不确定你的意思,像这样不应该抛出错误并且循环应该继续。我添加了tryCatch 的替代项,但是您仍然拥有NULLs,您可能需要根据您想要继续的方式删除它
  • 它抛出一个错误:名称(对象)中的错误
  • 现在到底在哪里?到目前为止,您是如何修改代码的?
【解决方案3】:

你可以试试这样的。

for(i in something)
{
  res <- try(expression_to_get_data)
  if(inherits(res, "try-error"))
  {
    #error handling code, maybe just skip this iteration using
    continue
  }
  #rest of iteration for case of no error
}

Source of solution

【讨论】:

    猜你喜欢
    • 2016-05-05
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2018-06-18
    • 1970-01-01
    相关资源
    最近更新 更多