Skip to content

Instantly share code, notes, and snippets.

View mrdwab's full-sized avatar

Ananda Mahto mrdwab

View GitHub Profile
@mrdwab
mrdwab / kobotoolboxAPI.R
Last active January 31, 2016 16:52
Used to retrieve data entered using the KoBo collect app and stored on kc.kobotoolbox.org
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.
@mrdwab
mrdwab / issuu_downloader.R
Created January 24, 2016 17:21
Downloads the images that comprise an issuu publication, uses ImageMagick to convert the jpegs to a PDF. Somewhat glitchy, and not the most stable since it relies on item positions in at least two places (but could probably be rewritten to avoid that).
library(rvest)
library(jsonlite)
issuu <- function(url) {
doc <- read_html(url)
props <- doc %>%
html_nodes("script") %>%
.[[4]] %>%
html_text() %>%
gsub("window.issuuDataCache = ", "", .) %>%
fromJSON() %>%
library(tidyr)
library(data.table)
library(iotools)
library(microbenchmark)
###
### Sample data
###
mydf <- data.frame(
@mrdwab
mrdwab / Image_Magick.sh
Last active January 3, 2016 10:33
Image Magick snippets
# 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}
getDots <- function(...) sapply(substitute(list(...))[-1], deparse)
@mrdwab
mrdwab / MELT.R
Last active March 3, 2018 16:16
Possible replacement for `merged.stack`. Need to figure out how to incorporate sep in here too.... See http://stackoverflow.com/a/34427860/1270695
# 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)
}
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[]
@mrdwab
mrdwab / wordExtract.R
Last active November 23, 2015 12:31
Extracts a certain number of words from a text string.
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")
}
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))
}
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"))