【问题标题】:What is the error `vec_assign()`: `value` should have been recycled to fit `x`. trying to tell me`vec_assign()` 的错误是什么:`value` 应该被回收以适应 `x`。试图告诉我
【发布时间】:2020-08-29 13:38:38
【问题描述】:

我有一个功能可以进行一些基本的网络采集。成功登录后调用此函数。 (网站已被屏蔽 xxxxxx)

搜索功能:

search <-function(HorseList){
  url <- "http://tnetwork.xxxxxx.com/tnet/HorseSearch.aspx"
  s <- GET(url)
  xxxxxx <- tibble(
    horse_name = character(),
    race_date = character(),
    race_nbr = character(),
    trk = character(),
    peak = character(),
    dist_run = character()
  )
  for (row in 1:nrow(HorseList))
  {
    
    url <-paste(c('http://tnetwork.xxxxxx.com/tnet/HorseSearchAPI.aspx?HorseName=',toString(HorseList[[row, 1]])),collapse='')
    #print(url)
    h <- GET(url)
    temp<-content(h, "text")
    doc <- htmlParse(temp)
    horse_name <- HorseList[[row,1]]
    horse_ID <-xpathSApply(doc,"//*[@id=\"resultsDiv\"]/p[1]/a/@href")
    horse_ID <-substr(horse_ID,27,40)
    h_list <- list()
    c <- nchar(horse_ID)
    if (length(c)>0)
    {
      h_list[1] <- horse_ID
    }
    
    
    id_count <- length(h_list)
    
    for (k in 1:id_count)
    {
      url <-paste(c('http://tnetwork.xxxxxx.com/tnet/t_PastPerf.aspx?HorseID=',toString(h_list[k])),collapse='')
      t <- GET(url)
      temp <- content(t, "text")
      pastperf <- htmlParse(temp)
      row_count<-length(xpathSApply(pastperf,"//*[@id=\"pastPerfTable\"]/tr"))
      
      for(j in 2:row_count)
      {
        j<- toString(j)
        race_data <- xpathSApply(pastperf,paste("//*[@id=\"pastPerfTable\"]/tr[",j,"]/td[1][1]"),xmlValue)
        race_date <- substr(race_data,1,10)
        race_number <-trimws(substr(race_data,12,100))
        horse_name <- URLdecode(toString(horse_name))
        race_nbr = str_match(race_number,'(Race\\s\\d+)(.*)')[,2]
        trk = str_match(race_number,'(Race\\s\\d+)(.*)')[,3]
        peak <-xpathSApply(pastperf,paste("//*[@id=\"pastPerfTable\"]/tr[",j,"]/td[13]"),xmlValue)
        cum_distance <-xpathSApply(pastperf,paste("//*[@id=\"pastPerfTable\"]/tr[",j,"]/td[14]"),xmlValue)
        newrow <- paste(horse_name,',',race_date,',',race_nbr,',',trk, ',',peak,',',cum_distance)
        xxxxxx <- add_row(trakus, horse_name = horse_name, race_date = race_date, race_nbr = race_nbr, trk=trk, peak = peak, dist_run = cum_distance)
      }
    }
  }
  
  return(xxxxxx)
}

该函数过去运行成功,但今天抛出以下错误:

Error: Internal error in `vec_assign()`: `value` should have been recycled to fit `x`.

我运行了 rlang::last_error() 和 last_trace() 命令来获得一些额外的见解,但我仍然不确定发生了什么。

> rlang::last_error()
<error/rlang_error>
Internal error in `vec_assign()`: `value` should have been recycled to fit `x`.
Backtrace:
 1. base::source("~/TimeForm/Scripts/past_perf.R", echo = TRUE)
 6. global::search(horse_list) ~/TimeForm/Scripts/past_perf.R:627:2
 7. tibble::add_row(...) ~/TimeForm/Scripts/past_perf.R:85:8
 8. tibble:::rbind_at(.data, df, pos)
 9. vctrs::vec_rbind(old, new)
Run `rlang::last_trace()` to see the full context.

> rlang::last_trace()
<error/rlang_error>
Internal error in `vec_assign()`: `value` should have been recycled to fit `x`.
Backtrace:
     x
  1. +-base::source("~/TimeForm/Scripts/past_perf.R", echo = TRUE)
  2. +-base::source("~/TimeForm/Scripts/past_perf.R", echo = TRUE)
  3. | +-base::withVisible(eval(ei, envir))
  4. | \-base::eval(ei, envir)
  5. |   \-base::eval(ei, envir)
  6. \-global::search(horse_list) ~/TimeForm/Scripts/past_perf.R:627:2
  7.   \-tibble::add_row(...) ~/TimeForm/Scripts/past_perf.R:85:8
  8.     \-tibble:::rbind_at(.data, df, pos)
  9.       \-vctrs::vec_rbind(old, new)
 10.         \-(function () ...

似乎我的代码中的 add_row() 行可能是罪魁祸首,但我不确定错误说明了什么或如何修复它。有没有人可以分享任何见解?

【问题讨论】:

    标签: r tidyverse tibble


    【解决方案1】:

    我发现问题出现在处理空字符字段、空值或使用NA。该问题已通过 map_if 替换空值得到纠正。

        map_depth(.depth = 1, map_if, is_empty, ~paste0(""))
    

    当然,您需要调整深度命令,以便在适合您的列表构造的级别进行更正。

    理想情况下,bind_rows() 会更稳健地处理包含 NA 或 NULL 值的向量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 2023-04-05
      • 2022-06-11
      相关资源
      最近更新 更多