Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding map_data R function #3

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(map_data)
export(individuals_by)
export(initiative_by)
export(replace_other)
Expand Down
1 change: 1 addition & 0 deletions R/Functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ individuals_by <- function(individual_data = individuals, ind_id = nom, group =
clickAction = click_action)
}
}

replace_other <- function(data = individuals, group = NULL, group_other = paste0("autre_", group), group_other_name = NULL, RAS = FALSE){
group_var <- group
# if (group_other == "autre_pays"){
Expand Down
1 change: 0 additions & 1 deletion R/initiative_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#' @param click_action character string with a JavaScript expression to evaluate when a node is clicked.
#'
#' @return Returns a network graph object
#' @export
#'
#' @examples # todo
initiative_by <- function(initiative_data, by = "pays", sep = ".", filter_var = NULL, filter_vals = NULL,
Expand Down
62 changes: 62 additions & 0 deletions R/map_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#' Map Data for Network Visualisation
#'
#' This function takes a data frame and generates a network visualization using the \code{forceNetwork} function
#' from the \code{networkD3} package. It separates and expands the data based on the specified values and node values,
#' and creates links between them.
#'
#' @param data The original data frame.
#' @param values The column name representing the primary values to be mapped.
#' @param node_values A character vector specifying the column names representing the node values.
#'
#' @return A network visualisation generated using the \code{forceNetwork} function.
#' @export
#'
#' @examples
#' # An example with simple data
#' df_original <- data.frame(initiative = c("A", "B", "C", "D"),
#' country = c("BF", "BF Niger", "Niger", "Niger"))
#' map_data(df_original, "initiative", "country")
#'
#'
#' df_original2 <- data.frame(individuals = c("A", "B", "C", "D"),
#' institutions = c("a b", "a", "a", "b c"),
#' initiatives = c("z", "z", "y z", "y"))
#' map_data(df_original2, "individuals", c("institutions", "initiatives"))
#'
map_data <- function(data, values, node_values) {
# repeat for each in node_values because of different sizes in cols
# separate_longer_delim doesn't repeat for each col, but groups
for (i in node_values){
data <- data %>%
separate_longer_delim(cols = {{ i }}, " ")
}

# Step 1: Expand data for the node data
df_expanded <- data %>%
tidyr::pivot_longer(cols = c({{ values }}, {{ node_values }}),
names_to = "group",
values_to = "id") %>%
dplyr::arrange(group) %>%
dplyr::distinct() %>%
dplyr::mutate(id_index = row_number() - 1)

# Step 2: Create the linked data
df_mapped <- data %>%
dplyr::left_join(df_expanded, by = setNames("id", values)) %>%
dplyr::rename(source = id_index)

df_mapped <- df_mapped %>%
tidyr::pivot_longer(cols = all_of({{ node_values }}), values_to = "id") %>%
dplyr::distinct() %>%
dplyr::left_join(df_expanded, by = "id") %>%
dplyr::rename(target = id_index)

df_mapped <- df_mapped %>%
dplyr::select(c(target, source))

networkD3::forceNetwork(Links = df_mapped, Nodes = df_expanded,
Source = "source", Target = "target",
NodeID = "id",
Group = "group",
legend = TRUE)
}
31 changes: 0 additions & 31 deletions man/initiative_by.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions man/map_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading