Skip to content

Commit

Permalink
streamline even further
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Sep 4, 2024
1 parent ba43288 commit 4afe7ab
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 122 deletions.
5 changes: 2 additions & 3 deletions r-package/R/read_immediate_region.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,22 @@ read_immediate_region <- function(code_immediate = "all",
# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }

## FILTERS
y <- code_immediate

# check code_immediate input
if(code_immediate=="all"){

# abbrev_state
} else if(code_immediate %in% temp_sf$abbrev_state){
y <- code_immediate
temp_sf <- subset(temp_sf, abbrev_state == y)

# code_state
} else if(code_immediate %in% temp_sf$code_state){
y <- code_immediate
temp_sf <- subset(temp_sf, code_state == y)

# code_immediate
} else if(code_immediate %in% temp_sf$code_immediate){
y <- code_immediate
temp_sf <- subset(temp_sf, code_immediate == y)

} else {stop(paste0("Error: Invalid Value to argument 'code_immediate'",collapse = " "))}
Expand Down
6 changes: 3 additions & 3 deletions r-package/R/read_intermediate_region.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ read_intermediate_region <- function(code_intermediate = "all",
# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }

## FILTERS
y <- code_intermediate

# input "all"
if(code_intermediate=="all"){

# abbrev_state
} else if(code_intermediate %in% temp_sf$abbrev_state){
y <- code_intermediate
temp_sf <- subset(temp_sf, abbrev_state == y)

# code_state
} else if(code_intermediate %in% temp_sf$code_state){
y <- code_intermediate
temp_sf <- subset(temp_sf, code_state == y)

# code_intermediate
} else if(code_intermediate %in% temp_sf$code_intermediate){
y <- code_intermediate
temp_sf <- subset(temp_sf, code_intermediate == y)

} else {stop(paste0("Error: Invalid Value to argument 'code_intermediate'",collapse = " "))}
Expand Down
38 changes: 14 additions & 24 deletions r-package/R/read_meso_region.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,25 @@ read_meso_region <- function(code_meso = "all",
# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }

# return all municipalities
if (code_meso =='all' ){
return(temp_sf)
}
## FILTERS
y <- code_meso

# FILTER particular region
x <- code_meso
# input "all"
if(code_meso=="all"){

if (!any(code_meso %in% temp_sf$code_meso |
code_meso %in% temp_sf$code_state |
code_meso %in% temp_sf$abbrev_state)) {
stop("Error: Invalid value to argument code_meso.")
}
# abbrev_state
} else if(code_meso %in% temp_sf$abbrev_state){
temp_sf <- subset(temp_sf, abbrev_state == y)

# particular state
if(nchar(code_meso)==2){
# code_state
} else if(code_meso %in% temp_sf$code_state){
temp_sf <- subset(temp_sf, code_state == y)

if (is.numeric(code_meso)) {
temp_sf <- subset(temp_sf, code_state == x)
}
# code_meso
} else if(code_meso %in% temp_sf$code_meso){
temp_sf <- subset(temp_sf, code_meso == y)

if (is.character(code_meso)) {
temp_sf <- subset(temp_sf, abbrev_state == x)
}
}
} else {stop(paste0("Error: Invalid Value to argument 'code_meso'",collapse = " "))}

# particular meso
if(nchar(code_meso)==4 & is.numeric(code_meso)){
temp_sf <- subset(temp_sf, code_meso == x)
}
return(temp_sf)
}
38 changes: 14 additions & 24 deletions r-package/R/read_micro_region.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,25 @@ read_micro_region <- function(code_micro = "all",
# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }

# return all municipalities
if (code_micro =='all' ){
return(temp_sf)
}
## FILTERS
y <- code_micro

# FILTER particular region
x <- code_micro
# input "all"
if(code_micro=="all"){

if (!any(code_micro %in% temp_sf$code_micro |
code_micro %in% temp_sf$code_state |
code_micro %in% temp_sf$abbrev_state)) {
stop("Error: Invalid value to argument code_micro.")
}
# abbrev_state
} else if(code_micro %in% temp_sf$abbrev_state){
temp_sf <- subset(temp_sf, abbrev_state == y)

# particular state
if(nchar(code_micro)==2){
# code_state
} else if(code_micro %in% temp_sf$code_state){
temp_sf <- subset(temp_sf, code_state == y)

if (is.numeric(code_micro)) {
temp_sf <- subset(temp_sf, code_state == x)
}
# code_micro
} else if(code_micro %in% temp_sf$code_micro){
temp_sf <- subset(temp_sf, code_micro == y)

if (is.character(code_micro)) {
temp_sf <- subset(temp_sf, abbrev_state == x)
}
}
} else {stop(paste0("Error: Invalid Value to argument 'code_micro'",collapse = " "))}

# particular micro
if(nchar(code_micro)==5 & is.numeric(code_micro)){
temp_sf <- subset(temp_sf, code_micro == x)
}
return(temp_sf)
}
41 changes: 15 additions & 26 deletions r-package/R/read_municipality.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,25 @@ read_municipality <- function(code_muni = "all",
# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }

# return all municipalities
if (code_muni =='all' ){
return(temp_sf)
}

# FILTER particular state or muni
x <- code_muni
## FILTERS
y <- code_muni

if (!any(code_muni %in% temp_sf$code_muni |
code_muni %in% temp_sf$code_state |
code_muni %in% temp_sf$abbrev_state)) {
stop("Error: Invalid Value to argument code_muni.")
}
# input "all"
if(code_muni=="all"){

# abbrev_state
} else if(code_muni %in% temp_sf$abbrev_state){
temp_sf <- subset(temp_sf, abbrev_state == y)

# particular state
if(nchar(code_muni)==2){
# code_state
} else if(code_muni %in% temp_sf$code_state){
temp_sf <- subset(temp_sf, code_state == y)

if (is.numeric(code_muni)) {
temp_sf <- subset(temp_sf, code_state == x)
}
# code_muni
} else if(code_muni %in% temp_sf$code_muni){
temp_sf <- subset(temp_sf, code_muni == y)

if (is.character(code_muni)) {
temp_sf <- subset(temp_sf, abbrev_state == x)
}
}
} else {stop(paste0("Error: Invalid Value to argument 'code_muni'",collapse = " "))}

# particular muni
if(nchar(code_muni)>2 & is.numeric(code_muni)){
temp_sf <- subset(temp_sf, code_muni == x)
}
return(temp_sf)
}
}
29 changes: 12 additions & 17 deletions r-package/R/read_state.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,21 @@ read_state <- function(code_state = "all",
# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }

# data files before 1992 do not have state code nor state abbrev
if (year < 1992 | code_state =='all' ){
return(temp_sf)
}
## FILTERS
y <- code_state

# FILTER particular state
x <- code_state
# input "all" & data files before 1992 do not have state code nor state abbrev
if (year < 1992 | code_state=="all") {

if (!any(x %in% temp_sf$code_state | x %in% temp_sf$abbrev_state)) {
stop("Error: Invalid Value to argument code_state.")
}
# abbrev_state
} else if(code_state %in% temp_sf$abbrev_state){
temp_sf <- subset(temp_sf, abbrev_state == y)

if (is.numeric(code_state)) {
temp_sf <- subset(temp_sf, code_state == x)
}

if (is.character(code_state)) {
temp_sf <- subset(temp_sf, abbrev_state == x)
}
# code_state
} else if(code_state %in% temp_sf$code_state){
temp_sf <- subset(temp_sf, code_state == y)

} else {stop(paste0("Error: Invalid Value to argument 'read_state'",collapse = " "))}

return(temp_sf)
}
}
39 changes: 14 additions & 25 deletions r-package/R/read_weighting_area.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,25 @@ read_weighting_area <- function(code_weighting = "all",
# check if download failed
if (is.null(temp_sf)) { return(invisible(NULL)) }

# return all municipalities
if (code_weighting =='all') {
return(temp_sf)
}

# FILTER particular state or muni
x <- code_weighting
## FILTERS
y <- code_weighting

if (!any(code_weighting %in% temp_sf$code_muni |
code_weighting %in% temp_sf$code_state |
code_weighting %in% temp_sf$abbrev_state)) {
stop("Error: Invalid Value to argument code_weighting.")
}
# input "all"
if(code_weighting=="all"){

# abbrev_state
} else if(code_weighting %in% temp_sf$abbrev_state){
temp_sf <- subset(temp_sf, abbrev_state == y)

# particular state
if(nchar(code_weighting)==2){
# code_state
} else if(code_weighting %in% temp_sf$code_state){
temp_sf <- subset(temp_sf, code_state == y)

if (is.numeric(code_weighting)) {
temp_sf <- subset(temp_sf, code_state == x)
}
# code_muni
} else if(code_weighting %in% temp_sf$code_muni){
temp_sf <- subset(temp_sf, code_muni == y)

if (is.character(code_weighting)) {
temp_sf <- subset(temp_sf, abbrev_state == x)
}
}
} else {stop(paste0("Error: Invalid Value to argument 'code_weighting'",collapse = " "))}

# particular muni
if(nchar(code_weighting)==7 & is.numeric(code_weighting)){
temp_sf <- subset(temp_sf, code_muni == x)
}
return(temp_sf)
}
3 changes: 3 additions & 0 deletions r-package/tests/tests_rafa/test_rafa.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@



#### testing functions of geobr

#library(magrittr)
Expand Down

0 comments on commit 4afe7ab

Please sign in to comment.