也许这是一个非常愚蠢的问题,但我想使用ajax调用并在R中使用结果。这是来自www.randomuser.me的调用:
$.ajax({
url: 'https://randomuser.me/api/?format=csv?results=5000',
dataType: 'json',
success: function(data) {
console.log(data);
}
});
那么保存csv的最简单方法是什么,以便我可以在R中使用它?这可能直接在R?我真的很感激低级答案(我不知道jQuery等是什么)
答案 0 :(得分:1)
使用httr
包并使其成为一个正确的API函数vs连接查询参数(这是超级错误):
library(httr)
random_user <- function(user_count=1,
gender=NULL, # NULL params don't end up passing values for those fields
password = NULL,
nationality = NULL,
include_fields = NULL,
exclude_fields = NULL,
seed = NULL) {
httr::GET(
url = "https://randomuser.me/api/",
query = list(
results = user_count,
gender = gender,
password = password,
seed = seed,
nat = nationality,
inc = include_fields,
exc = exclude_fields,
format = "csv"
)
) -> res
httr::stop_for_status(res)
res <- suppressWarnings(suppressMessages(httr::content(res, as="parsed")))
res
}
使用中:
x <- random_user(100)
glimpse(x)
## Observations: 100
## Variables: 25
## $ gender <chr> "female", "male", "male", "female", "female", "female", "male", "m...
## $ name.title <chr> "ms", "mr", "mr", "ms", "mrs", "ms", "mr", "mr", "mr", "ms", "mr",...
## $ name.first <chr> "josefa", "xenócrates", "nero", "elen", "yasemin", "ella", "robert...
## $ name.last <chr> "calvo", "da conceição", "barros", "teixeira", "erçetin", "thomsen...
## $ location.street <chr> "7854 calle mota", "9652 rua são jorge", "7557 rua quatro", "9576 ...
## $ location.city <chr> "parla", "francisco morato", "itabira", "petrópolis", "artvin", "a...
## $ location.state <chr> "andalucía", "bahia", "rondônia", "são paulo", "amasya", "syddanma...
## $ location.postcode <chr> "89193", "60020", "12989", "10994", "16543", "41488", "68794", "78...
## $ email <chr> "josefa.calvo@example.com", "xenócrates.daconceição@example.com", ...
## $ login.username <chr> "blueswan335", "whitemeercat215", "organicladybug700", "yellowduck...
## $ login.password <chr> "interacial", "siemens", "theater", "cupcake", "enrique", "caberne...
## $ login.salt <chr> "yMTLU5A4", "QWIGWlHA", "k7FifdOw", "kdTfS4wQ", "xQn4xxXJ", "saGW2...
## $ login.md5 <chr> "cd72739b62cf65bf98e9c100007cb15b", "cbec674fc38c97fc101b86a1d3a71...
## $ login.sha1 <chr> "5e039ceb350c4cdd3ebd46689b09d4619e286396", "8eeb0afb6f56a47f9de15...
## $ login.sha256 <chr> "81e0dc00e947a5909d80eefee32107300e2b9721160e3e2b84781392434d6234"...
## $ dob <dttm> 1961-03-22 03:03:07, 1975-04-17 11:04:20, 1974-09-27 01:32:22, 19...
## $ registered <dttm> 2013-05-07 12:16:02, 2002-11-29 18:23:46, 2011-01-12 03:11:21, 20...
## $ phone <chr> "970-460-274", "(40) 5146-2153", "(37) 0732-7652", "(93) 1003-7035...
## $ cell <chr> "674-010-743", "(36) 0114-6484", "(75) 8899-4318", "(04) 0678-4231...
## $ id.name <chr> "DNI", NA, NA, NA, NA, "CPR", "DNI", "DNI", "CPR", "NINO", "SSN", ...
## $ id.value <chr> "18765757-R", NA, NA, NA, NA, "388682-5305", "94865867-K", "517030...
## $ picture.large <chr> "https://randomuser.me/api/portraits/women/32.jpg", "https://rando...
## $ picture.medium <chr> "https://randomuser.me/api/portraits/med/women/32.jpg", "https://r...
## $ picture.thumbnail <chr> "https://randomuser.me/api/portraits/thumb/women/32.jpg", "https:/...
## $ nat <chr> "ES", "BR", "BR", "BR", "TR", "DK", "ES", "ES", "DK", "GB", "US", ...
x
## # A tibble: 100 x 25
## gender name.title name.first name.last location.street
## <chr> <chr> <chr> <chr> <chr>
## 1 female ms josefa calvo 7854 calle mota
## 2 male mr xenócrates da conceição 9652 rua são jorge
## 3 male mr nero barros 7557 rua quatro
## 4 female ms elen teixeira 9576 rua santa luzia
## 5 female mrs yasemin erçetin 9542 kushimoto sk
## 6 female ms ella thomsen 1099 nøddelunden
## 7 male mr roberto campos 1046 calle de ángel garcía
## 8 male mr hector hernandez 5523 paseo de zorrilla
## 9 male mr sander andersen 1625 sneppevej
## 10 female ms susanna anderson 8476 the avenue
## # ... with 90 more rows, and 20 more variables: location.city <chr>, location.state <chr>,
## # location.postcode <chr>, email <chr>, login.username <chr>, login.password <chr>,
## # login.salt <chr>, login.md5 <chr>, login.sha1 <chr>, login.sha256 <chr>, dob <dttm>,
## # registered <dttm>, phone <chr>, cell <chr>, id.name <chr>, id.value <chr>,
## # picture.large <chr>, picture.medium <chr>, picture.thumbnail <chr>, nat <chr>
答案 1 :(得分:0)
使用rjson
包查询该URL并将返回的JSON数据解析为R列表:
> library(rjson)
> url = "https://randomuser.me/api/?format=csv?results=5000"
> data = rjson::fromJSON(file=url)
> str(data)
List of 2
$ results:List of 1
..$ :List of 12
.. ..$ gender : chr "female"
.. ..$ name :List of 3
请注意,此处忽略格式和结果参数,因为您的网址查询字符串为format=csv?results=5000
,您应将参数与&
分开,而不是?
。
如果您想从CSV来源阅读,修复网址,然后从网址中读取:
> url2 = "https://randomuser.me/api/?format=csv&results=5"
> d = read.csv(url2)
> dim(d)
[1] 5 25
> names(d)
[1] "gender" "name.title" "name.first"
[4] "name.last" "location.street" "location.city"
[7] "location.state" "location.postcode" "email"
[10] "login.username" "login.password" "login.salt"
[13] "login.md5" "login.sha1" "login.sha256"
[16] "dob" "registered" "phone"
[19] "cell" "id.name" "id.value"
[22] "picture.large" "picture.medium" "picture.thumbnail"
[25] "nat"
>