Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
migconforto authored Aug 29, 2022
1 parent a4430da commit 59b57f7
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 378 deletions.
24 changes: 14 additions & 10 deletions R/baixa_pnadc.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Download PNADc -------------------------------------------------------------
#' pnadc_download
#'
#' @description download the PNADc version and create a design directly on the computer.
#' @description download the PNADc version and create a survey design directly on your computer. Saves an external representation of an object in \R that will be read later in other functions of PNADc.table
#' @description [Documentation in English](https://github.com/migux14/PNADc.table/tree/main/vignettes)
#' @description [Documentation in Portuguese - BR](https://github.com/migux14/PNADc.table/tree/main/Documents%20PT-BR)
#'
#' @param year The year you want to download. Must be a number between 2012 and the current year. It does not accept a vector of years.
#' @param quartile The quartile of the year you want to download. Must be a number between 1 and 4. It does not accept a vector of quartiles.
Expand All @@ -11,31 +13,33 @@
#' @importFrom PNADcIBGE read_pnadc
#' @importFrom PNADcIBGE pnadc_design
#'
#' @export
#' @return No return value
#'
#' @examples pnadc_download(2018, 1)
#' @examples
#'\donttest{pnadc_download(2018, 1)}
#' #A file named "Design_PNADc_2018_1" will be created directly in the computer's local directory
pnadc_download <- function(year, quartile, path = NULL) {
a <- webshot2::`%>%`(1,sum())
attachNamespace("PNADcIBGE")
if (is.null(path)) {
if (file.exists(fs::path_home(paste("Design_PNADc", year, quartile, sep = "_"))) == T) {
print("This edition of the PNADc has already been downloaded and can be used.")
if (file.exists(fs::path_home(paste("Design_PNADc", year, quartile, sep = "_"))) == TRUE) {
message("This edition of the PNADc has already been downloaded and can be used.")
}
else {
design_PNADc <- PNADcIBGE::get_pnadc(year, quarter = quartile, design = T)
design_PNADc <- PNADcIBGE::get_pnadc(year, quarter = quartile, design = TRUE)

file <- paste("Design_PNADc", year, quartile, sep = "_")
save(design_PNADc, file = fs::path_home(file))
}
}
else {
if (file.exists(paste(path, "/Design_PNADc_", year, "_", quartile, sep = "")) == T) {
print("This edition of the PNADc has already been downloaded and can be used.")
if (file.exists(paste(path, "/Design_PNADc_", year, "_", quartile, sep = "")) == TRUE) {
message("This edition of the PNADc has already been downloaded and can be used.")

save(path, file = fs::path_home(paste("path_PNADcTABLE", year, quartile, sep = "_")))
}
else {
if (file.exists(paste(path, "/PNADC_0", quartile, year, ".txt", sep = "")) == T) {
if (file.exists(paste(path, "/PNADC_0", quartile, year, ".txt", sep = "")) == TRUE) {
PNAD_txt <- paste(path, "/PNADC_0", quartile, year, ".txt", sep = "")
INPUT_sas <- paste(path, "/input_PNADC_trimestral.sas", sep = "")
pnadc <- PNADcIBGE::read_pnadc(PNAD_txt, INPUT_sas)
Expand All @@ -46,7 +50,7 @@ pnadc_download <- function(year, quartile, path = NULL) {
save(path, file = fs::path_home(paste("path_PNADcTABLE", year, quartile, sep = "_")))
}
else {
design_PNADc <- PNADcIBGE::get_pnadc(year, quarter = quartile, design = T)
design_PNADc <- PNADcIBGE::get_pnadc(year, quarter = quartile, design = TRUE)

path <- paste(path, "/Design_PNADc_", year, "_", quartile, sep = "")
save(design_PNADc, file = path)
Expand Down
277 changes: 139 additions & 138 deletions R/pnadc_faixas.R

Large diffs are not rendered by default.

238 changes: 120 additions & 118 deletions R/pnadc_grafico.R

Large diffs are not rendered by default.

62 changes: 32 additions & 30 deletions R/pnadc_media.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#' pnadc_mean
#'
#' @description Create PNADc mean tables
#' @description [Documentation in English](https://github.com/migux14/PNADc.table/tree/main/vignettes)
#' @description [Documentation in Portuguese - BR](https://github.com/migux14/PNADc.table/tree/main/Documents%20PT-BR)
#'
#' @param variable Variable of interest that will be used to calculate the mean.
#' @param filter Variable that defines the aggregation level of the variable of interest. It can contain more than one level of aggregation.
Expand All @@ -21,16 +23,16 @@
#' @return gt table.
#' @export
#'
#' @examples pnadc_mean(~V403312, ~UF+V2010, 2019, 1)
pnadc_mean <- function(variable, filter, year, quartile, path = F, export = F) {
#' @examples \donttest{pnadc_mean(~V403312, ~UF+V2010, 2019, 1)}
pnadc_mean <- function(variable, filter, year, quartile, path = FALSE, export = FALSE) {
design_PNADc <- NULL
if (path == F) {
if (file.exists(fs::path_home(paste("Design","PNADc", year, quartile, sep = "_"))) == T) {
if (path == FALSE) {
if (file.exists(fs::path_home(paste("Design","PNADc", year, quartile, sep = "_"))) == TRUE) {
Design <- paste("Design","PNADc", year, quartile, sep = "_")
load(fs::path_home(Design))

tabela <- survey::svyby(formula = variable, by = filter, design = design_PNADc, FUN = survey::svymean, na.rm = T, na.rm.by = T, na.rm.all = T)
med.geral <- survey::svymean(x = variable, design = design_PNADc, na.rm = T)
tabela <- survey::svyby(formula = variable, by = filter, design = design_PNADc, FUN = survey::svymean, na.rm = TRUE, na.rm.by = TRUE, na.rm.all = TRUE)
med.geral <- survey::svymean(x = variable, design = design_PNADc, na.rm = TRUE)
tabela <- as.data.frame(tabela)
tabela <- rbind(tabela, med.geral)

Expand Down Expand Up @@ -78,22 +80,22 @@ pnadc_mean <- function(variable, filter, year, quartile, path = F, export = F) {
missing_text = "General Average")
}

if (export == F) {
if (export == FALSE) {
return(tabela_final)
}
if (export == "df") {
return(tabela)
}
if (export == "csv") {
utils::write.csv2(tabela, file = paste(fs::path_home(),"/Tabela_pnadc_mean_", year, "_", quartile,".csv", sep = ""))
print(paste("Saved in directory: ",fs::path_home(), sep = ""))
message(paste("Saved in directory: ",fs::path_home(), sep = ""))
} else {
gt::gtsave(tabela_final, filename = paste("Tabela_pnadc_mean_", year, "_", quartile,".", export, sep = ""), path = fs::path_home())
print(paste("Saved in directory: ",fs::path_home(), sep = ""))
message(paste("Saved in directory: ",fs::path_home(), sep = ""))
}

rm(list = c("tabela", "tabela_final", "grupo"))
gc(reset = T)
gc(reset = TRUE)

}
else {
Expand All @@ -102,8 +104,8 @@ pnadc_mean <- function(variable, filter, year, quartile, path = F, export = F) {
Design <- paste("Design","PNADc", year, quartile, sep = "_")
load(fs::path_home(Design))

tabela <- survey::svyby(formula = variable, by = filter, design = design_PNADc, FUN = survey::svymean, na.rm = T, na.rm.by = T, na.rm.all = T)
med.geral <- survey::svymean(x = variable, design = design_PNADc, na.rm = T)
tabela <- survey::svyby(formula = variable, by = filter, design = design_PNADc, FUN = survey::svymean, na.rm = TRUE, na.rm.by = TRUE, na.rm.all = TRUE)
med.geral <- survey::svymean(x = variable, design = design_PNADc, na.rm = TRUE)
tabela <- as.data.frame(tabela)
tabela <- rbind(tabela, med.geral)

Expand Down Expand Up @@ -151,36 +153,36 @@ pnadc_mean <- function(variable, filter, year, quartile, path = F, export = F) {
missing_text = "General Average")
}

if (export == F) {
if (export == FALSE) {
return(tabela_final)
}
if (export == "df") {
return(tabela)
}
if (export == "csv") {
utils::write.csv2(tabela, file = paste(fs::path_home(),"/Tabela_pnadc_mean_", year, "_", quartile,".csv", sep = ""))
print(paste("Saved in directory: ",fs::path_home(), sep = ""))
message(paste("Saved in directory: ",fs::path_home(), sep = ""))
} else {
gt::gtsave(tabela_final, filename = paste("Tabela_pnadc_mean_", year, "_", quartile,".", export, sep = ""), path = fs::path_home())
print(paste("Saved in directory: ",fs::path_home(), sep = ""))
message(paste("Saved in directory: ",fs::path_home(), sep = ""))
}

rm(list = c("tabela", "tabela_final", "grupo"))
gc(reset = T)
gc(reset = TRUE)

}
}
else {
path_file <- fs::path_home(paste("path_PNADcTABLE", year, quartile, sep = "_"))

if (file.exists(path_file) == T) {
if (file.exists(path_file) == TRUE) {
load(path_file)
local_file <- paste(path, "/Design_PNADc_", year, "_", quartile, sep = "")
load(local_file)


tabela <- survey::svyby(formula = variable, by = filter, design = design_PNADc, FUN = survey::svymean, na.rm = T, na.rm.by = T, na.rm.all = T)
med.geral <- survey::svymean(x = variable, design = design_PNADc, na.rm = T)
tabela <- survey::svyby(formula = variable, by = filter, design = design_PNADc, FUN = survey::svymean, na.rm = TRUE, na.rm.by = TRUE, na.rm.all = TRUE)
med.geral <- survey::svymean(x = variable, design = design_PNADc, na.rm = TRUE)
tabela <- as.data.frame(tabela)
tabela <- rbind(tabela, med.geral)

Expand Down Expand Up @@ -228,25 +230,25 @@ pnadc_mean <- function(variable, filter, year, quartile, path = F, export = F) {
missing_text = "General Average")
}

if (export == F) {
if (export == FALSE) {
return(tabela_final)
}
if (export == "df") {
return(tabela)
}
if (export == "csv") {
utils::write.csv2(tabela, file = paste(path, "/Tabela_pnadc_mean_", year, "_", quartile,".csv", sep = ""))
print(paste("Saved in directory: ", path, sep = ""))
message(paste("Saved in directory: ", path, sep = ""))
} else {
gt::gtsave(tabela_final, filename = paste("Tabela_pnadc_mean_", year, "_", quartile,".", export, sep = ""), path = path)
print(paste("Saved in directory: ", path, sep = ""))
message(paste("Saved in directory: ", path, sep = ""))
}

rm(list = c("tabela", "tabela_final", "grupo"))
gc(reset = T)
gc(reset = TRUE)
}
else {
if (file.exists(fs::path_home(paste("Design","PNADc", year, quartile, sep = "_"))) == T) {
if (file.exists(fs::path_home(paste("Design","PNADc", year, quartile, sep = "_"))) == TRUE) {
Design <- paste("Design","PNADc", year, quartile, sep = "_")
load(fs::path_home(Design))
} else {
Expand All @@ -256,8 +258,8 @@ pnadc_mean <- function(variable, filter, year, quartile, path = F, export = F) {
load(paste(path, Design, sep = ""))
}

tabela <- survey::svyby(formula = variable, by = filter, design = design_PNADc, FUN = survey::svymean, na.rm = T, na.rm.by = T, na.rm.all = T)
med.geral <- survey::svymean(x = variable, design = design_PNADc, na.rm = T)
tabela <- survey::svyby(formula = variable, by = filter, design = design_PNADc, FUN = survey::svymean, na.rm = TRUE, na.rm.by = TRUE, na.rm.all = TRUE)
med.geral <- survey::svymean(x = variable, design = design_PNADc, na.rm = TRUE)
tabela <- as.data.frame(tabela)
tabela <- rbind(tabela, med.geral)

Expand Down Expand Up @@ -305,22 +307,22 @@ pnadc_mean <- function(variable, filter, year, quartile, path = F, export = F) {
missing_text = "General Average")
}

if (export == F) {
if (export == FALSE) {
return(tabela_final)
}
if (export == "df") {
return(tabela)
}
if (export == "csv") {
utils::write.csv2(tabela, file = paste(path, "/Tabela_pnadc_mean_", year, "_", quartile,".csv", sep = ""))
print(paste("Saved in directory: ", path, sep = ""))
message(paste("Saved in directory: ", path, sep = ""))
} else {
gt::gtsave(tabela_final, filename = paste("Tabela_pnadc_mean_", year, "_", quartile,".", export, sep = ""), path = path)
print(paste("Saved in directory: ", path, sep = ""))
message(paste("Saved in directory: ", path, sep = ""))
}

rm(list = c("tabela", "tabela_final", "grupo"))
gc(reset = T)
gc(reset = TRUE)
}
}
}
Loading

0 comments on commit 59b57f7

Please sign in to comment.