-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74f724f
commit 0142464
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ README.md | |
^_pkgdown\.yml$ | ||
^docs$ | ||
^pkgdown$ | ||
^vignettes/articles$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ Suggests: | |
Config/testthat/edition: 3 | ||
Depends: | ||
R (>= 2.10) | ||
Config/Needs/website: rmarkdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.html | ||
*.R |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
--- | ||
title: "FAQ" | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
```{r setup} | ||
library(microdatasus) | ||
``` | ||
|
||
## Download timeout | ||
|
||
Por padrão, o R espera que um arquivo seja baixado em até 60 segundos. Ao baixar algum arquivo muito grande ou com uma conexão com a Internet lenta, o tempo necessário para download será maior. Neste caso, você verá uma mensagem de *warning* semelhante a esta: | ||
|
||
``` | ||
1: In utils::download.file(file, temp, mode = "wb", method = "libcurl") : | ||
downloaded length 12260742 != reported length 30474112 | ||
2: In utils::download.file(file, temp, mode = "wb", method = "libcurl") : | ||
URL 'ftp://ftp.datasus.gov.br/dissemin/publicos/SINAN/DADOS/FINAIS/DENGBR10.dbc': Timeout of 60 seconds was reached | ||
``` | ||
Para corrigir isto, é bem simples: aumente o tempo de tolerância do R para downloads. Exemplo: tolerância de 2 minutos. | ||
|
||
```{r} | ||
options(timeout = 120) | ||
``` | ||
|
||
|
||
## Download de muitos dados | ||
|
||
O download dos dados através do pacote é realizado em memória. Isto significa que cada arquivo baixado do DataSUS é lido e armazenado na memória RAM do computador. Caso você esteja baixando muitos arquivos, para vários estados e anos, você irá precisar de mais memória RAM disponível. | ||
|
||
Um modo de contornar este problema é a realizar o download e armazenar os resultados em um banco de dados, como SQLite, DuckDB ou outro. Abaixo segue um exemplo de como fazer isso. | ||
|
||
### Pacotes | ||
|
||
```{r eval=FALSE} | ||
library(microdatasus) | ||
library(tidyverse) | ||
library(dbplyr) | ||
library(DBI) | ||
library(RSQLite) | ||
``` | ||
|
||
### Cria conexão com o banco de dados | ||
|
||
```{r eval=FALSE} | ||
conn <- dbConnect(RSQLite::SQLite(), "sih.SQLite") | ||
``` | ||
|
||
|
||
|
||
### Realiza download e pré-processamento dos dados em loop | ||
|
||
```{r eval=FALSE} | ||
anos <- c(2019, 2020) | ||
meses <- 1:12 | ||
ufs <- c("AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", | ||
"GO", "MA", "MT", "MS", "MG", "PA", "PB", "PR", "PE", | ||
"PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SP", "SE", | ||
"TO") | ||
``` | ||
|
||
```{r eval=FALSE} | ||
for(ano in anos){ | ||
for(mes in meses){ | ||
for(uf in ufs){ | ||
# Baixa o dado para um ano, mês e UF específico | ||
tmp <- fetch_datasus( | ||
year_start = ano, year_end = ano, | ||
month_start = mes, month_end = mes, | ||
uf = uf, information_system = "SIH-RD" | ||
) | ||
# Pré-processamento dos dados | ||
tmp <- process_sih(data = tmp) | ||
# Escreve na tabela "sih" no banco de dados, | ||
# apensando os dados de cada ano e mês | ||
dbWriteTable(conn = conn, name = "sih", value = tmp, append = TRUE) | ||
# Remove a tabela temporária | ||
rm(tmp) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
|
||
### Exemplo de consulta ao banco de dados, | ||
|
||
Uma tabela de quantidade de registrosm, por sexo e causa básica | ||
|
||
```{r eval=FALSE} | ||
tbl(conn, "sih") %>% | ||
group_by(SEXO, COMPLEX) %>% | ||
summarise(freq = n()) %>% | ||
ungroup() %>% | ||
collect() | ||
``` |