R:通过循环输出API获得的环境

时间:2019-03-06 17:29:48

标签: r api loops

我在R中使用NamSor API来处理名称并推断某些人口统计信息。我的以下代码运行良好,并指出了名称的性别:

### NamSor Onomastics

# Only need the first three lines if namsor api and package not installed yet

# install.packages("devtools")
# library(devtools)
# install_github("wing328/namsor-r-client")

#______________________________________________________________________________#
library(namsor)

var.first.name <- 'John' 
var.last.name <- 'Smith'
api.instance <- PersonalApi$new()
api.instance$apiClient$apiKeys['X-API-KEY'] <- 'THE-API-KEY-HERE';
result <- api.instance$Gender(var.first.name, var.last.name)
dput(result)

unlist(mget(c('firstName','lastName','likelyGender', 'genderScale', 'id', 'score'), envir = result))

#______________________________________________________________________________#

但是,当我尝试将其编写为一个简单的循环时,似乎我的unlist(mget(...))命令被忽略了,因此无法处理大量数据。上面的代码是一个简单的循环:

### NamSor Onomastics

# Only need the first three lines if namsor api and package not installed yet

# install.packages("devtools")
# library(devtools)
# install_github("wing328/namsor-r-client")

#______________________________________________________________________________
library(namsor)

first_names <- as.vector(c('Tim', 'John', 'Jane'))
last_names <- as.vector(c('Walter', 'Smith', 'Doe'))
for (first in first_names) {
  var.first.name <- first 
  var.last.name <- 'Smith'
  api.instance <- PersonalApi$new()
  api.instance$apiClient$apiKeys['X-API-KEY'] <- 'THE-API-KEY-HERE';
  result <- api.instance$Gender(var.first.name, var.last.name)
  dput(result)
  mget(c('firstName','lastName','likelyGender', 'genderScale', 'id', 'score'), envir = result)

  unlist(mget(c('firstName','lastName','likelyGender', 'genderScale', 'id', 'score'), envir = result))
}

#_____________________________________________________________________________#

请让我知道我在想什么。值得一提的是,我的怀疑与“结果”是一个环境有关。

谢谢你, 保罗。

0 个答案:

没有答案