"Error in parse_url(url) : length(url) == 1 is not TRUE" 当我使用 sapply

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

我创建了一个调用 API 的函数,但我遇到了长度 (url) 的问题。我将我的代码简化如下以显示问题:


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)

我有下一个错误: Error in parse_url(url) : length(url) == 1 is not TRUE 追溯是这样的:

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) 


如何更改我的函数“调用”以应用于我的 df?谢谢!

这里,我们可能需要提取'names'列,然后使用

sapply(df$names, calling)