Skip to content

Instantly share code, notes, and snippets.

@artemklevtsov
artemklevtsov / str-pad.R
Last active May 20, 2016 05:48
Based on the stringi::stri_pad and tools::showNonASCII
str_pad <- function(str, width = floor(0.9 * getOption("width")), side = c("left", "both", "right")) {
side <- match.arg(side)
asc <- iconv(str, "latin1", "ASCII")
if (any(is.na(asc) | asc != str))
width <- width + nchar(str, "bytes") - nchar(str, "width")
switch(side,
left = sprintf("%*s", width, str),
right = sprintf("%-*s", width, str),
both = sprintf("%-*s", width, sprintf("%*s", floor(width / 2), str)))
}
rm_words <- function(string, words) {
stopifnot(is.character(string))
stopifnot(is.character(words))
splitted <- strsplit(string, " ", fixed = TRUE)
if (length(string) > 1)
res <- vapply(splitted, function(x) paste(x[!tolower(x) %in% words], collapse = " "), character(1L))
else
res <- paste(splitted[[1L]][!tolower(splitted[[1L]]) %in% words], collapse = " ")
return(res)
}
@artemklevtsov
artemklevtsov / get-env.R
Last active January 24, 2016 06:53
Find an object environment name
getEnvName <- function(f) {
attached <- c(environmentName(.GlobalEnv), loadedNamespaces())
envs <- c(.GlobalEnv, lapply(attached[-1], .getNamespace))
attached[vapply(envs, function(env) exists(f, env, inherits = FALSE), logical(1))]
}
# returns only a first found result
getEnvName2 <- function(f) {
envs <- c(.GlobalEnv, lapply(loadedNamespaces(), .getNamespace))
for (env in envs) {
@artemklevtsov
artemklevtsov / shiny-progressbar.R
Last active January 29, 2016 17:36
Bootstrap progress bar widget for the Shiny framework. Source: http://getbootstrap.com/components/#progress
prgoressBar <- function(value = 0, label = FALSE, color = "aqua", size = NULL,
striped = FALSE, active = FALSE, vertical = FALSE) {
stopifnot(is.numeric(value))
if (value < 0 || value > 100)
stop("'value' should be in the range from 0 to 100", call. = FALSE)
if (!(color %in% shinydashboard:::validColors || color %in% shinydashboard:::validStatuses))
stop("'color' should be a valid status or color.", call. = FALSE)
if (!is.null(size))
size <- match.arg(size, c("sm", "xs", "xxs"))
text_value <- paste0(value, "%")
@artemklevtsov
artemklevtsov / melt.R
Last active January 28, 2017 03:47
Improved version of the reshape2::melt function
melt2 <- function(data, id.vars, measure.vars, variable.name = "variable", value.name = "value", na.rm = FALSE) {
varnames <- names(data)
n <- .row_names_info(data, 2L)
if (!missing(id.vars) && is.numeric(id.vars))
id.vars <- varnames[id.vars]
if (!missing(measure.vars) && is.numeric(measure.vars))
measure.vars <- varnames[measure.vars]
if (missing(id.vars) && missing(measure.vars)) {
discrete <- vapply(data, function(x) is.factor(x) || is.character(x) || is.logical(x),
FUN.VALUE = logical(1L), USE.NAMES = FALSE)
get_psylab_images <- function(term, output.dir = ".") {
stopifnot(is.character(term))
if (length(term) > 1L)
term <- paste(term, collapse = " ")
get_results <- function(query) {
api_url <- "http://psylab.info/api.php"
response <- httr::GET(api_url, query = query)
httr::stop_for_status(response, "Send API query")
jsonlite::fromJSON(httr::content(response, as = "text"), flatten = TRUE)
}
rep.data.frame <- function(x, times) {
x <- lapply(x, rep.int, times = times)
class(x) <- "data.frame"
if (!is.numeric(rnames <- attr(x, "row.names")))
attr(x, "row.names") <- make.unique(rep.int(rnames, times))
else
attr(x, "row.names") <- .set_row_names(length(rnames) * times)
x
}
spiral <- function(n) {
stopifnot(is.numeric(n))
stopifnot(n > 0)
offset <- c(1, n, -1, -n)
reps <- n - seq_len(n * 2 - 1L) %/% 2
indicies <- rep(rep_len(offset, length(reps)), reps)
indicies <- cumsum(indicies)
values <- integer(length(indicies))
values[indicies] <- seq_along(indicies)
matrix(values, n, n, byrow = TRUE)
# Options
options(repos = list("CRAN" = "https://cloud.r-project.org/"))
#options(help_type = "html")
options(Ncpus = 4L)
options(digits.secs = 3)
options(tab.width = 4)
options(max.print = 100)
options(scipen = 10)
options(show.signif.stars = FALSE)
#options(stringsAsFactors = FALSE)
#!/usr/bin/env python
import sys
import errno
from argparse import ArgumentParser
from os.path import isfile, expanduser
from urllib.request import urlopen
if __name__ == "__main__":
parser = ArgumentParser(description = "Convert Good Line IPTV playlist for the SMplayer.")