将关键字搜索功能修改为 ignore/print NAs 或打印 "verify"
Modifying a keyword search function to either ignore/print NAs or print "verify"
我正在尝试访问一个 public 数据库,其中包含有关某些类型工作的信息。
我创建的函数是这样的:
onet.sum <- function(x) {
obj1 <- as.list(keySearch(x)) # enter self-reported job title into ONET's search engine
job.title <- obj1[["title"]][1] # pull best-matching title
soc.code <- obj1[["code"]][1] # pull best matching title's SOC code
obj4 <- as.data.frame(cbind(job.title,soc.code))
return(obj4)
}
如果将 onet.sum 应用于包含两个职位的数据集并且职位不包含空格,则它有效:
final_data <- lapply(c("psychologist","socialworker"), onet.sum) %>%
bind_rows
如果我包含 NA,则它会中断:
final_data <- lapply(c("psychologist","socialworker", NA), onet.sum) %>%
bind_rows
具体来说,会抛出这个错误:
[.data.frame
(keyOutput, , 1:2) 中的错误:选择了未定义的列。
...这可以追溯到我的 onet.sum() 函数调用的 keySearch() 函数。
keySearch <-
function (keyword) {
output <- getURL(paste("https://services.onetcenter.org/ws/mnm/search?keyword=",
keyword, sep = ""), userpwd = paste(get("creds", envir = cacheEnv)[[1]],
":", get("creds", envir = cacheEnv)[[2]], sep = ""),
httpauth = 1L)
if (grepl("Authorization Error", output)) {
message("Your API credentials are invalid. Please enter valid HTTPS credentials using setCreds().")
}
else if (grepl("total=\"0\"", output)) {
message("Your keyword returned no results. Please try another keyword or occupational title.")
}
else {
output <- xmlParse(output)
keyOutput <- xmlToDataFrame(nodes = getNodeSet(output,
"//career"))
message("Find a SOC code below and use with socSearch() function to pull job data.")
return(keyOutput[, 1:2])
}
}
问题
如何修改 keySearch() 函数,以便在行不包含职位时在 keyOutput() 对象中打印 NA?
有些职位不会 return 结果(例如,“服务台支持专家”不会生成结果,即使忽略空格也是如此)。我很好地将这些打印为要检查的值,因为我正在使用的网站显然可以为“帮助台支持专家”生成结果。理想情况下,我可以调试这个问题,但我不确定从哪里开始。
这里是包维护者。为了解决这些错误,我对 Github 上的开发版本进行了一些更新,多年来它也有一些其他重大变化,包括将多个功能折叠在一起。本质上,所有工作信息功能都被折叠到 jobInfo()
中,您可以在其中明确指出您正在搜索的信息类型,并且 keySearch()
和 socSearch()
被折叠到 jobSearch()
其中您指明您的搜索是 type="keyword"
还是 type="soc"
。我尽力避免破坏性更改,所以所有旧功能应该仍然可以正常工作,尽管有一条消息表明它们在技术上是 'deprecated'.
安装开发版本的说明:
- 转到https://github.com/eknud/ONETr
- 点击右上角的绿色按钮'Code'和'Download ZIP'。
- 解压缩文件。
- 运行 命令
devtools::install("path/to/unzipped_folder")
在 R 中。你现在应该设置 运行 开发功能。
更新了onet.sum函数和测试代码
onet.sum <- function(x) {
obj1 <- as.list(jobSearch(x, type = "keyword"))
job.title <- obj1[["title"]][1] # pull best-matching title
soc.code <- obj1[["code"]][1] # pull best matching title's SOC code
obj4 <- as.data.frame(cbind(job.title,soc.code))
return(obj4)
}
# now test it
library(ONETr)
library(tidyverse)
setCreds("user","pass")
final_data <- lapply(c("psychologist","social worker",NA), onet.sum) %>% bind_rows
我正在尝试访问一个 public 数据库,其中包含有关某些类型工作的信息。
我创建的函数是这样的:
onet.sum <- function(x) {
obj1 <- as.list(keySearch(x)) # enter self-reported job title into ONET's search engine
job.title <- obj1[["title"]][1] # pull best-matching title
soc.code <- obj1[["code"]][1] # pull best matching title's SOC code
obj4 <- as.data.frame(cbind(job.title,soc.code))
return(obj4)
}
如果将 onet.sum 应用于包含两个职位的数据集并且职位不包含空格,则它有效:
final_data <- lapply(c("psychologist","socialworker"), onet.sum) %>%
bind_rows
如果我包含 NA,则它会中断:
final_data <- lapply(c("psychologist","socialworker", NA), onet.sum) %>%
bind_rows
具体来说,会抛出这个错误:
[.data.frame
(keyOutput, , 1:2) 中的错误:选择了未定义的列。
...这可以追溯到我的 onet.sum() 函数调用的 keySearch() 函数。
keySearch <-
function (keyword) {
output <- getURL(paste("https://services.onetcenter.org/ws/mnm/search?keyword=",
keyword, sep = ""), userpwd = paste(get("creds", envir = cacheEnv)[[1]],
":", get("creds", envir = cacheEnv)[[2]], sep = ""),
httpauth = 1L)
if (grepl("Authorization Error", output)) {
message("Your API credentials are invalid. Please enter valid HTTPS credentials using setCreds().")
}
else if (grepl("total=\"0\"", output)) {
message("Your keyword returned no results. Please try another keyword or occupational title.")
}
else {
output <- xmlParse(output)
keyOutput <- xmlToDataFrame(nodes = getNodeSet(output,
"//career"))
message("Find a SOC code below and use with socSearch() function to pull job data.")
return(keyOutput[, 1:2])
}
}
问题
如何修改 keySearch() 函数,以便在行不包含职位时在 keyOutput() 对象中打印 NA?
有些职位不会 return 结果(例如,“服务台支持专家”不会生成结果,即使忽略空格也是如此)。我很好地将这些打印为要检查的值,因为我正在使用的网站显然可以为“帮助台支持专家”生成结果。理想情况下,我可以调试这个问题,但我不确定从哪里开始。
这里是包维护者。为了解决这些错误,我对 Github 上的开发版本进行了一些更新,多年来它也有一些其他重大变化,包括将多个功能折叠在一起。本质上,所有工作信息功能都被折叠到 jobInfo()
中,您可以在其中明确指出您正在搜索的信息类型,并且 keySearch()
和 socSearch()
被折叠到 jobSearch()
其中您指明您的搜索是 type="keyword"
还是 type="soc"
。我尽力避免破坏性更改,所以所有旧功能应该仍然可以正常工作,尽管有一条消息表明它们在技术上是 'deprecated'.
安装开发版本的说明:
- 转到https://github.com/eknud/ONETr
- 点击右上角的绿色按钮'Code'和'Download ZIP'。
- 解压缩文件。
- 运行 命令
devtools::install("path/to/unzipped_folder")
在 R 中。你现在应该设置 运行 开发功能。
更新了onet.sum函数和测试代码
onet.sum <- function(x) {
obj1 <- as.list(jobSearch(x, type = "keyword"))
job.title <- obj1[["title"]][1] # pull best-matching title
soc.code <- obj1[["code"]][1] # pull best matching title's SOC code
obj4 <- as.data.frame(cbind(job.title,soc.code))
return(obj4)
}
# now test it
library(ONETr)
library(tidyverse)
setCreds("user","pass")
final_data <- lapply(c("psychologist","social worker",NA), onet.sum) %>% bind_rows