This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
library(data.table) | |
#' Specifies the host URL of the API to use | |
#' | |
#' A helper function to conveniently switch different APIs. | |
#' | |
#' @param instring Either "kobo", "kobohr", "ona", or a custom (full) URL. | |
#' @return A single string with the URL to use. | |
#' @note API URLs are made available for KoBo Toolbox ("kobo", \url{https://kc.kobotoolbox.org/api/v1/}), KoBo Humanitarian Response ("kobohr", \url{https://kc.humanitarianresponse.info/api/v1/}), and Ona ("ona", \url{https://ona.io/api/v1/}). For your own installation, or other installations using the same API but accessed at a different URL, enter the full URL. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rvest) | |
library(jsonlite) | |
issuu <- function(url) { | |
doc <- read_html(url) | |
props <- doc %>% | |
html_nodes("script") %>% | |
.[[4]] %>% | |
html_text() %>% | |
gsub("window.issuuDataCache = ", "", .) %>% | |
fromJSON() %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyr) | |
library(data.table) | |
library(iotools) | |
library(microbenchmark) | |
### | |
### Sample data | |
### | |
mydf <- data.frame( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CONVERT A SET OF PNGS TO A GIF | |
# | |
# -delay = the amount of time for each frame | |
# -loop = set to zero for infinite repeat | |
# +repage = in case you are having trouble with extra transparency around image | |
convert -delay 20 -loop 0 +repage *.png animate.gif | |
# CROP A SET OF PNGS TO SAME SIZE | |
# | |
# -crop = defined as: {width}x{height}+{x_coord}+{y_coord} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getDots <- function(...) sapply(substitute(list(...))[-1], deparse) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Should be faster than the other option here, hopefully with not too much overhead compared to `melt` | |
NA_type <- function(string) { | |
switch(string, | |
double = NA_real_, | |
integer = NA_integer_, | |
complex = NA_complex_, | |
character = NA_character_, | |
NA) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flatten <- function(indt, cols, drop = FALSE) { | |
if (!is.data.table(indt)) indt <- as.data.table(indt) | |
x <- unlist(indt[, lapply(.SD, function(x) max(lengths(x))), .SDcols = cols]) | |
nams <- paste(rep(cols, x), sequence(x), sep = "_") | |
indt[, (nams) := unlist(lapply(.SD, transpose), recursive = FALSE), .SDcols = cols] | |
if (isTRUE(drop)) { | |
indt[, (nams) := unlist(lapply(.SD, transpose), recursive = FALSE), | |
.SDcols = cols][, (cols) := NULL] | |
} | |
indt[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wordExtract <- function(instring, number, start = TRUE, after = NULL) { | |
len <- length(gregexpr("\\S+", instring)[[1]]) | |
mlen <- if (is.null(after)) number else number + after | |
if (len <= mlen) stop("can't do what you've asked for....") | |
if (!is.null(after) & !isTRUE(start)) { | |
start <- TRUE | |
message("start specified as FALSE but ignored") | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getDataList <- function(owner) { | |
require(gWidgets) | |
options(guiToolkit="tcltk") | |
username = ginput("Enter your username: ") | |
password = ginput("Enter your password: ") | |
url <- "curl -X GET https://kc.kobotoolbox.org/api/v1/data" | |
cmd <- sprintf("%s.csv?owner=%s -u %s:%s", url, owner, username, password) | |
read.csv(text = system(cmd, intern = TRUE, ignore.stderr = TRUE)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(1) | |
myVec <- sample(as.character(-1:1), 100000, TRUE) | |
library(microbenchmark) | |
funAM <- function(invec = myVec) factor(invec, levels = as.character(-1:1), labels = c("no", "maybe", "yes")) | |
funJ1 <- function(invec = myVec) c("no", "maybe", "yes")[as.numeric(invec) + 2] | |
funJ2 <- function(invec = myVec) c("no", "maybe", "yes")[match(invec, -1:1)] | |
funJ3 <- function(invec = myVec) unname(c("-1"="no", "0"="maybe", "1"="yes")[invec]) | |
funJ4 <- function(invec = myVec) ifelse(invec == -1, "no", ifelse(invec == 0, "maybe", "yes")) |