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
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) |
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
# 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=""), |
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
############################################################ | |
#### 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 |
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(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 |
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 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 |
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
#' 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") | |
) |
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
# 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! |
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
#!/bin/bash | |
set -e | |
DIR="/tmp/test-merge" | |
if [ -d "$DIR" ]; then | |
rm -rf "$DIR" | |
fi |
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
#!/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!" |
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
#!/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 |
OlderNewer