Skip to content

Instantly share code, notes, and snippets.

@akbertram
Last active July 12, 2023 02:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akbertram/48aa39fb1b941e01b0557e9375044efd to your computer and use it in GitHub Desktop.
Save akbertram/48aa39fb1b941e01b0557e9375044efd to your computer and use it in GitHub Desktop.
Examples scripts from the 2022-09-22 webinar
install.packages("remotes")
remotes::install_github("bedatadriven/activityinfo-R")
install.packages("tidyverse")
# Go make some coffee :-)
# Add a Personal API Token first and copy that into the password
library(activityinfo)
activityInfoLogin()
library(activityinfo)
sector <- "Early Childhood Development"
df <- queryTable("cm864ryl8d0yx5ob7n",
"Organization Name" = "cd3hjlwkq6ikojan.name",
"Sector Name" = "cljer4ukq6jzbgms.c3g7i69kq6jst8k3z.caxmhjxkq6jqe373c",
"Sub-sector Name" = "cljer4ukq6jzbgms.cttl2pfkq6jsxss40",
filter = sprintf('Sector.Name=="%s"', sector))
table(df$Organization.Name)
write.csv(df, file = "test.csv")
library(activityinfo)
beneficiary_form_id <- "c2owfk6l8d3geybh"
addRecord(formId = beneficiary_form_id, parentRecordId = NA_character_,
list(name = "Alex", age = 40, type = "IDP"))
provinces <- queryTable(form = "ce8oy6zl8d0yx5ob7m", id = "_id", name = "name")
provinceLookup <- provinces$id
names(provinceLookup) <- provinces$name
updateRecord(formId = beneficiary_form_id, recordId = "c3yfnm2l8d3khm82",
list(province = provinceLookup["Mon"],
location = list(latitude = 45, longitude = 2)))
library(activityinfo)
library(tidyverse)
partners <- queryTable(form = "cicy592l8d0yx5ob7i", id = "_id", name = "name")
partnerLookup <- partners$id
names(partnerLookup) <- partners$name
library(activityinfo)
library(tidyverse)
df_import <- readxl::read_xlsx("~/import.xlsx")
df_import$id <- 1:nrow(df_import)
df_clean <- df_import |>
dplyr::mutate(id = sprintf("c%d", id)) |>
dplyr::rename(partner = "Partner Name") |>
dplyr::mutate(partner = partnerLookup[partner]) |>
dplyr::rename(start_date = "Start date") |>
dplyr::rename(num_ben = "Number of beneficiaries") |>
dplyr::rename(impl_type = "Type of implementation") |>
dplyr::filter(!impl_type == "Direct or Indirect")
importTable(formId = "cccf6csl8d210954", data = df_clean,
recordIdColumn = "id")
tree <- getDatabaseTree("cglps0pl8d0yx5ob7g")
schema <- getFormSchema("cccf6csl8d210954")
newSchema <- list(
id = activityinfo::cuid(),
label = "new form",
databaseId = "cglps0pl8d0yx5ob7g",
elements = list(
list(id = activityinfo::cuid(),
label = "My text",
type = "FREE_TEXT"),
list(id = activityinfo::cuid(),
label = "Quantity",
type = "quantity",
typeParameters = list(unit = "Km")),
))
addForm(databaseId = "cglps0pl8d0yx5ob7g", newSchema)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment