Skip to content

Commit 2051a82

Browse files
committed
Merged source code for sim.* into sim.load. Other sim.* functions works as wrappers over sim.load now. Fixed bug in Windows platforms.
1 parent 194fecf commit 2051a82

20 files changed

+665
-908
lines changed

NAMESPACE

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(cnes.lt)
34
export(datasus.init)
5+
export(datasus.lang)
6+
export(sim.do)
47
export(sim.doext)
58
export(sim.dofet)
69
export(sim.doinf)
710
export(sim.domat)
8-
export(sim.dores)
11+
export(sim.load)
912
export(territory.load)

R/cnes.lt.R

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# cnes.lt.R
2+
# Copyright (C) 2016 Daniela Petruzalek
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#' Provides Access to the Beds Dataset from the National Register of Health Establishments (CNES).
18+
#'
19+
#' \code{cnes.lt} returns a data.frame with a subset of the Beds (LT) dataset
20+
#'
21+
#' @details
22+
#' This system contains data divided in the following categories:
23+
#'
24+
#' \itemize{
25+
#' \item LT: Beds (from Oct 2005)
26+
#' \item ST: Establishments (from Aug 2005)
27+
#' \item DC: Complimentary Data (from Aug 2005)
28+
#' \item EQ: Equipments (from Aug 2005)
29+
#' \item SR: Specialized Services (from Aug 2005)
30+
#' \item HB: License (from Mar 2007)
31+
#' \item PF: Practitioner (from Ago 2005)
32+
#' \item EP: Teams (from Apr 2007)
33+
#' \item RC: Contractual Rules (from Mar 2007)
34+
#' \item IN: Incentives (from Nov 2007)
35+
#' \item EE: Teaching Establishments (from Mar 2007)
36+
#' \item EF: Philanthropic Establishment (from Mar 2007)
37+
#' \item GM: Management and Goals (from Jun 2007)
38+
#' }
39+
#'
40+
#' The \code{cnes.*} functions are provided for each available subsystem, for example, \code{cnes.lt}, \code{cnes.gm} and so on.
41+
#'
42+
#' @note
43+
#' DATASUS is the name of the Department of Informatics of the Brazilian Unified Health System (SUS) and is resposible for publishing public healthcare data. Besides the DATASUS, the Brazilian National Agency for Supplementary Health (ANS) also uses this file format for its public data. The name DATASUS is also often used to represent the public datasets they provide.
44+
#'
45+
#' Neither this project, nor its author, has any association with the brazilian government.
46+
#' @param years numeric; one or more years to select the target data to be read
47+
#' @param months numeric; one or more months to select the target data to be read
48+
#' @param states character; one or more state (UF) representing the location of the data to be read, or 'ALL' if you want to read data from all states (UFs) at the same time.
49+
#' @param language character; column names in Portuguese ("pt") or English ("en"). Default is "en".
50+
#' @return a data.frame with Brazil's health establishment data
51+
#' @keywords datasus
52+
#' @export
53+
#' @author Daniela Petruzalek, \email{[email protected]}
54+
#' @seealso \code{\link{datasus.init}} \code{\link{read.dbc}}
55+
#' @examples
56+
#'
57+
#' pr201001 <- cnes.lt(2010, 'jan', 'PR')
58+
cnes.lt <- function(years, months, states, language = datasus.lang()) {
59+
# WIP
60+
}

R/datasus.R

