Skip to content

Commit 723bb47

Browse files
authored
Merge pull request #106 from analytics-ufcg/842-migrar-fetch-votacao-senado
Adiciona o fetch_votacoes_senado
2 parents b7c0112 + 6d5c3ed commit 723bb47

13 files changed

+198
-45
lines changed

R/colunas_constants.R

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"nome_poder_origem"="character", "sigla_casa_origem"="character", "nome_casa_origem"="character", "proposicoes_relacionadas"="character",
4848
"proposicoes_apensadas"="character", "codigo_natureza"="integer", "nome_natureza"="character", "descricao_natureza"="character", "autor_nome"="character")
4949

50-
.COLNAMES_VOTACOES <- c("id"="integer","uri"="character","titulo"="character","uriEvento"="character","uriProposicaoPrincipal"="character",
50+
.COLNAMES_VOTACOES_CAMARA <- c("id"="integer","uri"="character","titulo"="character","uriEvento"="character","uriProposicaoPrincipal"="character",
5151
"tipoVotacao"="character","aprovada"="logical","placarSim"="integer","placarNao"="integer","placarAbstencao"="integer",
5252
"proposicao.id"="integer","proposicao.uri"="character","proposicao.siglaTipo"="character","proposicao.codTipo"="integer",
5353
"proposicao.numero"="integer","proposicao.ano"="integer","proposicao.ementa"="character")
@@ -108,6 +108,64 @@
108108
"ementa"="character"
109109
## "siglaTipo"="character","codTipo"="integer","numero"="numeric","ano"="numeric",
110110
)
111+
.COLNAMES_VOT_SEN <-
112+
c(
113+
"descricao_identificacao_materia"="character",
114+
"indicador_votacao_secreta"="character",
115+
"descricao_votacao"="character",
116+
"descricao_resultado"="character",
117+
"codigo_sessao"="character",
118+
"sigla_casa_sessao"="character",
119+
"nome_casa_sessao"="character",
120+
"codigo_sessao_legislativa"="character",
121+
"sigla_tipo_sessao"="character",
122+
"numero_sessao"="character",
123+
"data_sessao"="character",
124+
"hora_inicio_sessao"="character",
125+
"codigo_tramitacao"="character",
126+
"numero_autuacao"="character",
127+
"data_tramitacao"="character",
128+
"numero_ordem_tramitacao"="character",
129+
"texto_tramitacao"="character",
130+
"indicador_recebimento"="character",
131+
"data_recebimento"="character",
132+
"origem_tramitacao_local_codigo_local"="character",
133+
"origem_tramitacao_local_tipo_local"="character",
134+
"origem_tramitacao_local_sigla_casa_local"="character",
135+
"origem_tramitacao_local_nome_casa_local"="character",
136+
"origem_tramitacao_local_sigla_local"="character",
137+
"origem_tramitacao_local_nome_local"="character",
138+
"destino_tramitacao_local_codigo_local"="character",
139+
"destino_tramitacao_local_tipo_local"="character",
140+
"destino_tramitacao_local_sigla_casa_local"="character",
141+
"destino_tramitacao_local_nome_casa_local"="character",
142+
"destino_tramitacao_local_sigla_local"="character",
143+
"destino_tramitacao_local_nome_local"="character",
144+
"situacao_codigo_situacao"="character",
145+
"situacao_sigla_situacao"="character",
146+
"situacao_descricao_situacao"="character",
147+
"descricao_voto"="character",
148+
"codigo_parlamentar"="character",
149+
"nome_parlamentar"="character",
150+
"nome_completo_parlamentar"="character",
151+
"sexo_parlamentar"="character",
152+
"forma_tratamento"="character",
153+
"url_foto_parlamentar"="character",
154+
"url_pagina_parlamentar"="character",
155+
"email_parlamentar"="character",
156+
"sigla_partido_parlamentar"="character",
157+
"uf_parlamentar"="character",
158+
"codigo_materia"="character",
159+
"sigla_casa_identificacao_materia"="character",
160+
"nome_casa_identificacao_materia"="character",
161+
"sigla_subtipo_materia"="character",
162+
"descricao_subtipo_materia"="character",
163+
"descricao_identificacao_materia"="character",
164+
"numero_materia"="character",
165+
"ano_materia"="character",
166+
"indicador_tramitando"="character",
167+
"descricao_objetivo_processo"="character"
168+
)
111169

112170
# Partidos
113171
.COLNAMES_PARTIDOS <- c("id"="integer","sigla"="character","nome"="character","uri"="character")

