Skip to content

Commit

Permalink
using styler::tidyverse_style
Browse files Browse the repository at this point in the history
  • Loading branch information
dlizcano committed May 22, 2024
1 parent 0df8d52 commit 256de54
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 194 deletions.
77 changes: 40 additions & 37 deletions R/mammalmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,70 +25,73 @@
#' @importFrom mammalcol taxon
#' @importFrom mammalcol colmap
#' @export
mammalmap <- function(species, legend=TRUE){

if (!requireNamespace("ggplot2", quietly = TRUE))
mammalmap <- function(species, legend = TRUE) {
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
if (!requireNamespace("sf", quietly = TRUE))
}
if (!requireNamespace("sf", quietly = TRUE)) {
install.packages("sf")

}

if (missing(species)) {
stop("Argument species was not included")
}

if (!is.character(species)) {
stop(paste0("Argument species must be a character, not ", class(species)))
}

if (!is.logical(legend)) {
stop(paste0("Argument legend must be logical, not ", class(legend)))
}

# require("ggplot2")
# require("sf")
#load("data/colmap.rda")
#load("data/taxon.rda")
#data(mammalcol::taxon)
#data(mammalcol::colmap)

# load("data/colmap.rda")
# load("data/taxon.rda")

# data(mammalcol::taxon)
# data(mammalcol::colmap)

distribution_list <-
strsplit(taxon$distribution, "\\|") # trimws () removes spaces
deptos <- as.data.frame(cbind(Depto=unique(colmap$NAME_1), fill="white"))

deptos <- as.data.frame(cbind(Depto = unique(colmap$NAME_1), fill = "white"))
sp_id <- which(taxon$scientificName == species)
unos <- trimws(distribution_list[[ sp_id ]]) # species number
unos <- trimws(distribution_list[[sp_id]]) # species number

# nested loop to get deptos
for (i in 1:length(deptos[,1])){
for (j in 1:length(unos)){
if(deptos$Depto[i]==unos[j]){deptos$fill[i] <- "blue"}
for (i in 1:length(deptos[, 1])) {
for (j in 1:length(unos)) {
if (deptos$Depto[i] == unos[j]) {
deptos$fill[i] <- "blue"
}
}
}

# make the map
# if legend true
if(legend==TRUE) {
mapa <- ggplot2::ggplot(colmap) +
# if legend true
if (legend == TRUE) {
mapa <- ggplot2::ggplot(colmap) +
ggplot2::geom_sf(ggplot2::aes(fill = NAME_1)) +
ggplot2::scale_fill_manual(values = deptos$fill) +
# ggtitle(taxon$scientificName[25]) + #species name number
ggplot2::labs(subtitle = taxon$scientificName[sp_id])+
ggplot2::theme(legend.position="right", # location legend
legend.title = ggplot2::element_blank(),#element_text(size=7),#,
legend.text = ggplot2::element_text(size=8,), # text depto size
plot.subtitle = ggplot2::element_text(face = "italic") # italica
ggplot2::labs(subtitle = taxon$scientificName[sp_id]) +
ggplot2::theme(
legend.position = "right", # location legend
legend.title = ggplot2::element_blank(), # element_text(size=7),#,
legend.text = ggplot2::element_text(size = 8, ), # text depto size
plot.subtitle = ggplot2::element_text(face = "italic") # italica
)
}else{ # if legend false
mapa <- ggplot2::ggplot(colmap) +
} else { # if legend false
mapa <- ggplot2::ggplot(colmap) +
ggplot2::geom_sf(ggplot2::aes(fill = NAME_1), show.legend = FALSE) + # removes legend
ggplot2::scale_fill_manual(values = deptos$fill) +
# ggtitle(taxon$scientificName[25]) + #species name number
ggplot2::labs(subtitle = taxon$scientificName[sp_id]) +
ggplot2::theme(plot.subtitle = ggplot2::element_text(face = "italic")
)# italica
ggplot2::theme(plot.subtitle = ggplot2::element_text(face = "italic")) # italica
}
return(mapa)

return(mapa)
} # end function
11 changes: 5 additions & 6 deletions R/onload.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

.onAttach <- function(lib, pkg) {
packageStartupMessage("This is mammalcol ",
utils::packageDescription("mammalcol",
fields = "Version"
),
appendLF = TRUE
utils::packageDescription("mammalcol",
fields = "Version"
),
appendLF = TRUE
)
}

Expand All @@ -20,7 +19,7 @@ show_progress <- function() {

.onLoad <- function(libname, pkgname) {
opt <- options()
opt_mammalcol<- list(
opt_mammalcol <- list(
mammalcol.show_progress = TRUE
)
to_set <- !(names(opt_mammalcol) %in% names(opt))
Expand Down
48 changes: 25 additions & 23 deletions R/search_mammalcol.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,30 @@ search_mammalcol <- function(splist, max_distance = 0.2) {
if (missing(splist)) {
stop("Argument splist was not defined")
}

if (is.factor(splist)) {
splist <- as.character(splist)
}
# Fix species name
splist_st <- standardize_names(splist)
dupes_splist_st <- find_duplicates(splist_st)

if(length(dupes_splist_st) != 0 ){
message("The following names are repeated in the 'splist': ",
paste(dupes_splist_st, collapse = ", "))
if (length(dupes_splist_st) != 0) {
message(
"The following names are repeated in the 'splist': ",
paste(dupes_splist_st, collapse = ", ")
)
}
splist_std <- unique(splist_st)

# create an output data container
output_matrix <- matrix(nrow = length(splist_std), ncol = 21) # two more
colnames(output_matrix) <- c("name_submitted",
names(taxon),
"Distance")

colnames(output_matrix) <- c(
"name_submitted",
names(taxon),
"Distance"
)

# loop code to find the matching string

for (i in seq_along(splist_std)) {
Expand All @@ -62,32 +66,31 @@ search_mammalcol <- function(splist, max_distance = 0.2) {

# fuzzy and exact match
matches <- agrep(splist_std[i],
taxon$scientificName, # base data column
max.distance = max_distance_fixed,
value = TRUE)
taxon$scientificName, # base data column
max.distance = max_distance_fixed,
value = TRUE
)

# check non matching result
if (length(matches) == 0) {
row_data <- rep("nill", 19) # number of columns
}
else if (length(matches) != 0){ # match result
} else if (length(matches) != 0) { # match result
dis_value <- as.numeric(utils::adist(splist_std[i], matches))
matches1 <- matches[dis_value <= max_distance_fixed]
dis_val_1 <- dis_value[dis_value <= max_distance_fixed]

if (length(matches1) == 0){
if (length(matches1) == 0) {
row_data <- rep("nill", 19) # number of columns
}
else if (length(matches1) != 0){
row_data <- as.matrix(taxon[taxon$scientificName %in% matches1,])
} else if (length(matches1) != 0) {
row_data <- as.matrix(taxon[taxon$scientificName %in% matches1, ])
}
}

# distance value
if(is.null(nrow(row_data))){
if (is.null(nrow(row_data))) {
dis_value_1 <- "nill"
} else{
dis_value_1 <- utils::adist(splist_std[i], row_data[,2])
} else {
dis_value_1 <- utils::adist(splist_std[i], row_data[, 2])
}

output_matrix[i, ] <-
Expand All @@ -97,7 +100,6 @@ search_mammalcol <- function(splist, max_distance = 0.2) {
# Output
output <- as.data.frame(output_matrix)
# rownames(output) <- NULL
output <- output[,-2]# delete the id column
return(output[output$scientificName != "nill",])
output <- output[, -2] # delete the id column
return(output[output$scientificName != "nill", ])
}

8 changes: 4 additions & 4 deletions R/sp_by_depto.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ sp_by_depto <- function(states, type = c("any", "only", "all"), taxa = NULL) {
if (length(states) == 0) stop("Please provide at least one Colombian Departamento")
type <- match.arg(type)
states <- sort(states)

# states <- paste("BR-", states, sep = "")
if (length(states) == 0) stop("Please provide at least one Colombian Departamento")
# res <- lapply(occurrences, match, states)
if (type == "any") {
#res <- lapply(res, function(x) any(!is.na(x)))
# res <- lapply(res, function(x) any(!is.na(x)))
res <- subset(distribution, grepl(paste(states, collapse = "|"), locality))
}
if (type == "only") {
Expand All @@ -50,10 +50,10 @@ sp_by_depto <- function(states, type = c("any", "only", "all"), taxa = NULL) {
return(NA)
}
if (is.null(taxa)) {
merge(taxon[, c("scientificName", "family", "order", "id")], res[, c("id", "locality")], by = "id")[,-1]
merge(taxon[, c("scientificName", "family", "order", "id")], res[, c("id", "locality")], by = "id")[, -1]
# removes id
} else {
merge(taxon[taxon$order %in% taxa, c("scientificName", "family", "order", "id" )], res[, c("id", "locality")], by = "id")[,-1]
merge(taxon[taxon$order %in% taxa, c("scientificName", "family", "order", "id")], res[, c("id", "locality")], by = "id")[, -1]
# removes id
}
}
Loading

0 comments on commit 256de54

Please sign in to comment.