Home > OS >  Using API to load more than one question ids per call
Using API to load more than one question ids per call

Time:04-11

Using a dataframe with question ids from SO:

df <- data.frame (qid = c(71663375, 71674701, 71724524))

Using the folliwng code it is possible to make 3 api calls to receive info for every question.

However SO app provides the chance to run into 1 call 100 ids. How is it possible to make it?

library(stackr)
for (j in 1:nrow(df)) {
  
  questions <- stack_questions(df$qid[j])
}

CodePudding user response:

If the app can handles 100 at a time, split the qid into list of vectors with each list element holding 100 at a time,

lst1 <- split(df$qid, as.integer(gl(nrow(df), 100, nrow(df))))

and then use lapply

library(stackr)
out <- lapply(lst1, stack_questions)
out_dat <- do.call(rbind, out)

or for loop to loop over the list

out <- vector('list', length(lst1))
for(i in seq_along(lst1)) {
   out[[i]] <- stack_questions(lst1[[i]])
}
out_dat <- do.call(rbind, out)

-output

> out
[[1]]
              tags is_answered view_count accepted_answer_id answer_count score  last_activity_date       creation_date      last_edit_date question_id content_license
1                r        TRUE         32           71724636            1     0 2022-04-03 04:32:10 2022-04-03 04:15:43 2022-04-03 04:23:59    71724524    CC BY-SA 4.0
2                r        TRUE         19           71674900            1     0 2022-03-30 04:38:41 2022-03-30 04:25:06 2022-03-30 04:32:58    71674701    CC BY-SA 4.0
3 sql,dataexplorer       FALSE         27                 NA            1     0 2022-03-29 09:18:20 2022-03-29 08:54:52 2022-03-29 09:00:36    71663375    CC BY-SA 4.0
                                                                                                                          link                                                                           title
1                                            https://stackoverflow.com/questions/71724524/melt-a-dataframe-using-a-list-column                                            Melt a dataframe using a list column
2 https://stackoverflow.com/questions/71674701/create-a-new-column-using-detecting-the-domain-of-a-url-from-an-existing-column Create a new column using detecting the domain of a url from an existing column
3                                     https://stackoverflow.com/questions/71663375/paginate-pages-to-receive-results-from-tsql                                     Paginate pages to receive results from tSQL
  owner_account_id owner_reputation owner_user_id owner_user_type                                                                     owner_profile_image owner_display_name
1         24733596                5      18621268      registered https://lh3.googleusercontent.com/a/AATXAJwQRtIYRrvKJi1a4AfvTHoE4ht8f_WQ1Qv3jtbr=k-s256            Domin D
2         24733596                5      18621268      registered https://lh3.googleusercontent.com/a/AATXAJwQRtIYRrvKJi1a4AfvTHoE4ht8f_WQ1Qv3jtbr=k-s256            Domin D
3         24733596                5      18621268      registered https://lh3.googleusercontent.com/a/AATXAJwQRtIYRrvKJi1a4AfvTHoE4ht8f_WQ1Qv3jtbr=k-s256            Domin D
                                        owner_link
1 https://stackoverflow.com/users/18621268/domin-d
2 https://stackoverflow.com/users/18621268/domin-d
3 https://stackoverflow.com/users/18621268/domin-d

data

df <- data.frame (qid = c(71663375, 71674701, 71724524))
  • Related