首页 > 解决方案 > 在 Google 趋势请求循环中跳过错误

问题描述

对于我的学士论文,我需要为不同国家的几个品牌提取谷歌趋势数据。由于我对 R 完全陌生,我的一个朋友帮我创建了循环代码,它会自动执行。过了一会儿,错误 data must be a data frame, or other object coercible by fortify(), not a list 出现并且循环停止。当检查谷歌趋势页面本身时,我发现没有足够的数据来支持请求。

我现在的问题是,是否可以继续循环,而不管错误如何,只是“跳过”导致错误的请求。

我已经在其他线程中环顾四周,但try()似乎在这里不起作用,或者我做错了。我low_search_volume = FALSE也将默认设置更改为TRUE,但这并没有改变任何东西。

for (row in 1:nrow(my_data)) {
    country_code <- as.character(my_data[row, "Country_Code"])
    query <- as.character(my_data[row, "Brand"])
    trend <- gtrends(
      c(query),
      geo = country_code,
      category = 68,
      low_search_volume = TRUE,
      time = "all"
      )
    plot(trend)
    export <- trend[["interest_over_time"]]
    filepath <- paste(
      "C:\\Users\\konst\\Desktop\\Uni\\Bachelorarbeit\\R\\Ganzer Datensatz\\",
      query, "_", country_code,
      ".csv",
      sep = ""
       )
    write.csv(export, filepath)
  }

要重现错误,请使用以下列表:

Brand       Country Code
Gucci       MA
Gucci       US
allsaints   MA
allsaints   US

allsaints MA请求应产生错误。因此,allsaints US遗嘱不予处理。

预先感谢大家的帮助。

来自德国汉堡的美好祝愿

标签: rgtrendsr

解决方案


推荐阅读