标题列必须具有一致的长度,如何从列表中正确提取

时间:2019-12-21 01:46:14

标签: r purrr tibble

我对该函数正在接收的以下错误感到困惑。如果我放下ementatema,它可以平稳运行,但是通过包含这些端点,它会因为长度不匹配而中断。我在做什么错了?

  download_data <- function(y){

  url = paste0("http://legis.senado.gov.br/dadosabertos/materia/pesquisa/lista?ano=",y)

  request <- httr::GET(url)
  request <- httr::content(request, as = "parsed")

  Materia <- purrr::flatten(request$PesquisaBasicaMateria$Materias$Materia)
  DadosBasicos_df <- purrr::flatten(Materia)
  # AutoresPrincipais <- purrr::flatten(request$PesquisaBasicaMateria$Materias$AutoresPrincipais)

  dados <- tibble::tibble(
  id = purrr::map_chr(Materia, .null = NA, "CodigoMateria") %>% na.omit(),
  nome = purrr::map_chr(Materia, .null = NA, "DescricaoIdentificacaoMateria") %>% na.omit(),
  AutoresPrincipais = purrr::map_chr(Materia$AutoresPrincipais, .null = NA, "NomeAutor") %>% na.omit(),
  ementa = purrr::map_chr(Materia$DadosBasicosMateria, .null = NA, "EmentaMateria") %>% na.omit(),
  tema = purrr::map_chr(Materia$DadosBasicosMateria, .null = NA, "IndexacaoMateria") %>% na.omit())
  dados 
  }

然后我们运行

  library(httr)
  library(tidyverse)
  library(jsonlite)
  library(purrr)


  y = 2018
  data <- map_df(y, download_data)




Error: Tibble columns must have consistent lengths, only values of length one are recycled:
* Length 0: Columns `ementa`, `tema`
* Length 2907: Columns `id`, `nome`
Run `rlang::last_error()` to see where the error occurred. 

0 个答案:

没有答案