Search code examples
rapisapply

"Error in parse_url(url) : length(url) == 1 is not TRUE" when I use sapply


I've created a function to call an API, but I have a problem with length(url). I simplified my code as follow to show the problem:


library(jsonlite)
library(httr)

df<-data.frame("names"=c("20523717759","20555589574"))
calling<- function(x){
  url1<-paste("https://api.sunat.cloud/ruc/",x,sep = "", collapse = NULL)
  url1
  res<- GET(url1)
  }

sapply(df,calling)

I have the next error: Error in parse_url(url) : length(url) == 1 is not TRUE The traceback is this:

13.
stop(simpleError(msg, call = if (p <- sys.parent(1L)) sys.call(p))) 
12.
stopifnot(length(url) == 1) 
11.
parse_url(url) 
10.
is.url(url) 
9.
stopifnot(is.url(url)) 
8.
build_url(parse_url(url)[c("scheme", "hostname", "port")]) 
7.
handle_name(url) 
6.
handle_find(url) 
5.
handle_url(handle, url, ...) 
4.
GET(url1) 
3.
FUN(X[[i]], ...) 
2.
lapply(X = X, FUN = FUN, ...) 
1.
sapply(df, calling) 


How can I change my function "calling" to apply to my df ? Thanks!


Solution

  • Here, we may need to extracting the 'names' column and then use

    sapply(df$names, calling)