Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R在URL内粘贴列表_R_List_Api_Paste_Httr - Fatal编程技术网

使用R在URL内粘贴列表

使用R在URL内粘贴列表,r,list,api,paste,httr,R,List,Api,Paste,Httr,我正在玩一些API,有一个简单的问题。如何在下面的URL中的=之后粘贴逗号分隔的列表,而不是手动写入所有内容 library(httr) X <- GET("url/?query=") 谢谢 更新 我看起来像: > dput(L) list("a","b","c","d") 您可以使用2个粘贴: 这是一种构建/传递查询字符串的更安全、更理智的方法: library(httr) res <- GET(url = "http://httpbin.org/get",

我正在玩一些API,有一个简单的问题。如何在下面的URL中的=之后粘贴逗号分隔的列表,而不是手动写入所有内容

library(httr)

X <- GET("url/?query=")
谢谢

更新 我看起来像:

> dput(L)
list("a","b","c","d")
您可以使用2个粘贴:


这是一种构建/传递查询字符串的更安全、更理智的方法:

library(httr)

res <- GET(url = "http://httpbin.org/get",
           query = list(
             query = paste0(list("a","b","c","d"), collapse=",")
           ))

str(content(res, as="parsed"))
## List of 4
##  $ args   :List of 1
##   ..$ query: chr "a,b,c,d"
##  $ headers:List of 5
##   ..$ Accept         : chr "application/json, text/xml, application/xml, */*"
##   ..$ Accept-Encoding: chr "gzip, deflate"
##   ..$ Connection     : chr "close"
##   ..$ Host           : chr "httpbin.org"
##   ..$ User-Agent     : chr "libcurl/7.51.0 r-curl/2.3 httr/1.2.1"
##  $ origin : chr "50.252.233.22"
##  $ url    : chr "http://httpbin.org/get?query=a%2Cb%2Cc%2Cd"

为什么不使用内置方式修改查询字符串?我不知道这意味着什么?很抱歉,我对R和编码非常陌生。
url = paste("url/?query=",paste(L,collapse=","),sep="")
X <- GET(url)
library(httr)

res <- GET(url = "http://httpbin.org/get",
           query = list(
             query = paste0(list("a","b","c","d"), collapse=",")
           ))

str(content(res, as="parsed"))
## List of 4
##  $ args   :List of 1
##   ..$ query: chr "a,b,c,d"
##  $ headers:List of 5
##   ..$ Accept         : chr "application/json, text/xml, application/xml, */*"
##   ..$ Accept-Encoding: chr "gzip, deflate"
##   ..$ Connection     : chr "close"
##   ..$ Host           : chr "httpbin.org"
##   ..$ User-Agent     : chr "libcurl/7.51.0 r-curl/2.3 httr/1.2.1"
##  $ origin : chr "50.252.233.22"
##  $ url    : chr "http://httpbin.org/get?query=a%2Cb%2Cc%2Cd"