R/constants.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
.DEFERIMENTO_SENADO_PATH <- "/dadosabertos/materia/movimentacoes/"
3939
.AGENDA_SENADO_PATH <- "/dadosabertos/plenario/agenda/mes/"
4040
.AGENDA_SENADO_COMISSOES <- "/dadosabertos/agenda/"
41+
.SENADO_VOTACOES_PATH <- "/dadosabertos/materia/votacoes/"
4142

4243
# Link do repositório do rcongresso
4344
.RCONGRESSO_LINK <- "https://github.com/analytics-ufcg/rcongresso"

R/proposicoes.R

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -142,29 +142,6 @@ fetch_proposicao_senado <- function(id) {
142142
.coerce_types(.COLNAMES_PROPOSICAO_SENADO)
143143
}
144144

145-
#' @title Fetches all the votings which a proposition went through
146-
#' @description Returns all the votings related to a proposition by its id.
147-
#' @param id_prop Proposition's ID
148-
#' @return Dataframe containing all the votings.
149-
#' @examples
150-
#' votacoes_pec241 <- fetch_votacoes(2088351)
151-
#' @seealso
152-
#' \code{\link[rcongresso]{fetch_id_proposicao_camara}}, \code{\link[rcongresso]{fetch_proposicao_from_votacao}}
153-
#' @rdname fetch_votacoes
154-
#' @export
155-
fetch_votacoes <- function(id_prop){
156-
id <- NULL
157-
tibble::tibble(id = id_prop) %>%
158-
dplyr::mutate(path = paste0(.CAMARA_PROPOSICOES_PATH, "/", id, "/votacoes")) %>%
159-
dplyr::rowwise() %>%
160-
dplyr::do(
161-
.camara_api(.$path)
162-
) %>%
163-
dplyr::ungroup() %>%
164-
.assert_dataframe_completo(.COLNAMES_VOTACOES) %>%
165-
.coerce_types(.COLNAMES_VOTACOES)
166-
}
167-
168145
#' @title Fetches all propositions related to a proposition
169146
#' @description Returns all propositions related to a proposition by its id.
170147
#' @param id_prop Proposition's ID

R/utils.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,26 @@ rename_table_to_underscore <- function(df) {
430430
tramitacao_df
431431
}
432432

433+
#' @title Renames the cols of the bill's voting on Senate
434+
#' @description Renames each item from vector with the pattern: split by underscore and lowercase
435+
#' @param df Dataframe
436+
#' @return Dataframe containing the renamed strings.
437+
#' @export
438+
#' @rdname fetch_votacoes_senado
439+
#' @export
440+
.rename_votacoes_df <- function(df) {
441+
new_names = names(df) %>%
442+
.to_underscore() %>%
443+
stringr::str_replace(
444+
"sessao_plenaria_|tramitacao_identificacao_tramitacao_|identificacao_parlamentar_",
445+
""
446+
)
447+
448+
names(df) <- new_names
449+
450+
df
451+
}
452+
433453
#' @title Get the author on Chamber
434454
#' @description Return a dataframe with the link, name, code, type and house
435455
#' @param prop_id Proposition ID

R/votacoes.R

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#' @title Fetches details about a voting
2-
#' @description Fetches details about a voting.
1+
#' @title Fetches details about a voting on Chamber
2+
#' @description Fetches details about a voting on Chamber.
33
#' @param id_votacao Voting's ID
44
#' @return Dataframe containing details about a voting, including tittle,
55
#' start voting time, finish voting time, result and approval
@@ -21,6 +21,66 @@ fetch_votacao <- function(id_votacao = NULL){
2121
.coerce_types(.COLNAMES_VOTACAO)
2222
}
2323

