Skip to content

Commit

Permalink
Merge pull request #34 from dtacled/tb
Browse files Browse the repository at this point in the history
Update example creds and more inter_num updates
  • Loading branch information
dtacled authored Dec 18, 2024
2 parents e0fd6ce + dbdc8a3 commit 3e52902
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 115 deletions.
117 changes: 85 additions & 32 deletions R/acled_transform_wider.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))
Expand All @@ -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))) %>%
Expand All @@ -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)))))
Expand All @@ -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))) %>%
Expand All @@ -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)))))
Expand Down Expand Up @@ -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)))))
Expand All @@ -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) %>%
Expand Down
2 changes: 1 addition & 1 deletion R/acled_update.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

Expand Down
10 changes: 2 additions & 8 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<!-- badges: end -->

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
Expand Down
74 changes: 13 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<!-- badges: end -->

# 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.
```
2 changes: 1 addition & 1 deletion tests/testthat/setup-acled_access.R
Original file line number Diff line number Diff line change
@@ -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"))
3 changes: 1 addition & 2 deletions tests/testthat/test-acled_access.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"),"[email protected]")
expect_equal(Sys.getenv("acled_email"),"[email protected]")
expect_equal(Sys.getenv("acled_email"),"[email protected]")
expect_equal(nchar(Sys.getenv("acled_key")), 20)
})

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-acled_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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. ----
Expand Down Expand Up @@ -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]))
})


Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-acled_deletions_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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. ----
Expand All @@ -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 ----
Expand Down Expand Up @@ -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]))
})


Expand Down
Loading

0 comments on commit 3e52902

Please sign in to comment.