Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match, when using Rinat package in R

Viewed 49

I am using the Rinat API to extract the observations submitted by a list of users to the iNaturalist app. The API function I'm using is as below, which is taken from GitHub (https://github.com/ropensci/rinat/blob/master/R/get_inat_obs_user.R):

get_inat_obs_user <- function(username, maxresults = 10000){
  
  # check Internet connection
  if (!curl::has_internet()) {
    message("No Internet connection.")
    return(invisible(NULL))
  }
  
  base_url <- "http://www.inaturalist.org/"
  # check that iNat can be reached
  if (httr::http_error(base_url)) { # TRUE: 400 or above
    message("iNaturalist API is unavailable.")
    return(invisible(NULL))
  }
  
  q_path <- paste0(username, ".csv")
  ping_path <- paste0(username, ".json")
  
  ping_query <- "&per_page=1&page=1"
  ### Make the first ping to the server to get the number of results
  ### easier to pull down if you make the query in json, but easier to arrange results
  ### that come down in CSV format
  ping <-  GET(base_url, path = paste0("observations/", ping_path), query = ping_query)
  total_res <- as.numeric(ping$headers$`x-total-entries`)
  
  if(total_res == 0){
    stop("Your search returned zero results. Perhaps your user does not exist.")
  }
  page_query <-"&per_page=200&page=1"
  dat <-  GET(base_url, path = paste0("observations/", q_path), query = page_query)
  data_out <- read.csv(textConnection(content(dat, as = "text")))
  if(maxresults > 200){
    for(i in 2:ceiling(total_res/200)){
      page_query <- paste0("&per_page=200&page=", i)
      dat <-  GET(base_url, path = paste0("observations/", q_path), query = page_query)
      data_out <- rbind(data_out, read.csv(textConnection(content(dat, as = "text"))))
    }
    
  }
  
  if(maxresults < dim(data_out)[1]){
    data_out <- data_out[1:maxresults,]
  }
  
  return(data_out)
}

I'm also using a for loop to iterate the API function through a list of usernames (in below sample data):

usernames<-

    user_login
1   a_random_kaiser
2   a_wade
3   achaas33
4   adam247
5   adam294
6   ailsa2
7   anne278
8   anne370
9   apwiemer
10  arbw
11  aslan1758
12  asmalley
13  aspinwall
14  atypicalhermit
15  aurora143
16  autumn307
17  avenkal
18  awburck
19  badbotanist
20  baleinenature
21  barich
22  batesfire
23  beavers
24  becca57
25  beesis
26  beetlequeenrs
27  ben543
28  ben566
29  bethanyk63
30  betty108
31  boatbirder
32  bobbyboy
33  bobkat
34  brad131987
35  bradders100
36  cadds
37  caketea86
38  carluk
39  carolina711
40  cleo2211
41  cobydo
42  cocothecat

EDIT

sequence <- seq_along(usernames)  
User_obs_global <- data.frame()  

for (var in sequence){
  User_obs <- get_inat_obs_user(as.character(
    User_no_obs_Oxfordshire$user_login[var])) 
  
  User_obs_global <- rbind(User_obs_global, User_obs)
}

sequence = 1:nrow(usernames)
for (var in sequence)
  {User_obs<- get_inat_obs_user(as.character(username$user_login[var]))}

However, I keep getting the below error:

Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match

I tried searching previous Stack Overflow posts related to this error, and understand this could be due to mismatch in number of columns between successive username records. Nevertheless, the number of columns should be the same for all username records, since their source is the same. Hence, I'm wondering if there is something wrong with my code.

I can run the API function successfully for individual usernames by the way, but I have a few thousand usernames to loop through.

0 Answers
Related