Skip to content

Instantly share code, notes, and snippets.

@fernandobarbalho
Last active September 6, 2023 14:27
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandobarbalho/84894cc2b61fa84f1b589679839e14ce to your computer and use it in GitHub Desktop.
Save fernandobarbalho/84894cc2b61fa84f1b589679839e14ce to your computer and use it in GitHub Desktop.
O código abaixo mostra como consumir dados de dados.gov.br a partir da API de consumo de dados disponibilizada
library(jsonlite)
# load the httr package
library(httr)
#Você deve ter uma chave gerada de API. Para isso você precisa criar um usuário na página dados.gov.br usando o gov.br
my_key<- "<sua_chave_aqui>"
# Define the API endpoint and headers
url <- "https://dados.gov.br/dados/api/publico/conjuntos-dados"
headers <- c(
"accept" = "application/json",
"chave-api-dados-abertos" = my_key
)
# Define the query parameters to retrieve data about "Impostos"
params <- list(
isPrivado = "false",
nomeConjuntoDados = "Imposto",
pagina = "1"
)
# Make the GET request
response <- GET(url, add_headers(headers), query=params)
# Print the response content
conteudo<- content(response, "text")
df_impostos<- jsonlite::fromJSON(conteudo)
#Url para dados sobre grandes números de impostos
url_grandes_numeros <- str_c(url,"/", df_impostos$id[2] )
# Make the GET request
response <- GET(url_grandes_numeros, add_headers(headers))
conteudo_grandes_numeros<- content(response, "text")
list_impostos_grandes_numeros<- jsonlite::fromJSON(conteudo_grandes_numeros)
#download data
recursos<- list_impostos_grandes_numeros[["recursos"]]
#link de bens e direitos
link_arquivo<- recursos$link[2]
download.file(link_arquivo, destfile = "bens_direitos.csv", mode = "wb")
library(tidyverse)
bens_direitos <- read_delim("bens_direitos.csv",
delim = ";", escape_double = FALSE, locale = locale(decimal_mark = ",",
grouping_mark = "."), trim_ws = TRUE)
bens_direitos_tidy<-
bens_direitos %>%
pivot_longer(cols = -1, names_to = "bens_direitos", values_to = "valor") %>%
rename(ano = `Ano Calendário`)
bens_direitos_tidy %>%
filter(ano == 2020) %>%
slice_max(order_by = valor, n=10) %>%
mutate(bens_direitos = reorder(str_wrap(bens_direitos,20), valor)) %>%
ggplot()+
geom_col(aes(x= valor, y=bens_direitos)) +
theme_light() +
labs(x= "Valor em R$ mi",
y="",
title= "Grandes números IRPF: total bens e consumo para 2020")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment