【问题标题】:Trouble with for looping column values into an API call将列值循环到 API 调用中的问题
【发布时间】:2021-12-14 20:15:35
【问题描述】:

我正在做一个项目,我正在尝试获取一个只有一列包含单词的数据框,然后尝试构建一个循环,该循环将从 Merriam-Webster API 中提取单词的一些数据,然后放置它进入一个新的数据框。

希望最终的数据框如下所示:

name | class | pronunciation | definition
ambit  noun    am*bit          circuit, compass

到目前为止我的代码:

words_for_loop 数据帧

diurnal  
omnibus 
chatelaine 
mantic 
limitrophe 
oaf

代码:

for (i in seq_along(words_for_loop)) {

url_word_vector <- paste("https://www.dictionaryapi.com/api/v3/references/collegiate/json/", words_for_loop[[i]], "?key=numbers", sep = "")
  

data_holder <- tibble()

data_loop <- fromJSON(url_word_vector, flatten = TRUE) %>% 
  select(meta.id, fl, hwi.hw, shortdef) %>% 
  dplyr::rename(name = meta.id, class = fl, pronunciation = hwi.hw, definition = shortdef)  
   

data_holder <-
  data_holder %>%
  bind_rows(data_loop)

}

结果是一个空的小标题。我认为我在正确的轨道上,因为运行url_word_vector 的结果是:

[1] "https://www.dictionaryapi.com/api/v3/references/collegiate/json/diurnal?key=99820690-b2a0-4aa3-902a-f54c7fa3c685"   
[2] "https://www.dictionaryapi.com/api/v3/references/collegiate/json/omnibus?key=99820690-b2a0-4aa3-902a-f54c7fa3c685"   
[3] "https://www.dictionaryapi.com/api/v3/references/collegiate/json/chatelaine?key=99820690-b2a0-4aa3-902a-f54c7fa3c685"
[4] "https://www.dictionaryapi.com/api/v3/references/collegiate/json/mantic?key=99820690-b2a0-4aa3-902a-f54c7fa3c685"    
[5] "https://www.dictionaryapi.com/api/v3/references/collegiate/json/limitrophe?key=99820690-b2a0-4aa3-902a-f54c7fa3c685"
[6] "https://www.dictionaryapi.com/api/v3/references/collegiate/json/oaf?key=99820690-b2a0-4aa3-902a-f54c7fa3c685" 

我只是不知道如何让它循环这些单独的调用。我需要在循环内嵌套循环吗?只是在寻找下一步。谢谢!

【问题讨论】:

  • (1) 您在循环中使用words_for_loop[[1]]。你拼错了words_for_loop[[i]]吗? (2) 在循环中定义 data_holder &lt;- tibble()。这会导致 data_holder 在循环的每次迭代中被清空。我想你把这个移到外面循环。 (3) 我想你想在rename 函数之后直接传递bind_rows 部分并将其直接分配给data_holder。在这种情况下,您不需要data_loop
  • 至 (1):我的错误。我希望words_for_loop 是一个向量。
  • 嘿马丁,[[1]] 应该是 [[i]]

标签: r loops jsonlite


【解决方案1】:

我认为最好将所有结果放在一个列表中并在最后rbind 它们。

library(dplyr)
library(jsonlite)

words_for_loop <- data.frame(V1 = c("diurnal", "omnibus", "chatelaine", 
                                    "mantic", "limitrophe", "oaf"))

home <- "https://www.dictionaryapi.com/api/v3/references/collegiate/json/"

key <- "my-api-key"

url_word_vector <- paste0(home, words_for_loop[[1]], "?key=", key)

do.call(rbind, lapply(url_word_vector, function(url) {
  fromJSON(url, flatten = TRUE) %>% 
    select(meta.id, fl, hwi.hw, shortdef) %>% 
    rename(name = meta.id, class = fl, pronunciation = hwi.hw, definition = shortdef) %>%
    as_tibble()
}))
#> # A tibble: 11 x 4
#>    name              class                     pronunciation          definition
#>    <chr>             <chr>                     <chr>                  <list>    
#>  1 diurnal:1         adjective                 di*ur*nal              <chr [3]> 
#>  2 diurnal:2         noun                      diurnal                <chr [2]> 
#>  3 omnibus:1         noun                      om*ni*bus              <chr [2]> 
#>  4 omnibus:2         adjective                 omnibus                <chr [2]> 
#>  5 justitia omnibus~ Latin phrase              jus*ti*tia om*ni*bus   <chr [1]> 
#>  6 quod semper, quo~ Latin quotation from {i_~ quod sem*per, quod ub~ <chr [1]> 
#>  7 the man on the C~ noun phrase               the man on the Clapha~ <chr [1]> 
#>  8 chatelaine        noun                      chat*e*laine           <chr [3]> 
#>  9 mantic            adjective                 man*tic                <chr [1]> 
#> 10 limitrophe        adjective                 lim*i*trophe           <chr [1]> 
#> 11 oaf               noun                      oaf                    <chr [2]>

reprex package (v2.0.0) 于 2021 年 10 月 29 日创建

【讨论】:

  • 嗨艾伦,非常感谢您的回复。我尝试了您提供的框架,但遇到了障碍。我创建了一个函数dictionary_reader,但是当我使用url_word_vector 作为参数时,我收到以下错误:Error in match.fun(FUN) : 'dictionary_reader(url_word_vector)' is not a function, character or symbol 我想我误解了你的建议。你能帮我澄清你的意图吗?谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 2018-11-05
  • 2015-09-10
相关资源
最近更新 更多