24+
#' @title Fetches all the votings which a proposition went through
25+
#' @description Returns all the votings related to a proposition by its id.
26+
#' @param id_prop Proposition's ID
27+
#' @return Dataframe containing all the votings.
28+
#' @examples
29+
#' votacoes_pec241 <- fetch_votacoes_camara(2088351)
30+
#' @seealso
31+
#' \code{\link[rcongresso]{fetch_id_proposicao_camara}}, \code{\link[rcongresso]{fetch_proposicao_from_votacao}}
32+
#' @rdname fetch_votacoes_camara
33+
#' @export
34+
fetch_votacoes_camara <- function(id_prop){
35+
id <- NULL
36+
tibble::tibble(id = id_prop) %>%
37+
dplyr::mutate(path = paste0(.CAMARA_PROPOSICOES_PATH, "/", id, "/votacoes")) %>%
38+
dplyr::rowwise() %>%
39+
dplyr::do(
40+
.camara_api(.$path)
41+
) %>%
42+
dplyr::ungroup() %>%
43+
.assert_dataframe_completo(.COLNAMES_VOTACOES_CAMARA) %>%
44+
.coerce_types(.COLNAMES_VOTACOES_CAMARA)
45+
}
46+
47+
#' @title Fetches details about a voting on Senate
48+
#' @description Fetches details about a voting on Senate.
49+
#' Ao fim, a função retira todos as colunas que tenham tipo lista para uniformizar o dataframe.
50+
#' @param proposicao_id Proposition Id
51+
#' @return Dataframe containing details about a voting on Senate
52+
#' @examples
53+
#' fetch_votacoes_senado(91341)
54+
#' @rdname fetch_votacoes_senado
55+
#' @export
56+
fetch_votacoes_senado <- function(proposicao_id) {
57+
58+
json_votacoes <- .senado_api(paste0(.SENADO_VOTACOES_PATH, proposicao_id), asList = TRUE)
59+
votacoes_data <-
60+
json_votacoes %>%
61+
magrittr::extract2("VotacaoMateria") %>%
62+
magrittr::extract2("Materia")
63+
votacoes_ids <-
64+
votacoes_data %>%
65+
magrittr::extract2("IdentificacaoMateria") %>%
66+
tibble::as_tibble() %>%
67+
unique()
68+
votacoes_df <-
69+
votacoes_data %>%
70+
magrittr::extract2("Votacoes") %>%
71+
purrr::map_df( ~ .) %>%
72+
tidyr::unnest()
73+
74+
votacoes_df <-
75+
votacoes_df %>%
76+
tibble::add_column(!!!votacoes_ids)
77+
78+
votacoes_df <- votacoes_df[,!sapply(votacoes_df, is.list)]
79+
80+
.rename_votacoes_df(votacoes_df)
81+
votacoes_df
82+
}
83+
2484
#' @title Fetches the positions of a group on a voting
2585
#' @description Fetch how the groups in the chamber of deputies
2686
#' instructed their members to vote on a given voting.
@@ -83,7 +143,7 @@ fetch_votos <- function(id_votacao = NULL){
83143
#' @param votacoes Dataframe containing all the votings related to a proposition
84144
#' @return Dataframe containing only the last voting related to a proposition
85145
#' @examples
86-
#' votacoes_pec241 <- fetch_votacoes(2088351)
146+
#' votacoes_pec241 <- fetch_votacoes_camara(2088351)
87147
#' ultima_votacao <- ultima_votacao(votacoes_pec241)
88148
#' @rdname ultima_votacao
89149
#' @export

man/fetch_votacao.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fetch_votacoes_senado.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ultima_votacao.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_proposicoes.R

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pec_241_id <<- fetch_id_proposicao_camara("PEC", 241, 2016)
55
pec_241_por_id <<- fetch_proposicao_camara(pec_241_id)
66
pls_91341 <<- fetch_proposicao_senado(91341)
77
pls_1489 <<- fetch_proposicao_senado(1489)
8-
votacoes_pec_241 <<- fetch_votacoes(pec_241_id)
98
relacionadas_pec_241 <<- fetch_relacionadas(pec_241_id)
109
deferimento <- fetch_deferimento(c("102343", "109173", "115853"))
1110

@@ -29,14 +28,12 @@ test_that("GET proposição com ID inexistente", {expect_error(fetch_proposicao_
2928
test_that("Is dataframe", {
3029
expect_true(is.data.frame(pec_241))
3130
expect_true(is.data.frame(pec_241_por_id))
32-
expect_true(is.data.frame(votacoes_pec_241))
3331
expect_true(is.data.frame(deferimento))
3432
})
3533

3634
test_that("Not Empty", {
3735
expect_true(nrow(pec_241) != 0)
3836
expect_true(nrow(pec_241_por_id) != 0)
39-
expect_true(nrow(votacoes_pec_241) != 0)
4037
expect_true(nrow(deferimento) != 0)
4138
})
4239

@@ -65,10 +62,6 @@ test_that("fetch_proposicao_senado() is dataframe", {
6562
expect_true(is.data.frame(pls_1489))
6663
})
6764

68-
test_that("fetch_votacoes()", {
69-
expect_true(all(sapply(votacoes_pec_241, class) %in% .COLNAMES_VOTACOES))
70-
})
71-
7265
test_that("fetch_relacionadas()", {
7366
expect_true(all(sapply(relacionadas_pec_241, class) %in% .COLNAMES_RELACIONADAS))
7467
})

0 commit comments

Comments
 (0)