diff --git a/.Rbuildignore b/.Rbuildignore index 5fae00b..24f02dc 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -3,3 +3,4 @@ LICENSE.md _pkgdown.yml ^\.github$ +.lintr diff --git a/.lintr b/.lintr new file mode 100644 index 0000000..84cecc3 --- /dev/null +++ b/.lintr @@ -0,0 +1,7 @@ +linters: linters_with_defaults( + line_length_linter(250), + commented_code_linter = NULL, + trailing_whitespace_linter(allow_empty_lines = TRUE), + object_name_linter = NULL + ) +encoding: "UTF-8" diff --git a/R/build_Lifemap.R b/R/build_Lifemap.R index 576ea02..73b9645 100644 --- a/R/build_Lifemap.R +++ b/R/build_Lifemap.R @@ -4,7 +4,7 @@ #' The dataframe can contain any number of additional columns defining traits/characters/values associated to #' each taxid. #' @param basemap The chosen basemap for Lifemap ("fr", "ncbi", "base" or "virus"). -#' @param verbose If TRUE (the default), the function will print detailed information to the console. +#' @param verbose If TRUE (the default), the function will print detailed information to the console. #' If FALSE, it will run silently. #' #' @return A list of class lifemap_obj containing: @@ -16,7 +16,7 @@ #' - A list of its ascendants (ascend) #' - Its type ("requested" or "ancestor") #' - Its direct ancestor -#' - Its type (type), i.e. whether the taxid was +#' - Its type (type), i.e. whether the taxid was #' requested by the user ("requested") or if it is the anecestor of a requested taxid ("ancestor") #' - basemap : the basemap used to get taxa's details #' @@ -34,16 +34,16 @@ #' LM <- build_Lifemap(eukaryotes_80, "fr") #' } build_Lifemap <- function(df, basemap = c("ncbi", "base", "fr", "virus"), verbose = TRUE) { - + basemap <- match.arg(arg = basemap, choices = basemap) if (is.null(df$taxid)) { stop('The dataframe must at least contain a "taxid" column') } # create a new "environment" to store the full data - if (!exists("lifemap_basemap_envir", where = .GlobalEnv)) { + if (!exists("lifemap_basemap_envir", where = .GlobalEnv)) { lifemap_basemap_envir <- new.env() } - + ## SET DATASETS ASDRESSES # getting the right URL depending on the basemap wanted if (basemap == "ncbi") { @@ -59,7 +59,7 @@ build_Lifemap <- function(df, basemap = c("ncbi", "base", "fr", "virus"), verbos else { stop(sprintf('%s is not a working basemap, try c("base", "fr", "ncbi" or "virus")', basemap)) } - + y <- tryCatch({ load(url(basemap_url), envir = lifemap_basemap_envir) }, @@ -72,61 +72,61 @@ build_Lifemap <- function(df, basemap = c("ncbi", "base", "fr", "virus"), verbos return(NA) } ) - + if (!is.na(y)) { # download full data for chosen basemap if (verbose) { cat("Downloading basemap coordinates...\n") } load(url(basemap_url), envir = lifemap_basemap_envir) - + # add LUCA LUCA <- data.frame("taxid" = 0, "lon" = 0, "lat" = -4.226497, "sci_name" = "Luca", "zoom" = 5) lifemap_basemap_envir$DF <- dplyr::bind_rows(lifemap_basemap_envir$DF, LUCA) - + # get info for unique taxids (then we work with df_distinct, not df anymore) df_distinct <- dplyr::distinct(df, .data$taxid, .keep_all = TRUE) if (nrow(df_distinct) != nrow(df)) { - warning(sprintf("%s duplicated TaxIDs were removed \n", nrow(df) - nrow(df_distinct))) + warning(sprintf("%s duplicated TaxIDs were removed \n", nrow(df) - nrow(df_distinct))) } - + # get data if (verbose) { cat("Getting info for requested taxids...\n") } - + # get index of requested taxids indexes <- fastmatch::fmatch(df_distinct$taxid, lifemap_basemap_envir$DF$taxid) if (sum(is.na(indexes)) > 0) { - warning(sprintf("%s TaxID(s) could not be found: %s \n", - sum(is.na(indexes)), - paste(df_distinct$taxid[is.na(indexes)], sep = ","))) + warning(sprintf("%s TaxID(s) could not be found: %s \n", + sum(is.na(indexes)), + paste(df_distinct$taxid[is.na(indexes)], sep = ","))) } - + # create new df with only existing taxids df_exists <- df_distinct[!is.na(indexes), ] DATA0 <- lifemap_basemap_envir$DF[indexes[!is.na(indexes)], ] - + # get ancestors unique_ancestors <- unique(unlist(DATA0$ascend)) real_ancestors <- setdiff(unique_ancestors, df_exists$taxid) ANCESTORS <- lifemap_basemap_envir$DF[fastmatch::fmatch(real_ancestors, lifemap_basemap_envir$DF$taxid), ] - + # add type DATA0$type <- "requested" ANCESTORS$type <- "ancestor" # bind all DATA1 <- rbind(DATA0, ANCESTORS) - - # merge + + # merge DATA2 <- merge(DATA1, df_exists, by = "taxid", all = TRUE) - + # replace the column 'ascend' by simply the direct ancestor DATA2$ancestor <- unlist(lapply(DATA2$ascend, function(x) ifelse(!is.null(x), x[1], NA))) - + lm_obj <- list(df = DATA2, basemap = basemap) class(lm_obj) <- c("lifemap_obj", "list") - + return(lm_obj) } else { return(NA) diff --git a/R/create_matrix.R b/R/create_matrix.R index 50d05f6..3e81a72 100644 --- a/R/create_matrix.R +++ b/R/create_matrix.R @@ -15,10 +15,10 @@ #' #' create_matrix(LM_eukaryotes$df, c("GC.", "Genes")) create_matrix <- function(df, cols) { - a <- sapply(1:nrow(df), - function(x,y) { + a <- sapply(seq_len(nrow(df)), + function(x, y) { cbind(y$taxid[x], c(y$taxid[x], y$ascend[x][[1]])) - }, + }, y = df) a <- a[-length(a)] B <- do.call(rbind, a) @@ -30,7 +30,7 @@ create_matrix <- function(df, cols) { new_df <- dplyr::full_join(new_df, df[, c("taxid", var)], by = dplyr::join_by("descendant" == "taxid")) } } - return (new_df) + return(new_df) } #' Infer numerical values to nodes. @@ -49,9 +49,10 @@ create_matrix <- function(df, cols) { #' #' inferred_values <- pass_infos(M = infos, FUN = mean, value = "GC.") pass_infos <- function(M, FUN, value) { - inferred_values <- tapply(M[[value]], M$ancestor, function(x) { + iv <- tapply(M[[value]], M$ancestor, function(x) { x <- x[!is.na(x)] FUN(x)}) + return(iv) } @@ -74,15 +75,15 @@ pass_infos <- function(M, FUN, value) { #' #' inferred_values <- pass_infos_discret(M = infos, value = "Status") pass_infos_discret <- function(M, value) { - bind_values <- M |> + bind_values <- M |> dplyr::select(.data$ancestor, dplyr::all_of(value)) |> stats::na.omit() |> dplyr::group_by(.data[[value]], .data$ancestor) |> - dplyr::count() |> + dplyr::count() |> tidyr::pivot_wider(names_from = dplyr::all_of(value), values_from = .data$n, values_fill = 0) |> as.data.frame() |> - dplyr::rename("taxid" = "ancestor") |> + dplyr::rename("taxid" = "ancestor") |> dplyr::arrange(.data$taxid) - + return(bind_values) } diff --git a/R/display_map.R b/R/display_map.R index 32ca94c..c38c366 100644 --- a/R/display_map.R +++ b/R/display_map.R @@ -19,17 +19,16 @@ #' display_map() display_map <- function(df = NULL, basemap = c("fr", "ncbi", "base", "virus")) { basemap <- match.arg(basemap) - if (basemap == "fr"){ + if (basemap == "fr") { display <- "http://lifemap-fr.univ-lyon1.fr/osm_tiles/{z}/{x}/{y}.png" - } else if (basemap == "ncbi"){ + } else if (basemap == "ncbi") { display <- "http://lifemap-ncbi.univ-lyon1.fr/osm_tiles/{z}/{x}/{y}.png" - } else if (basemap == "base"){ + } else if (basemap == "base") { display <- "http://lifemap.univ-lyon1.fr/osm_tiles/{z}/{x}/{y}.png" - } else if (basemap == "virus"){ + } else if (basemap == "virus") { display <- "https://virusmap.univ-lyon1.fr/osm_tiles/{z}/{x}/{y}.png" } - url2check <- strsplit(display, "osm_tiles")[[1]][1] - + m <- tryCatch({ leaflet::leaflet(df) |> leaflet::addTiles(display, options = leaflet::providerTileOptions(minZoom = 5, maxZoom = 50)) @@ -43,7 +42,7 @@ display_map <- function(df = NULL, basemap = c("fr", "ncbi", "base", "virus")) { return(NA) } ) - + if (!all(is.na(m))) { return(m) } else { diff --git a/R/draw_Lifemap.R b/R/draw_Lifemap.R index 72d196d..8e64eb8 100644 --- a/R/draw_Lifemap.R +++ b/R/draw_Lifemap.R @@ -9,16 +9,16 @@ #' #' @return A vector of values. create_value_range <- function(value, df, df2, min, max) { - if (value %in% colnames(df)) { - old_min <- min(df[[value]], na.rm = TRUE) - old_max <- max(df[[value]], na.rm = TRUE) - old_range <- old_max - old_min - new_range <- max - min - info <- (((df2[[value]] - old_min) * new_range) / old_range) + min - } else { - info <- value - } - return(info) + if (value %in% colnames(df)) { + old_min <- min(df[[value]], na.rm = TRUE) + old_max <- max(df[[value]], na.rm = TRUE) + old_range <- old_max - old_min + new_range <- max - min + info <- (((df2[[value]] - old_min) * new_range) / old_range) + min + } else { + info <- value + } + return(info) } @@ -34,77 +34,82 @@ create_value_range <- function(value, df, df2, min, max) { #' #' @return An updated map with the new layer added. add_lm_markers <- function(proxy, aes, df, df_visible, group_info) { - - if (!(aes$var_fillColor %in% "default")) { - if (is.numeric(df[[aes$var_fillColor]])) { - make_fillColor <- leaflet::colorNumeric(palette = aes$fillPalette, domain = df[[aes$var_fillColor]], reverse = TRUE) - fillColor_info <- make_fillColor(df_visible[[aes$var_fillColor]]) - } else { - make_fillColor <- leaflet::colorFactor(palette = aes$fillPalette, domain = df[[aes$var_fillColor]], reverse = TRUE) - fillColor_info <- make_fillColor(df_visible[[aes$var_fillColor]]) - } - } else { - fillColor_info <- aes$fillColor} - - if (aes$radius %in% "default") { - radius_info <- create_value_range(aes$value, df, df_visible, aes$min, aes$max) - # radius_info <- aes$value + + if (!(aes$var_fillColor %in% "default")) { + if (is.numeric(df[[aes$var_fillColor]])) { + make_fillColor <- leaflet::colorNumeric(palette = aes$fillPalette, domain = df[[aes$var_fillColor]], reverse = TRUE) + fillColor_info <- make_fillColor(df_visible[[aes$var_fillColor]]) } else { - radius_info <- create_value_range(aes$radius, df, df_visible, aes$min, aes$max) + make_fillColor <- leaflet::colorFactor(palette = aes$fillPalette, domain = df[[aes$var_fillColor]], reverse = TRUE) + fillColor_info <- make_fillColor(df_visible[[aes$var_fillColor]]) } - - # stroke presence - if (isTRUE(aes$stroke)) { - if (!(aes$var_color %in% "default")) { - if (is.numeric(df[[aes$var_color]])){ - make_color <- leaflet::colorNumeric(aes$palette, df[[aes$var_color]], reverse = TRUE) - color_info <- make_color(df_visible[[aes$var_color]]) - } else { - make_color <- leaflet::colorFactor(palette = aes$palette, domain = df[[aes$var_color]], reverse = TRUE) - color_info <- make_color(df_visible[[aes$var_color]]) - } - } else { color_info <- aes$color } - } else {color_info <- aes$color} - - # stroke opacity - opacity_info <- create_value_range(aes$opacity, df, df_visible, 0.1, 1) - - # stroke weight - weight_info <- create_value_range(aes$weight, df, df_visible, 1, 10) - - # fill opacity - fillOpacity_info <- create_value_range(aes$fillOpacity, df, df_visible, 0.1, 1) - - ### to improve ### - if (is.null(aes$label)) { - proxy <- leaflet::addCircleMarkers(proxy, - lng = df_visible$lon, - lat = df_visible$lat, - radius = radius_info, - fillColor = fillColor_info, - fillOpacity = fillOpacity_info, - stroke = aes$stroke, - color = color_info, - opacity = opacity_info, - weight = weight_info, - group = group_info - ) - } else if (length(df_visible[[aes$label]]) > 0) { - proxy <- leaflet::addCircleMarkers(proxy, - lng = df_visible$lon, - lat = df_visible$lat, - radius = radius_info, - fillColor = fillColor_info, - fillOpacity = fillOpacity_info, - stroke = aes$stroke, - color = color_info, - opacity = opacity_info, - weight = weight_info, - group = group_info, - label = df_visible[[aes$label]] - ) + } else { + fillColor_info <- aes$fillColor + } + + if (aes$radius %in% "default") { + radius_info <- create_value_range(aes$value, df, df_visible, aes$min, aes$max) + # radius_info <- aes$value + } else { + radius_info <- create_value_range(aes$radius, df, df_visible, aes$min, aes$max) + } + + # stroke presence + if (isTRUE(aes$stroke)) { + if (!(aes$var_color %in% "default")) { + if (is.numeric(df[[aes$var_color]])) { + make_color <- leaflet::colorNumeric(aes$palette, df[[aes$var_color]], reverse = TRUE) + color_info <- make_color(df_visible[[aes$var_color]]) + } else { + make_color <- leaflet::colorFactor(palette = aes$palette, domain = df[[aes$var_color]], reverse = TRUE) + color_info <- make_color(df_visible[[aes$var_color]]) + } + } else { + color_info <- aes$color } - proxy + } else { + color_info <- aes$color + } + + # stroke opacity + opacity_info <- create_value_range(aes$opacity, df, df_visible, 0.1, 1) + + # stroke weight + weight_info <- create_value_range(aes$weight, df, df_visible, 1, 10) + + # fill opacity + fillOpacity_info <- create_value_range(aes$fillOpacity, df, df_visible, 0.1, 1) + + ### to improve ### + if (is.null(aes$label)) { + proxy <- leaflet::addCircleMarkers(proxy, + lng = df_visible$lon, + lat = df_visible$lat, + radius = radius_info, + fillColor = fillColor_info, + fillOpacity = fillOpacity_info, + stroke = aes$stroke, + color = color_info, + opacity = opacity_info, + weight = weight_info, + group = group_info + ) + } else if (length(df_visible[[aes$label]]) > 0) { + proxy <- leaflet::addCircleMarkers(proxy, + lng = df_visible$lon, + lat = df_visible$lat, + radius = radius_info, + fillColor = fillColor_info, + fillOpacity = fillOpacity_info, + stroke = aes$stroke, + color = color_info, + opacity = opacity_info, + weight = weight_info, + group = group_info, + label = df_visible[[aes$label]] + ) + } + proxy } #' Compute the aesthetics for a subtree visualisation. @@ -121,48 +126,52 @@ add_lm_markers <- function(proxy, aes, df, df_visible, group_info) { #' #' @return An updated map with the new layer added. add_lm_branches <- function(proxy, aes, df, df_visible, df_descendants, group_info, all_taxids) { - if (!(aes$var_color %in% "default")) { - make_col <- leaflet::colorNumeric(palette = aes$palette, domain = df[[aes$var_color]], reverse = TRUE) - } - - if (aes$size %in% colnames(df)) { - old_min <- min(df[[aes$size]], na.rm = TRUE) - old_max <- max(df[[aes$size]], na.rm = TRUE) - old_range <- old_max - old_min - new_range <- aes$max - aes$min - } - - if (!(is.null(aes$taxids))) { - descendants_visible <- df_descendants[df_descendants$taxid %in% all_taxids, ] - } else { descendants_visible = df_descendants } + if (!(aes$var_color %in% "default")) { + make_col <- leaflet::colorNumeric(palette = aes$palette, domain = df[[aes$var_color]], reverse = TRUE) + } + + if (aes$size %in% colnames(df)) { + old_min <- min(df[[aes$size]], na.rm = TRUE) + old_max <- max(df[[aes$size]], na.rm = TRUE) + old_range <- old_max - old_min + new_range <- aes$max - aes$min + } + + if (!(is.null(aes$taxids))) { + descendants_visible <- df_descendants[df_descendants$taxid %in% all_taxids, ] + } else { + descendants_visible <- df_descendants + } + + for (id in df_visible$taxid) { + # for each descendant of each taxid + + for (desc in descendants_visible[descendants_visible$ancestor == id, ]$taxid) { + if (!(aes$var_color %in% "default")) { + col_info <- make_col(descendants_visible[descendants_visible$taxid == desc, aes$var_color]) + } else { + col_info <- aes$color + } - for (id in df_visible$taxid) { - # for each descendant of each taxid - - for (desc in descendants_visible[descendants_visible$ancestor == id, ]$taxid) { - if (!(aes$var_color %in% "default")) { - col_info <- make_col(descendants_visible[descendants_visible$taxid == desc, aes$var_color]) - } else { - col_info <- aes$color - } - - if (aes$size %in% colnames(df)) { - size_info <- (((descendants_visible[descendants_visible$taxid == desc, aes$size] - old_min) * new_range) / old_range) + aes$min - } else {size_info <- aes$value} - - proxy <- leaflet::addPolylines(proxy, - lng = c(df_visible[df_visible$taxid == id, "lon"], - descendants_visible[descendants_visible$taxid == desc, "lon"]), - lat = c(df_visible[df_visible$taxid == id, "lat"], - descendants_visible[descendants_visible$taxid == desc, "lat"]), - color = col_info, - opacity = aes$opacity, - fillOpacity = 0.5, - group = group_info, - weight = size_info) - } + if (aes$size %in% colnames(df)) { + size_info <- (((descendants_visible[descendants_visible$taxid == desc, aes$size] - old_min) * new_range) / old_range) + aes$min + } else { + size_info <- aes$value + } + + proxy <- leaflet::addPolylines(proxy, + lng = c(df_visible[df_visible$taxid == id, "lon"], + descendants_visible[descendants_visible$taxid == desc, "lon"]), + lat = c(df_visible[df_visible$taxid == id, "lat"], + descendants_visible[descendants_visible$taxid == desc, "lat"]), + color = col_info, + opacity = aes$opacity, + fillOpacity = 0.5, + group = group_info, + weight = size_info) } - proxy + } + proxy } #' Compute the aesthetics for discret values visualisation. #' @@ -177,26 +186,26 @@ add_lm_branches <- function(proxy, aes, df, df_visible, df_descendants, group_in #' #' @return An updated map with the new layer added. add_lm_piecharts <- function(proxy, aes, df, df_visible, layer) { - values <- unique(df[df$type == "requested", aes$param]) - layerId_info <- sapply(X = 1:nrow(df_visible), FUN = function(x){paste(layer,x,collapse="", sep = "")}) - make_col <- leaflet::colorFactor(aes$pal, values) - proxy <- proxy |> - leaflet.minicharts::addMinicharts( - lng = df_visible$lon, - lat = df_visible$lat, - chartdata = df_visible[, values], - type = aes$type, - colorPalette = make_col(values), - width = aes$width, - height = aes$height, - opacity = aes$opacity, - showLabels = aes$showLabels, - transitionTime = 0, - legend = aes$legend, - legendPosition = aes$legendPosition, - layerId = layerId_info - ) - proxy + values <- unique(df[df$type == "requested", aes$param]) + layerId_info <- sapply(X = seq_len(nrow(df_visible)), FUN = function(x) {paste(layer, x, collapse = "", sep = "")}) + make_col <- leaflet::colorFactor(aes$pal, values) + proxy <- proxy |> + leaflet.minicharts::addMinicharts( + lng = df_visible$lon, + lat = df_visible$lat, + chartdata = df_visible[, values], + type = aes$type, + colorPalette = make_col(values), + width = aes$width, + height = aes$height, + opacity = aes$opacity, + showLabels = aes$showLabels, + transitionTime = 0, + legend = aes$legend, + legendPosition = aes$legendPosition, + layerId = layerId_info + ) + proxy } @@ -211,36 +220,36 @@ add_lm_piecharts <- function(proxy, aes, df, df_visible, layer) { #' #' @return An updated map. display_option <- function(m, aes, df, type, leaves, i) { - - if (aes$display == "requested") { - df_visible <- df[df$type == "requested",] - } else if (aes$display == "all") { - df_visible <- df - } else if (aes$display == "leaves") { - df_visible <- df[df$taxid %in% leaves,] - } - ancestors <- unique(unlist(df[df$taxid %in% aes$taxids[[1]], "ascend"])) - all_taxids <- c(df[df$taxid %in% aes$taxids[[1]],"taxid"], ancestors) - if (!(is.null(aes$taxids))) { - df_visible = df_visible[df_visible$taxid %in% all_taxids, ] - } - if (nrow(df_visible) < 5000) { - - if (type == "markers") { - m <- m |> - add_lm_markers(aes = aes, df = df, - df_visible = df_visible, - group_info = as.character(i)) - } else if (type == "discret") { - m <- m |> - add_lm_piecharts(aes = aes, df = df, - df_visible = df_visible, - layer = as.character(i)) - } - } else { - stop("you are trying to draw to many points at a time, maybe you shoud try another options") + + if (aes$display == "requested") { + df_visible <- df[df$type == "requested", ] + } else if (aes$display == "all") { + df_visible <- df + } else if (aes$display == "leaves") { + df_visible <- df[df$taxid %in% leaves, ] + } + ancestors <- unique(unlist(df[df$taxid %in% aes$taxids[[1]], "ascend"])) + all_taxids <- c(df[df$taxid %in% aes$taxids[[1]], "taxid"], ancestors) + if (!(is.null(aes$taxids))) { + df_visible <- df_visible[df_visible$taxid %in% all_taxids, ] + } + if (nrow(df_visible) < 5000) { + + if (type == "markers") { + m <- m |> + add_lm_markers(aes = aes, df = df, + df_visible = df_visible, + group_info = as.character(i)) + } else if (type == "discret") { + m <- m |> + add_lm_piecharts(aes = aes, df = df, + df_visible = df_visible, + layer = as.character(i)) } - return(m) + } else { + stop("you are trying to draw to many points at a time, maybe you shoud try another options") + } + return(m) } @@ -267,71 +276,71 @@ display_option <- function(m, aes, df, type, leaves, i) { #' lifemap(LM_eukaryotes) + lm_markers() + lm_branches() #' } draw_Lifemap <- function(lm_obj) { - - df <- lm_obj$df - basemap <- lm_obj$basemap - aes <- lm_obj$aes - zoom_level <- lm_obj$options$zoom - - all_ancestors <- unique(unlist(df$ascend)) - leaves <- df[!(df$taxid %in% all_ancestors), "taxid"] - - variables <- c() - for (i in 1:length(aes)) { - for (param in aes[[i]]){ - if ((is.character(param)) && param %in% colnames(df)){ - variables <- append(variables, param) - } - } - } - variables <- unique(variables) - - if (length(variables) > 0) { - M <- create_matrix(df, variables) + + df <- lm_obj$df + basemap <- lm_obj$basemap + aes <- lm_obj$aes + zoom_level <- lm_obj$options$zoom + + all_ancestors <- unique(unlist(df$ascend)) + leaves <- df[!(df$taxid %in% all_ancestors), "taxid"] + + variables <- c() + for (i in seq_along(aes)) { + for (param in aes[[i]]){ + if ((is.character(param)) && param %in% colnames(df)) { + variables <- append(variables, param) + } } - - cat("passing the information to the nodes \n") - #pass the information to the nodes or not - for (i in 1:length(aes)) { - # passing information if the function is given - if (is.lm_markers(aes[[i]]) && !(is.null(aes[[i]]$FUN))) { - for (parameter in aes[[i]]) { - - if (!(is.null(parameter)) && is.character(parameter) && parameter %in% colnames(df)) { - new_df <- pass_infos(M = M, - FUN = aes[[i]]$FUN, - value = parameter) - for (id in names(new_df)) { - if (is.na(df[df$taxid == id, parameter])) { - df[df$taxid == id, parameter] <- new_df[id] - } - } - } + } + variables <- unique(variables) + + if (length(variables) > 0) { + M <- create_matrix(df, variables) + } + + cat("passing the information to the nodes \n") + #pass the information to the nodes or not + for (i in seq_along(aes)) { + # passing information if the function is given + if (is.lm_markers(aes[[i]]) && !(is.null(aes[[i]]$FUN))) { + for (parameter in aes[[i]]) { + + if (!(is.null(parameter)) && is.character(parameter) && parameter %in% colnames(df)) { + new_df <- pass_infos(M = M, + FUN = aes[[i]]$FUN, + value = parameter) + for (id in names(new_df)) { + if (is.na(df[df$taxid == id, parameter])) { + df[df$taxid == id, parameter] <- new_df[id] } - } else if (is.lm_branches(aes[[i]]) && !(is.null(aes[[i]]$FUN))) { - for (parameter in aes[[i]]) { - if (!(is.null(parameter)) && is.character(parameter) && parameter %in% colnames(df)) { - new_df <- pass_infos(M = M, - FUN = aes[[i]]$FUN, - value = parameter) - for (id in names(new_df)) { - if (is.na(df[df$taxid == id, parameter])) { - df[df$taxid == id, parameter] <- new_df[id] - } - } - } + } + } + } + } else if (is.lm_branches(aes[[i]]) && !(is.null(aes[[i]]$FUN))) { + for (parameter in aes[[i]]) { + if (!(is.null(parameter)) && is.character(parameter) && parameter %in% colnames(df)) { + new_df <- pass_infos(M = M, + FUN = aes[[i]]$FUN, + value = parameter) + for (id in names(new_df)) { + if (is.na(df[df$taxid == id, parameter])) { + df[df$taxid == id, parameter] <- new_df[id] } - } else if (is.lm_piecharts(aes[[i]])) { - new_df <- pass_infos_discret(M = M, - value = aes[[i]]$param) - df <- merge(df, new_df, by.x = "taxid", by.y = "taxid") + } } + } + } else if (is.lm_piecharts(aes[[i]])) { + new_df <- pass_infos_discret(M = M, + value = aes[[i]]$param) + df <- merge(df, new_df, by.x = "taxid", by.y = "taxid") } - - ui <- shiny::fluidPage( - htmltools::tags$head( - htmltools::tags$style( - htmltools::HTML(" + } + + ui <- shiny::fluidPage( + htmltools::tags$head( + htmltools::tags$style( + htmltools::HTML(" .leaflet-container { background: #000000; outline: 0; @@ -345,200 +354,200 @@ draw_Lifemap <- function(lm_obj) { position:fixed !important; } ") - )), - - # htmltools::tags$style(type = "text/css", "#mymap {height: calc(100vh) !important; }"), - leaflet::leafletOutput("mymap"), - htmltools::p() + )), + + # htmltools::tags$style(type = "text/css", "#mymap {height: calc(100vh) !important; }"), + leaflet::leafletOutput("mymap"), + htmltools::p() + ) + + server <- function(input, output, session) { + + # define the zone visible by the users + df_zoom_bounds <- shiny::reactive( + df[df$zoom <= (input$mymap_zoom + zoom_level) & + df$lat > input$mymap_bounds$south & + df$lat < input$mymap_bounds$north & + df$lon > input$mymap_bounds$west & + df$lon < input$mymap_bounds$east, ] ) + + # define the descendants of df_zoom_bounds' taxids + df_descendants <- shiny::reactive({ + visibles <- df_zoom_bounds()$taxid + df[df$ancestor %in% visibles, ] + }) + + addLegendCustom <- function(map, colors, labels, sizes, shapes, borders, opacity = 0.5, title, position) { - server <- function(input, output, session) { - - # define the zone visible by the users - df_zoom_bounds <- shiny::reactive( - df[df$zoom <= (input$mymap_zoom + zoom_level) & - df$lat > input$mymap_bounds$south & - df$lat < input$mymap_bounds$north & - df$lon > input$mymap_bounds$west & - df$lon < input$mymap_bounds$east,] - ) + make_shapes <- function(colors, sizes, borders, shapes) { + shapes <- gsub("circle", "50%", shapes) + shapes <- gsub("square", "0%", shapes) + paste0(colors, "; width:", sizes, "px; height:", sizes, "px; border:3px solid ", borders, "; border-radius:", shapes) + } + make_labels <- function(sizes, labels) { + paste0("