Skip to content

Instantly share code, notes, and snippets.

View dickoa's full-sized avatar

Ahmadou Dicko dickoa

View GitHub Profile
@dickoa
dickoa / up_brewer.R
Last active August 27, 2023 16:31
Brewer method unequal probabilities speed up
up_brewer <- function(pik, eps = 1e-06) {
if (any(is.na(pik)))
stop("there are missing values in the pik vector")
list <- pik > eps & pik < 1 - eps
pikb <- pik[list]
s <- pik
N <- as.integer(length(pikb))
sb <- vector(mode = "double", length = N)
r <- .C("C_up_brewer", pikb, N, result = sb)
s[list] <- r$result
@dickoa
dickoa / pbs_moe_chart.R
Created February 24, 2023 23:06
Reproduce (kind of) PBS poll charts with MoE
library(tidyverse)
library(ggpattern)
library(ggtext)
## https://www.pbs.org/newshour/politics/most-americans-want-congress-to-compromise-as-debt-ceiling-looms
## Great chart by Megan McGrew
col <- c("#2aa7ff", "#f93037", "#a44bd2", "#c0c1c0")
bg_col <- "#ecedec"
@dickoa
dickoa / ridl_regional_dataset_list.R
Created February 10, 2023 15:52
List of dataset per region in RIDL including year of creation of each dataset
library(tidyverse)
library(ridl)
library(lubridate)
### Targeted region
region <- "west-and-central-africa"
### List all containers
ct_nm <- rc_list()
@dickoa
dickoa / onedriveR.R
Created April 6, 2022 09:56
Download and read Excel sheets from R
library(Microsoft365R)
library(tidyverse)
library(readxl)
odb <- get_business_onedrive(app = "d44a05d5-c6a5-4bbb-82d2-443123722380")
### Check files
odb$list_items()
### Check files in "Documents"
@dickoa
dickoa / get_ridl_metadata_wca.R
Created February 21, 2022 19:02
Get the list of RIDL Datasets for West and Central Africa
library(tidyverse)
library(janitor)
library(ridl) ## remotes::install_gitlab("dickoa/ridl")
library(popdata) ## remotes::install_gitlab("dickoa/popdata")
### Get the list of countries name, iso3 and unhcr code
ct <- pd_countries
ct <- filter(pd_countries,
bureau == "West and Central Africa") |>
@dickoa
dickoa / mali_violence_acled.R
Last active February 20, 2022 19:06
Mali violence map ACLED
library(tidyverse)
library(sf)
library(rgeoboundaries) ## remotes::install_github("wmgeolabs/rgeoboundaries")
library(racled) ## remotes::install_gitlab("dickoa/racled")
library(cowplot)
### Get Mali country boundaries
mli_adm0 <- gb_adm0("mali")
### Get Mali ACLED data
@dickoa
dickoa / read_kobo_data.R
Created February 18, 2022 09:28
Read KoBoToolbox data
library(robotoolbox) # if not available please do remotes::install_gitlab("dickoa/robotoolbox")
library(dplyr)
library(labelled)
token <- kobo_token(username = "xxxx",
password = "xxxxx",
url = "kobo.humanitarianresponse.info")
kobo_setup(url = "kobo.humanitarianresponse.info",
token = token)
@dickoa
dickoa / blastula_o365.R
Created February 1, 2022 14:17
Sending email from R using Microsoft365R
library(Microsoft365R)
library(blastula)
outl <- get_business_outlook(app = "d44a05d5-c6a5-4bbb-82d2-443123722380") ## just once after you can do `outl <- get_business_outlook()`
body <- "## Hello!
This is an email message that was generated by the blastula package.
We can use **Markdown** formatting with the `md()` function.
@dickoa
dickoa / demo_popdata.R
Created January 25, 2022 17:33
popdata demo
library(popdata)
library(unhcrthemes)
library(tidyverse)
library(janitor)
### Most function use "pd_" prefix
### "pd" stands for PopData
### Log into popdata
# pd_login()
@dickoa
dickoa / test_repeat.R
Created December 30, 2021 20:10
Have all repeat into a list of data.frame
#' @noRd
repeat_to_df <- function(x, form) {
nm <- unique(form$name[form$type %in% "begin_repeat"])
nm <- intersect(names(x), nm)
if (length(nm) > 0) {
x <- lapply(nm, function(n) {
res <- x[[n]]
res <- res[vapply(res, is.data.frame, logical(1))]
res <- rbindlist(res, fill = TRUE)