+101-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
#' Initialize DATASUS environment
1818
#'
19-
#' This function initializes the environment that contains the DATASUS package control variables, like the locations for the DATASUS ftp and the local working directory.
19+
#' \code{datasus.init} initializes the environment that contains the DATASUS package control variables, like the locations for the DATASUS ftp and the local working directory.
20+
#'
2021
#' @details
2122
#' This function prepares the \code{datasus.env} environment. You can specify the DATASUS working directory with the parameter \code{workdir} to set the local directory where to store the downloaded DATASUS files.
2223
#'
@@ -38,55 +39,103 @@
3839
#'
3940
#' datasus.init("~/datasus")
4041
datasus.init <- function(workdir = tempdir(), language = "en") {
41-
# Validate parameters
42-
if( !(language %in% c("en","pt")) )
43-
stop("Invalid language.")
44-
4542
# Create environment to store parameters
4643
if( !exists("datasus.env") ) {
4744
datasus.env <<- new.env()
4845
}
4946

47+
# Enabled languages
48+
datasus.env$valid_languages <- c("en","pt")
49+
50+
language <- tolower(language)
51+
5052
# Language used for column names
51-
datasus.env$language <- language
53+
if( !(language %in% datasus.env$valid_languages ) ) {
54+
warning("Invalid language. Using default (english).")
55+
datasus.env$language <- "en"
56+
}
57+
else {
58+
datasus.env$language <- language
59+
}
5260

5361
# Service URLs
5462
datasus.env$base_url <- "ftp://ftp.datasus.gov.br/dissemin/publicos"
5563
datasus.env$territory_url <- "ftp://ftp.datasus.gov.br/territorio/tabelas/base_territorial.zip"
64+
datasus.env$tabcnes_url <- "ftp://ftp.datasus.gov.br/dissemin/publicos/CNES/200508_/Auxiliar/tab_cnes_tabelasDBF_201512.zip"
5665

5766
# System URLs
5867
datasus.env$SIM_url <- paste(datasus.env$base_url, "SIM", sep = "/")
59-
datasus.env$CNES_url <- ""
68+
datasus.env$CNES_url <- paste(datasus.env$base_url, "CNES", "200508_", "Dados", sep = "/")
6069

6170
# Local Workspace
6271
datasus.env$workdir <- path.expand(workdir)
6372
datasus.env$tempdir <- tempdir()
6473

74+
# Local Paths
6575
datasus.env$territory_dir <- file.path(datasus.env$workdir, "territory")
76+
77+
# SIM
6678
datasus.env$SIM_dir <- file.path(datasus.env$workdir, "SIM")
6779
datasus.env$SIM.DORES_dir <- file.path(datasus.env$SIM_dir, "DORES")
6880
datasus.env$SIM.DOFET_dir <- file.path(datasus.env$SIM_dir, "DOFET")
6981
datasus.env$SIM.DOEXT_dir <- file.path(datasus.env$SIM_dir, "DOEXT")
7082
datasus.env$SIM.DOINF_dir <- file.path(datasus.env$SIM_dir, "DOINF")
7183
datasus.env$SIM.DOMAT_dir <- file.path(datasus.env$SIM_dir, "DOMAT")
72-
datasus.env$CNES_dir <- file.path(datasus.env$workdir, "CNES")
84+
85+
# CNES
86+
datasus.env$cnes_dir <- file.path(datasus.env$workdir, "CNES")
87+
datasus.env$cnes.lt_dir <- file.path(datasus.env$cnes_dir, "LT")
88+
datasus.env$cnes.st_dir <- file.path(datasus.env$cnes_dir, "ST")
89+
datasus.env$cnes.dc_dir <- file.path(datasus.env$cnes_dir, "DC")
90+
datasus.env$cnes.eq_dir <- file.path(datasus.env$cnes_dir, "EQ")
91+
datasus.env$cnes.sr_dir <- file.path(datasus.env$cnes_dir, "SR")
92+
datasus.env$cnes.hb_dir <- file.path(datasus.env$cnes_dir, "HB")
93+
datasus.env$cnes.pf_dir <- file.path(datasus.env$cnes_dir, "PF")
94+
datasus.env$cnes.ep_dir <- file.path(datasus.env$cnes_dir, "EP")
95+
datasus.env$cnes.rc_dir <- file.path(datasus.env$cnes_dir, "RC")
96+
datasus.env$cnes.in_dir <- file.path(datasus.env$cnes_dir, "IN")
97+
datasus.env$cnes.ee_dir <- file.path(datasus.env$cnes_dir, "EE")
98+
datasus.env$cnes.ef_dir <- file.path(datasus.env$cnes_dir, "EF")
99+
datasus.env$cnes.gm_dir <- file.path(datasus.env$cnes_dir, "GM")
100+
101+
# Create directory structure
73102

103+
# Helper function
74104
create.if.not.exists <- function(path, ...) {
75105
if( !dir.exists(path) )
76106
dir.create(path, ...)
77107
}
78108

79-
# Create local directory structure
109+
# Create base directory structure
80110
create.if.not.exists(datasus.env$workdir, recursive = TRUE)
111+
112+
# Territory
81113
create.if.not.exists(datasus.env$territory_dir)
114+
115+
# SIM
82116
create.if.not.exists(datasus.env$SIM_dir)
83117
create.if.not.exists(datasus.env$SIM.DORES_dir)
84118
create.if.not.exists(datasus.env$SIM.DOFET_dir)
85119
create.if.not.exists(datasus.env$SIM.DOEXT_dir)
86120
create.if.not.exists(datasus.env$SIM.DOINF_dir)
87121
create.if.not.exists(datasus.env$SIM.DOMAT_dir)
88-
create.if.not.exists(datasus.env$CNES_dir)
89-
122+
123+
# CNES
124+
create.if.not.exists(datasus.env$cnes_dir)
125+
create.if.not.exists(datasus.env$cnes.lt_dir)
126+
create.if.not.exists(datasus.env$cnes.st_dir)
127+
create.if.not.exists(datasus.env$cnes.dc_dir)
128+
create.if.not.exists(datasus.env$cnes.eq_dir)
129+
create.if.not.exists(datasus.env$cnes.sr_dir)
130+
create.if.not.exists(datasus.env$cnes.hb_dir)
131+
create.if.not.exists(datasus.env$cnes.pf_dir)
132+
create.if.not.exists(datasus.env$cnes.ep_dir)
133+
create.if.not.exists(datasus.env$cnes.rc_dir)
134+
create.if.not.exists(datasus.env$cnes.in_dir)
135+
create.if.not.exists(datasus.env$cnes.ee_dir)
136+
create.if.not.exists(datasus.env$cnes.ef_dir)
137+
create.if.not.exists(datasus.env$cnes.gm_dir)
138+
90139
# Misc
91140
datasus.env$br.uf <- c('AC', 'AL', 'AP', 'AM',
92141
'BA', 'CE', 'DF', 'ES',
@@ -97,4 +146,45 @@ datasus.init <- function(workdir = tempdir(), language = "en") {
97146
'SP', 'SE', 'TO')
98147

99148
message("DATASUS environment loaded successfuly")
149+
}
150+
151+
#' Returns datasus' Package Default Language Setup
152+
#'
153+
#' \code{datasus.lang} returns the current selected language as a two character code.
154+
#'
155+
#' @details
156+
#' The value returned by this function is set internally by a previous call to \code{\link{datasus.init}}. This value is used as a default language for every data access function of this package to define the language of the dataset column headers.
157+
#'
158+
#' To provide more flexibility, every data access function has a parameter called \code{language} that can be used to override the initial language setting at runtime.
159+
#'
160+
#' Current supported languages are "pt" (Brazilian Portuguese) and "en" (English).
161+
#'
162+
#' @note
163+
#' DATASUS is the name of the Department of Informatics of the Brazilian Unified Health System (SUS) and is resposible for publishing public healthcare data. Besides the DATASUS, the Brazilian National Agency for Supplementary Health (ANS) also uses this file format for its public data. The name DATASUS is also often used to represent the public datasets they provide.
164+
#'
165+
#' Neither this project, nor its author, has any association with the brazilian government.
166+
#' @return The default language code as set by a previous call to \code{\link{datasus.init}}
167+
#' @keywords datasus
168+
#' @export
169+
#' @author Daniela Petruzalek, \email{[email protected]}
170+
#' @seealso \code{\link{read.dbc}}
171+
#' @examples
172+
#' # Show current default language
173+
#' datasus.lang()
174+
datasus.lang <- function() {
175+
# Check if environment is loaded
176+
if( !exists("datasus.env") ) {
177+
stop("DATASUS environment not loaded. Please call datasus.init")
178+
}
179+
180+
# Return current default language
181+
datasus.env$language
182+
}
183+
184+
datasus.validate.language <- function(language) {
185+
if( !(tolower(language) %in% datasus.env$valid_languages) ) {
186+
warning("Invalid 'language' input. Using default.")
187+
language = datasus.lang()
188+
}
189+
language
100190
}

R/sim.do.R

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# sim.do.R
2+
# Copyright (C) 2016 Daniela Petruzalek
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#' Provides Access to the Declarations of Death Dataset from the Mortality Information System (SIM).
18+
#'
19+
#' \code{sim.do} returns a data.frame with a subset of the Declarations of Death (DO) dataset
20+
#'
21+
#' @details
22+
#' The Mortality Information System (SIM) offers health managers, researchers and institutions highly relevant information for defining priorities for disease prevention and control programmes, based on death statement information collected by the State Health Departments. The national Database generated from this information is administered by the Health Surveillance Secretariat in cooperation with DATASUS.
23+
#'
24+
#' The system works through the filling in and collection of a standard document, the Death Statement (Declaration of Death), which is entered onto the system in the states and municipalities. The data collected is of great importance for health surveillance and epidemiological analysis, as well as health and demographical statistics.
25+
#'
26+
#' This system contains data divided in the following categories:
27+
#'
28+
#' \itemize{
29+
#' \item DO: Declarations of death (sometimes referenced as DORES)
30+
#' \item DOFET: Declarations of death (Fetal)
31+
#' \item DOEXT: Declarations of death (External Causes)
32+
#' \item DOINF: Declarations of death (Children)
33+
#' \item DOMAT: Declarations of death (Maternal)
34+
#' }
35+
#'
36+
#' The \code{sim.*} functions are provided for each available subsystem, for example, \code{sim.dofet}, \code{sim.doext} and so on.
37+
#'
38+
#' @note
39+
#' DATASUS is the name of the Department of Informatics of the Brazilian Unified Health System (SUS) and is resposible for publishing public healthcare data. Besides the DATASUS, the Brazilian National Agency for Supplementary Health (ANS) also uses this file format for its public data. The name DATASUS is also often used to represent the public datasets they provide.
40+
#'
41+
#' Neither this project, nor its author, has any association with the brazilian government.
42+
#' @param years numeric; one or more years representing the data to be read
43+
#' @param states character; one or more state (UF) representing the location of the data to be read, or 'ALL' if you want to read data from all states (UFs) at the same time.
44+
#' @param language character; column names in Portuguese ("pt") or English ("en"). Default is "en".
45+
#' @return a data.frame with Brazil's mortality data
46+
#' @keywords datasus
47+
#' @export
48+
#' @author Daniela Petruzalek, \email{[email protected]}
49+
#' @seealso \code{\link{datasus.init}} \code{\link{read.dbc}}
50+
#' @examples
51+
#'
52+
#' pr10 <- sim.dores(2010, 'PR')
53+
sim.do <- function(years, states, language = datasus.lang()) {
54+
sim.load("DO", years, language = language)
55+
}

0 commit comments

Comments
 (0)