Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions r-package/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: basedosdados
Title: 'Base Dos Dados' R Client
Version: 0.2.3
Authors@R:
Version: 0.2.4
Authors@R:
c(person(given = "Pedro",
family = "Cavalcante",
role = c("aut", "cre"),
Expand All @@ -20,8 +20,8 @@ Description: An R interface to the 'Base dos Dados' API <https://basedosdados.or
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports:
RoxygenNote: 7.3.2
Imports:
purrr (>= 0.3.4),
dplyr (>= 1.0.6),
tibble (>= 3.1.1),
Expand All @@ -39,7 +39,6 @@ Imports:
dbplyr (>= 2.1.1),
scales (>= 1.1.1),
DBI (>= 1.1.1),
typed (>= 0.0.1),
methods
Suggests:
rmarkdown,
Expand Down
4 changes: 1 addition & 3 deletions r-package/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
S3method(dbplyr_edition,BaseDosDadosConnection)
export("%>%")
export(bd_collect)
export(bd_request)
export(bd_write)
export(bd_write_csv)
export(bd_write_rds)
Expand Down Expand Up @@ -51,7 +52,4 @@ importFrom(scales,number_bytes)
importFrom(stringr,str_count)
importFrom(stringr,str_detect)
importFrom(stringr,str_replace_all)
importFrom(typed,"?")
importFrom(typed,check_arg)
importFrom(typed,check_output)
importFrom(writexl,write_xlsx)
File renamed without changes.
10 changes: 5 additions & 5 deletions r-package/R/bdplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ bd_collect <- function(

bd_write <- function(
.lazy_tbl,
.write_fn = ? typed::Function(),
path = ? typed::Character(length = 1),
overwrite = FALSE ? typed::Logical(1),
.write_fn,
path,
overwrite = FALSE,
...) {

# check if the path already exists
Expand Down Expand Up @@ -538,8 +538,8 @@ bd_write_rds <- function(
#' @export
bd_write_csv <- function(
.lazy_tbl,
path = ? typed::Character(1),
overwrite = FALSE ? typed::Logical(1),
path,
overwrite = FALSE,
...) {

# check if the path already exists
Expand Down
38 changes: 38 additions & 0 deletions r-package/R/http-client.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' Abstraction of an http request to the API
#' @param endpoint which endpoint to request.
#' @param query querystring passed as argument to the GEt request.
#' @export
#' @examples
#' \dontrun{
#'
#' bd_request(
#' endpoint = "dataset_search",
#' query = list(
#' resource_type = "bdm_table",
#' q = search_term,
#' page_size = 100))
#'
#' bd_request(
#' endpoint = "bdm_dataset_show",
#' query = list(
#' dataset_id = dataset_id)
#'
#'}
#'@return A httr::response() object.


bd_request <- function(
endpoint,
query = list()) {

base_url <- "https://basedosdados.org/api/3/action/bd_"

@aspeddro aspeddro Sep 16, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esse API não exite, certo @rdahis? Usamos https://backend.basedosdados.org/api/v1/graphql

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certo! Essa era a api do ckan

target_endpoint <- paste0(base_url, endpoint)

httr::GET(
target_endpoint,
encode = 'json',
query = query) %>%
httr::content() %>%
purrr::pluck("result")

}
17 changes: 0 additions & 17 deletions r-package/R/http_client.R

This file was deleted.

83 changes: 61 additions & 22 deletions r-package/R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ dataset_search <- function(search_term) {
page_size = 100)) ->
search

if (is.null(search) || is.null(search$datasets) || length(search$datasets) == 0) {
return(tibble::tibble(
dataset_name = character(0),
dataset_tables = list(),
url = character(0),
title = character(0)
))
}

tibble::tibble(
dataset_name = purrr::map_chr(
.x = search$datasets,
Expand Down Expand Up @@ -60,7 +69,7 @@ dataset_search <- function(search_term) {

list_dataset_tables <- function(dataset_id) {

bd_request(
basedosdados::bd_request(
endpoint = "bdm_dataset_show",
query = list(
dataset_id = dataset_id)) ->
Expand All @@ -80,7 +89,7 @@ list_dataset_tables <- function(dataset_id) {
purrr::pluck("resources") %>%
purrr::keep(~ .x$resource_type == "bdm_table") %>%
purrr::map(fetch_function) %>%
purrr::reduce(dplyr::bind_rows)
purrr::reduce(dplyr::bind_rows, .init = tibble::tibble(name = character(0), description = character(0)))

}

Expand All @@ -102,14 +111,23 @@ get_table_columns <- function(
dataset_id,
table_id) {

bd_request(
result <- basedosdados::bd_request(
endpoint = "bdm_table_show",
query = list(
table_id = table_id,
dataset_id = dataset_id)) %>%
dataset_id = dataset_id))

if (is.null(result) || is.null(result$columns) || length(result$columns) == 0) {
return(tibble::tibble(
name = character(0),
description = character(0)
))
}

result %>%
purrr::pluck("columns") %>%
purrr::map(tibble::as_tibble) %>%
purrr::reduce(dplyr::bind_rows) %>%
purrr::reduce(dplyr::bind_rows, .init = tibble::tibble()) %>%
dplyr::select(- c(.data$is_in_staging, .data$is_partition))

}
Expand All @@ -129,17 +147,25 @@ get_table_columns <- function(

get_dataset_description <- function(dataset_id) {

bd_request(
result <- basedosdados::bd_request(
endpoint = "bdm_dataset_show",
query = list(
dataset_id = dataset_id)) ->
result
dataset_id = dataset_id))

if (is.null(result)) {
return(tibble::tibble(
name = NA_character_,
title = NA_character_,
tables = list(tibble::tibble(name = character(0), description = character(0))),
notes = NA_character_
))
}

tibble::tibble(
name = result$name,
title = result$title,
name = if (is.null(result$name)) NA_character_ else result$name,
title = if (is.null(result$title)) NA_character_ else result$title,
tables = list(list_dataset_tables(dataset_id)),
notes = result$notes)
notes = if (is.null(result$notes)) NA_character_ else result$notes)

}

Expand All @@ -156,24 +182,37 @@ get_dataset_description <- function(dataset_id) {
#'

get_table_description <- function(
dataset_id = ? typed::Character(1),
table_id = ? typed::Character(1)) {
dataset_id,
table_id) {

bd_request(
result <- basedosdados::bd_request(
endpoint = "bdm_table_show",
query = list(
dataset_id = dataset_id,
table_id = table_id)) ->
result
table_id = table_id))

if (is.null(result)) {
return(tibble::tibble(
dataset_id = dataset_id,
table_id = table_id,
description = NA_character_,
columns = list(tibble::tibble())
))
}

columns_data <- if (is.null(result$columns) || length(result$columns) == 0) {
tibble::tibble()
} else {
result %>%
purrr::pluck("columns") %>%
purrr::map(tibble::as_tibble) %>%
purrr::reduce(dplyr::bind_rows, .init = tibble::tibble())
}

tibble::tibble(
dataset_id = dataset_id,
table_id = table_id,
description = result$description,
columns = result %>%
purrr::pluck("columns") %>%
purrr::map(tibble::as_tibble) %>%
purrr::reduce(dplyr::bind_rows) %>%
list())
description = if (is.null(result$description)) NA_character_ else result$description,
columns = list(columns_data))

}
File renamed without changes.
4 changes: 0 additions & 4 deletions r-package/R/utils-typed.R

This file was deleted.

2 changes: 1 addition & 1 deletion r-package/man/BaseDosDadosConnection-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion r-package/man/bd_collect.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions r-package/man/bd_request.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 4 additions & 15 deletions r-package/man/bd_write.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions r-package/man/bdplyr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading