diff --git a/R/acled_transform_wider.R b/R/acled_transform_wider.R index a0c2484..2afb473 100644 --- a/R/acled_transform_wider.R +++ b/R/acled_transform_wider.R @@ -39,6 +39,10 @@ acled_transform_wider <- function(data, type = "full_actors") { stop(paste0("Error: ", type, " is not a valid option. Please select a valid option:\"full_actors\", \"main_actors\", \"assoc_actors\", \"source\", \"api_monadic\"")) } + if(type %in% c("full_actors", "main_actors")) { + inter_numeric <- any(1:8 %in% unique(data$inter)) + } + if (type == "full_actors") { columns_present <- function(df, cols) { all(sapply(cols, function(x) !is.na(match(x, names(df))))) @@ -51,16 +55,18 @@ acled_transform_wider <- function(data, type = "full_actors") { stop("Some columns are missing. Please make sure your data frame includes: actor,type_of_actor,inter_type, and inter.") } + + reverse_data <- data %>% # Pivot actor firsts, flattening joint actors such as assoc actors pivot_wider(names_from = type_of_actor, values_from = actor, values_fn = function(x) str_flatten(x, collapse = "; "), values_fill = "") %>% - # Pivot inters next, adding a fill 9999 to those that do not apply, as a way of removing. inters from different types of actors - pivot_wider(names_from = inter_type, values_from = inter, values_fill = 9999) %>% - # Transform inter into character for collapsing + mutate( - inter1 = as.character(inter1), - inter2 = as.character(inter2) + inter = as.character(inter) ) %>% + + # Pivot inters next, adding a fill 9999 to those that do not apply, as a way of removing. inters from different types of actors + pivot_wider(names_from = inter_type, values_from = inter, values_fill = as.character(9999)) %>% mutate(inter1 = replace_na(inter1, "")) %>% mutate(inter2 = replace_na(inter2, "")) %>% group_by(across(c(-actor1, -actor2, -inter1, -inter2, -assoc_actor_1, -assoc_actor_2))) %>% @@ -74,21 +80,34 @@ acled_transform_wider <- function(data, type = "full_actors") { assoc_actor_2 = str_c(assoc_actor_2, collapse = "") ) %>% ungroup() %>% - # Transform inter into numeric column - mutate( - inter1 = as.numeric(inter1), - inter2 = as.numeric(inter2) - ) %>% mutate( actor2 = na_if(actor2, ""), actor1 = na_if(actor1, ""), assoc_actor_1 = na_if(assoc_actor_1, ""), - assoc_actor_2 = na_if(assoc_actor_2, ""), - inter1 = replace_na(inter1, 0), - inter2 = replace_na(inter2, 0) + assoc_actor_2 = na_if(assoc_actor_2, "") ) %>% # Match column structure for an acled dataset - select(names(acledR::acled_old_dummy)) + dplyr::select(names(acledR::acled_old_dummy)) + + # Coerce to numeric if inter were originally numeric + if(inter_numeric == TRUE) { + reverse_data <- + reverse_data %>% + mutate( + inter1 = as.numeric(inter1), + inter2 = as.numeric(inter2), + inter1 = replace_na(inter1, 0), + inter2 = replace_na(inter2, 0) + ) + } else { + reverse_data <- + reverse_data %>% + mutate( + inter1 = case_when(inter1 == "" ~ NA_character_, TRUE ~ inter1), + inter2 = case_when(inter2 == "" ~ NA_character_, TRUE ~ inter2) + ) + } + } else if (type == "main_actors") { columns_present <- function(df, cols) { all(sapply(cols, function(x) !is.na(match(x, names(df))))) @@ -104,13 +123,16 @@ acled_transform_wider <- function(data, type = "full_actors") { reverse_data <- data %>% # Pivot actor firsts, flattening joint actors such as assoc actors pivot_wider(names_from = type_of_actor, values_from = actor, values_fn = function(x) str_flatten(x, collapse = "; "), values_fill = "") %>% - # Pivot inters next, adding a fill 9999 to those that do not apply, as a way of removing. inters from different types of actors - pivot_wider(names_from = inter_type, values_from = inter, values_fill = 9999) %>% + # Transform inter into character for collapsing mutate( - inter1 = as.character(inter1), - inter2 = as.character(inter2) + inter = as.character(inter) ) %>% + + # Pivot inters next, adding a fill 9999 to those that do not apply, as a way of removing. inters from different types of actors + # Coerced to character to account for inters being text or numeric + pivot_wider(names_from = inter_type, values_from = inter, values_fill = as.character(9999)) %>% + mutate(inter1 = replace_na(inter1, "")) %>% mutate(inter2 = replace_na(inter2, "")) %>% group_by(across(c(-actor1, -actor2, -inter1, -inter2))) %>% @@ -122,21 +144,34 @@ acled_transform_wider <- function(data, type = "full_actors") { inter2 = str_trim(str_remove_all(str_c(inter2, collapse = " "), "9999")) ) %>% ungroup() %>% - # Transform inter into numeric column - mutate( - inter1 = as.numeric(inter1), - inter2 = as.numeric(inter2) - ) %>% mutate( actor2 = na_if(actor2, ""), actor1 = na_if(actor1, ""), assoc_actor_1 = na_if(assoc_actor_1, ""), - assoc_actor_2 = na_if(assoc_actor_2, ""), - inter1 = replace_na(inter1, 0), - inter2 = replace_na(inter2, 0) + assoc_actor_2 = na_if(assoc_actor_2, "") ) %>% # Match column structure for an acled dataset - select(names(acledR::acled_old_dummy)) + dplyr::select(names(acledR::acled_old_dummy)) + + # Coerce to numeric if inter were originally numeric + if(inter_numeric == TRUE) { + reverse_data <- + reverse_data %>% + mutate( + inter1 = as.numeric(inter1), + inter2 = as.numeric(inter2), + inter1 = replace_na(inter1, 0), + inter2 = replace_na(inter2, 0) + ) + } else { + reverse_data <- + reverse_data %>% + mutate( + inter1 = case_when(inter1 == "" ~ NA_character_, TRUE ~ inter1), + inter2 = case_when(inter2 == "" ~ NA_character_, TRUE ~ inter2) + ) + } + } else if (type == "assoc_actors") { columns_present <- function(df, cols) { all(sapply(cols, function(x) !is.na(match(x, names(df))))) @@ -164,12 +199,30 @@ acled_transform_wider <- function(data, type = "full_actors") { actor2 = na_if(actor2, ""), actor1 = na_if(actor1, ""), assoc_actor_1 = na_if(assoc_actor_1, ""), - assoc_actor_2 = na_if(assoc_actor_2, ""), - inter1 = replace_na(inter1, 0), - inter2 = replace_na(inter2, 0) + assoc_actor_2 = na_if(assoc_actor_2, "") ) %>% # Match column structure for an acled dataset - select(names(acledR::acled_old_dummy)) + dplyr::select(names(acledR::acled_old_dummy)) + + # Coerce to numeric if inter were originally numeric + # if(inter_numeric == TRUE) { + # reverse_data <- + # reverse_data %>% + # mutate( + # inter1 = as.numeric(inter1), + # inter2 = as.numeric(inter2), + # inter1 = replace_na(inter1, 0), + # inter2 = replace_na(inter2, 0) + # ) + # } else { + # reverse_data <- + # reverse_data %>% + # mutate( + # inter1 = case_when(inter1 == "" ~ NA_character_, TRUE ~ inter1), + # inter2 = case_when(inter2 == "" ~ NA_character_, TRUE ~ inter2) + # ) + # } + } else if (type == "source") { columns_present <- function(df, cols) { all(sapply(cols, function(x) !is.na(match(x, names(df))))) @@ -188,7 +241,7 @@ acled_transform_wider <- function(data, type = "full_actors") { summarise(source = str_c(source, collapse = "; ")) %>% ungroup() %>% # Match column structure for an acled dataset - select(names(acledR::acled_old_dummy)) + dplyr::select(names(acledR::acled_old_dummy)) } else if (type == "api_monadic") { df1 <- data %>% group_by(event_id_cnty) %>% diff --git a/R/acled_update.R b/R/acled_update.R index 2b2d454..3f98784 100644 --- a/R/acled_update.R +++ b/R/acled_update.R @@ -77,7 +77,7 @@ acled_update <- function(df, warning("Warning: End date is earlier than the latest event date in your dataframe.") } - if (inter_numeric == FALSE & length(df$inter1[[1]]) == 1) { + if (inter_numeric == FALSE & any(1:8 %in% unique(df$inter1))) { stop("The data frame provided appears to have numeric interaction values (inter1, inter2, and interaction variables). Set inter_numeric = TRUE in the acled_update() call to update data with numeric interaction values.") } diff --git a/README.Rmd b/README.Rmd index e7501df..7da9b77 100755 --- a/README.Rmd +++ b/README.Rmd @@ -20,17 +20,11 @@ knitr::opts_chunk$set( [![codecov](https://codecov.io/gh/ACLED/acledR/graph/badge.svg?token=TDJodXhEvx)](https://codecov.io/gh/ACLED/acledR) -Welcome to ACLED's official R package! This package simplifies access to the data via ACLED's API. It also provides convenient functions for making common ACLED data transformations. See dtacled.github.io/acledR for more information and tutorials on how to use the package. -# Overview +Welcome to acledR! This package provides tools to extract and manipulate data from the [Armed Conflict Location and Event Data Project (ACLED)](https://acleddata.com/). It also provides convenient functions for making common ACLED data transformations. See [dtacled.github.io/acledR](https://dtacled.github.io/acledR) for more information and tutorials on how to use the package. -This package provides tools to extract and manipulate data from the [Armed Conflict Location and Event Data Project (ACLED)](https://acleddata.com/). - -To access ACLED data, please register an account at [developer.acleddata.com](developer.acleddata.com). - -* You can visit our [guide](https://acleddata.com/acleddatanew//wp-content/uploads/2021/11/ACLED_Access-Guide_October-2020.pdf) on how to easily set up your ACLED account. -* We recommend you to save your ACLED API Key in an R object so you can easily re-use your key. +To access ACLED data, please register an account at [developer.acleddata.com](https://developer.acleddata.com). You can find detailed instructions for setting up your account and retreiving an API key [here](https://acleddata.com/acleddatanew//wp-content/uploads/2021/11/ACLED_Access-Guide_October-2020.pdf). ## Installation diff --git a/README.md b/README.md index f71f5e3..d6ca29b 100755 --- a/README.md +++ b/README.md @@ -7,74 +7,26 @@ [![R-CMD-check](https://github.com/ACLED/acledR/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ACLED/acledR/actions/workflows/R-CMD-check.yaml) [![codecov](https://codecov.io/gh/ACLED/acledR/graph/badge.svg?token=TDJodXhEvx)](https://codecov.io/gh/ACLED/acledR) - -# Overview - -This package provides tools to extract and manipulate data from the -[Armed Conflict Location and Event Data Project -(ACLED)](https://acleddata.com/). +Welcome to acledR! This package provides tools to extract and manipulate +data from the [Armed Conflict Location and Event Data Project +(ACLED)](https://acleddata.com/). It also provides convenient functions +for making common ACLED data transformations. See +[dtacled.github.io/acledR](https://dtacled.github.io/acledR) for more +information and tutorials on how to use the package. To access ACLED data, please register an account at -[developer.acleddata.com](developer.acleddata.com). - -- You can visit our - [guide](https://acleddata.com/acleddatanew//wp-content/uploads/2021/11/ACLED_Access-Guide_October-2020.pdf) - on how to easily set up your ACLED account. -- We recommend you to save your ACLED API Key in an R object so you can - easily re-use your key. - -## Installation (for private repo) - -Since the package repo is currently private, you need to tell R and -Github that you’re a collaborator. To do so, you first create a Github -personal access token (PAT). You can set this to expire after a certain -time (the default) or be permanent. We can initiate this process -internally via: - -``` r -# install.packages("usethis") if not installed already -# create personal access token - this should redirect to your github page where you can copy the token -usethis::create_github_token() -``` - -After you’ve copied the PAT from the browser, return to R and run this, -which will store the PAT locally. - -``` r -# paste the token where it says YourPAT -credentials::set_github_pat("YourPAT") -# if you run this, it should print your token; if not we've failed -Sys.getenv("GITHUB_PAT") -``` - -I recommend also setting the PAT in your `.Rprofile` so it’s stored for -all R sessions (i.e., you don’t have to save the PAT and paste it in -each time you re-install). +[developer.acleddata.com](https://developer.acleddata.com). You can find +detailed instructions for setting up your account and retreiving an API +key +[here](https://acleddata.com/acleddatanew//wp-content/uploads/2021/11/ACLED_Access-Guide_October-2020.pdf). -``` r -# to set your PAT for all R sessions, run -file.edit(file.path("~", ".Rprofile")) -# and then paste credentials::set_github_pat("YourPAT") into the .Rprofile script -# save the file -``` +## Installation -Now you can install the package and it will automatically locate your -PAT. +The package will be reviewed and available on CRAN shortly. In the +meantime, you can install the package from Github: ``` r -# install from github devtools::install_github("ACLED/acledR") ``` - -## Installation (for public use) - -Until the acledR package gets added into the Comprehensive R Archive -Network (CRAN), users can utilize devtools to install the package from -Github. Thankfully, the installation is rather simple. You can install -it through the following code: - -``` r -devtools::install_github("ACLED/acledR") ## if you are interested in a particular branch, please add a 'ref' argument. -``` diff --git a/tests/testthat/setup-acled_access.R b/tests/testthat/setup-acled_access.R index b492d43..f89cda3 100644 --- a/tests/testthat/setup-acled_access.R +++ b/tests/testthat/setup-acled_access.R @@ -1,5 +1,5 @@ # Setup for test acled_access -# Run the function to set up the enviornment credentials +# Run the function to set up the environment credentials acled_access(email = Sys.getenv("EMAIL_ADDRESS_EXAMPLES"), key = Sys.getenv("EXAMPLES_KEY")) diff --git a/tests/testthat/test-acled_access.R b/tests/testthat/test-acled_access.R index 8ee6a78..eeb081a 100644 --- a/tests/testthat/test-acled_access.R +++ b/tests/testthat/test-acled_access.R @@ -2,8 +2,7 @@ # Does it save the credentials in the enviornment? - Missing test_that("acled_access properly stores the credentials", { - # expect_equal(Sys.getenv("acled_email"),"acledexamples@gmail.com") - expect_equal(Sys.getenv("acled_email"),"t.billing@acleddata.com") + expect_equal(Sys.getenv("acled_email"),"acledrexamples@gmail.com") expect_equal(nchar(Sys.getenv("acled_key")), 20) }) diff --git a/tests/testthat/test-acled_api.R b/tests/testthat/test-acled_api.R index d97f2c3..87c5e91 100644 --- a/tests/testthat/test-acled_api.R +++ b/tests/testthat/test-acled_api.R @@ -178,7 +178,7 @@ test_that("Error when one of two countries are wrong",{ ## Test what happens when someone inputs acled_access as TRUE but it includes email and key. ---- test_that("Acled_access is ignored",{ - expect_true(grepl("t.billing", log_received_data_check_credential$email[1])) + expect_true(grepl("acledrexamples", log_received_data_check_credential$email[1])) }) # Test errors from incorrectly input arguments. ---- @@ -305,7 +305,7 @@ test_that("A warning appears that acled_access is being ignored, and the proper expect_message(acled_api(email = Sys.getenv("EMAIL_ADDRESS_EXAMPLES"), key = Sys.getenv("EXAMPLES_KEY"), acled_access = T, log = T, inter_numeric = TRUE), regexp = "acled_access is TRUE, but email and key are included in the function. Ignoring acled_access.") - expect_true(grepl("t.billing", alog$email[1])) + expect_true(grepl("acledrexamples", alog$email[1])) }) diff --git a/tests/testthat/test-acled_deletions_api.R b/tests/testthat/test-acled_deletions_api.R index 0262e95..5e52214 100644 --- a/tests/testthat/test-acled_deletions_api.R +++ b/tests/testthat/test-acled_deletions_api.R @@ -22,7 +22,7 @@ test_that("names of columns are correct - unix", { ## Test that email and key are handled appropiately ---- test_that("Email and key are handled as expected without acled_access",{ - expect_true(grepl("t.billing", received_deleted_log$email[1]))} + expect_true(grepl("acledrexamples", received_deleted_log$email[1]))} ) ## Test that acled_access is handled appropiately. ---- @@ -32,7 +32,7 @@ test_that("Email and Key are handled appropiately",{ some_log <- acled_deletions_api(date_deleted = "1658707200", acled_access = T, log = T) - expect_true(grepl("t.billing", some_log$email[1]))} + expect_true(grepl("acledrexamples", some_log$email[1]))} ) ## Date and unix return the same output ---- @@ -90,7 +90,7 @@ test_that("A warning appears that acled_access is being ignored, and the proper expect_message(acled_deletions_api(email = Sys.getenv("EMAIL_ADDRESS_EXAMPLES"), key = Sys.getenv("EXAMPLES_KEY"), date_deleted = "1658707200",acled_access = T, log = T), regexp = "acled_access is TRUE, but email and key are included in the function. Ignoring acled_access.") - expect_true(grepl("t.billing@acleddata.com", alog$email[1])) + expect_true(grepl("acledrexamples@gmail.com", alog$email[1])) }) diff --git a/tests/testthat/test-acled_transform_wider.R b/tests/testthat/test-acled_transform_wider.R index 2767d9b..04000dd 100644 --- a/tests/testthat/test-acled_transform_wider.R +++ b/tests/testthat/test-acled_transform_wider.R @@ -12,7 +12,8 @@ test_that("acled_transform_wider returns expected results for type = 'full_actor mutate(actor2 = na_if(actor2, "")) # Test if the original acledR::acled_old_dummy and the reversed acledR::acled_old_dummy are the same - expect_equal(dplyr::arrange(acledR::acled_old_dummy,event_id_cnty), dplyr::arrange(reversed_data, event_id_cnty)) + expect_equal(dplyr::arrange(acledR::acled_old_dummy,event_id_cnty), dplyr::arrange(reversed_data, event_id_cnty), + ignore_attr = TRUE) }) test_that("acled_transform_wider returns expected results for type = 'main_actors'", { @@ -26,7 +27,8 @@ test_that("acled_transform_wider returns expected results for type = 'main_actor reversed_data <- acled_transform_wider(transformed_data, type = "main_actors") # Test if the original acledR::acled_old_dummy and the reversed acledR::acled_old_dummy are the same - expect_equal(dplyr::arrange(acledR::acled_old_dummy,event_id_cnty), dplyr::arrange(reversed_data, event_id_cnty)) + expect_equal(dplyr::arrange(acledR::acled_old_dummy,event_id_cnty), dplyr::arrange(reversed_data, event_id_cnty), + ignore_attr = TRUE) }) test_that("acled_transform_wider returns expected results for type = 'assoc_actors'", { @@ -38,7 +40,8 @@ test_that("acled_transform_wider returns expected results for type = 'assoc_acto reversed_data <- acled_transform_wider(transformed_data, type = "assoc_actors") # Test if the original acledR::acled_old_dummy and the reversed acledR::acled_old_dummy are the same - expect_equal(dplyr::arrange(acledR::acled_old_dummy,event_id_cnty), dplyr::arrange(reversed_data, event_id_cnty)) + expect_equal(dplyr::arrange(acledR::acled_old_dummy,event_id_cnty), dplyr::arrange(reversed_data, event_id_cnty), + ignore_attr = TRUE) }) test_that("acled_transform_wider returns expected results for type = 'source'", { @@ -50,7 +53,8 @@ test_that("acled_transform_wider returns expected results for type = 'source'", reversed_data <- acled_transform_wider(transformed_data, type = "source") # Test if the original acledR::acled_old_dummy and the reversed acledR::acled_old_dummy are the same - expect_equal(dplyr::arrange(acledR::acled_old_dummy,event_id_cnty), dplyr::arrange(reversed_data, event_id_cnty)) + expect_equal(dplyr::arrange(acledR::acled_old_dummy,event_id_cnty), dplyr::arrange(reversed_data, event_id_cnty), + ignore_attr = TRUE) }) # Tests for proper errors and messages---- diff --git a/vignettes/get_started.Rmd b/vignettes/get_started.Rmd index 2fe0cdf..f744f25 100644 --- a/vignettes/get_started.Rmd +++ b/vignettes/get_started.Rmd @@ -1,5 +1,5 @@ --- -title: "Getting Started" +title: "Get Started" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Get Started}