create_data/1.get_ESCO_skills.R

library(jsonlite)
library(tidyverse)
library(parallel)

get_skill <-function(result) {

  skill_en <- tryCatch(result$preferredLabel$en,
                             error = function(e) {
                               skill_en <- NULL
                             })
  skill_it <- tryCatch(result$preferredLabel$it,
                             error = function(e) {
                               skill_it <- NULL
                             })

  return(c(skill_en, skill_it))
}

get_skill_uri <-function(result) {

  skill_uri <- tryCatch(result$uri,
                             error = function(e) {
                               skill_uri <- NULL
                             })

  return(skill_uri)
}


get_skills_from_response <- function(response) {

  results <- response$`_embedded`

  N <- length(results)

  ESCO_skills <- tibble(.rows = NULL)

  for (i in 1:N){

    # Slicing the row
    result <- results[[i]]
    # Getting the occupation title
    skill <- get_skill(result)
    # Getting the occupation URI
    skill_uri <- get_skill_uri(result)

    # Building data frame
    skill_en <- skill[1]
    skill_it <- skill[2]
    occupation_df <- tibble(skill_en, skill_it, skill_uri)

    ESCO_skills <- bind_rows(ESCO_skills, occupation_df)
  }

  return(ESCO_skills)
}


get_ESCO_skills <- function() {

  skills_scheme <- "http://data.europa.eu/esco/concept-scheme/member-skills"

  language <- "it"

  api_url <- "https://ec.europa.eu/esco/api"
  skill_endpoint <- "/resource/skill"

  url <- paste0(api_url, skill_endpoint,
                "?",
                "isInScheme=",
                skills_scheme,
                "&language=",
                language)

  response <- fromJSON(txt=url(url))

  total_skills <- response$total

  ESCO_skills <- tibble(.rows = NULL)

  ESCO_skills_from_response <- get_skills_from_response(response)

  ESCO_skills <- bind_rows(ESCO_skills, ESCO_skills_from_response)

  print(paste0("Queried ",
               nrow(ESCO_skills),
               " of ",
               total_skills,
               " skills"))

  next_url <- response$`_links`$`next`$href

  while(!is.null(next_url)) {

    response <- fromJSON(txt=url(next_url))

    ESCO_skills_from_response <- get_skills_from_response(response)

    ESCO_skills <- bind_rows(ESCO_skills, ESCO_skills_from_response)

    next_url <- response$`_links`$`next`$href

    print(paste0("Queried ",
                 nrow(ESCO_skills),
                 " of ",
                 total_skills,
                 " skills"))

  }

  return(ESCO_skills)
}

ESCO_skills <- get_ESCO_skills()

saveRDS(ESCO_skills, file = "create_data/ESCO_skills.rds")
ldbolanos/standards documentation built on Aug. 7, 2020, 8:13 p.m.