Skip to content

Instantly share code, notes, and snippets.

View strboul's full-sized avatar

Metin Yazici strboul

View GitHub Profile
days <- seq_along(1:365)
points <- list()
for (i in days) {
points[[i]] <- exp(-i/500)
}
x <- unlist(points)
df <- cbind.data.frame(x, days)
@strboul
strboul / compound.R
Last active March 16, 2018 21:46
compound_interest.R
# How much interest etc. would you pay for a mortgage?
compound <- function(rate, money, n) {
prin <- money * (1+rate/100)^(0:(n-1))
int <- prin * (rate/100)
totalInt <- sum(int)
totalMoney <- money + totalInt
rentpermonth <- totalMoney / (n * 12)
textTable <- c(
cat(rep("=", 30), "\n ", sep=""),
############################################################
#### Anonymous functions in R ####
############################################################
capitals <- c("Amsterdam", "Paris", "Warszawa", "Istanbul")
lapply(capitals, function(name) nchar(name))
# function with no name assigned to it, and does not appear in global env.:
{function(x) x^2}(5)
(function(x) x^2)(4) # without curly brackets
library(microbenchmark)
x <- sample(100000)
microbenchmark(x[order(x, method = "auto")],
x[order(x, method = "shell")],
x[order(x, method = "radix")])
# Unit: milliseconds
# expr min lq mean median uq max
# x[order(x, method = "auto")] 1.790454 1.863924 2.128368 1.930263 2.362573 3.328237
# x[order(x, method = "shell")] 17.530465 17.991802 18.914891 18.641179 19.763667 21.762103
# x[order(x, method = "radix")] 1.791103 1.847488 2.439882 1.975531 2.265257 33.209701
@strboul
strboul / eval_string.R
Created June 23, 2018 12:11
evaluate expressions (with a keyword)
#' Convert strings to R objects
#'
#' @param x a string
#' @details It is particularly designed for the YAML strings that can be
#' converted into R code. Strings to be evaluated should start with *`'r'`*
#' letter and be surrounded with backticks. If this is not the case, it will
#' return string as is. It is advised to keep all string in double quotes,
#' otherwise unquoted strings will be converted by the yaml reader.
#' @noRd
@strboul
strboul / past_input.R
Created June 26, 2018 16:45
Shiny record past input values
#' Record the past input values 'only for a session'. Persistent store approach is different.
#' This snippet has example for selecting rows in datatable from the DT package. Reference:
#' https://stackoverflow.com/a/41199134
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput("dt")
)
@strboul
strboul / sink.R
Created August 6, 2018 20:17
'sink' diverts R output to a connection
# sink() sends output to stdout to get it written in a file.
# reports how many sink diversions are in use:
sink.number()
sink(file = "sink.log")
cat("hello")
runif(10)
cat(runif(10), sep = "\n")
sink(file = NULL) # be sure you closed the sink!
@strboul
strboul / git_test.sh
Last active September 6, 2018 19:37
Generate git repo for testing commands
#!/bin/bash
set -e
DIR="/tmp/test-merge"
if [ -d "$DIR" ]; then
rm -rf "$DIR"
fi
@strboul
strboul / shellcheck.sh
Created August 18, 2018 21:09
Automate linting for bash files (add to CI).
#!/bin/bash -e
if ! [ -x "$(command -v shellcheck)" ]; then
echo 'Program ShellCheck not found. Aborting.' >&2
exit 1
fi
# finds either .sh or .bin files
if find . -type f \( -iname \*.sh -o -iname \*.bin \) -print0 | xargs -n1 -0 shellcheck -s bash; then
echo -e "ShellCheck lint test passed!"
@strboul
strboul / docker_volumes.sh
Last active August 27, 2018 09:46
How to use Docker volumes with an R example
#!/bin/bash
# M Y 2018-08-21. An example script on Docker volumes showing
# how to run a sandboxed 'R' code inside the container.
# Tip: Using `ls` (or else) with the volumes is helpful especially
# when need to understand why some particular files cannot be found.
set -x
V="rvol"
mkdir $V