Skip to content

Instantly share code, notes, and snippets.

@renkun-ken
renkun-ken / init.R
Last active November 26, 2019 12:07
vscode-R init
if (interactive()) {
local({
pid <- Sys.getpid()
tempdir <- tempdir(check = TRUE)
dir <- normalizePath(file.path(".vscode", "vscode-R"), mustWork = FALSE)
dir_session <- file.path(dir, pid)
if (dir.create(dir_session, showWarnings = FALSE, recursive = TRUE) || dir.exists(dir_session)) {
reg.finalizer(.GlobalEnv, function(e) {
unlink(dir_session, recursive = TRUE, force = TRUE)
}, onexit = TRUE)
@renkun-ken
renkun-ken / sim-demo.R
Created January 1, 2019 04:45
simulation-io-demo
library(data.table)
dir <- "~/output/sim"
dir.create(dir, showWarnings = FALSE, recursive = TRUE)
params <- expand.grid(
n = c(100, 1000, 10000),
mean = c(0.1, 0.2, 0.3),
sd = c(0.5, 1, 1.5))
@renkun-ken
renkun-ken / .tmux.conf
Created July 3, 2018 15:36
My tmux config
# Global options
## No delay for escape key press
set -sg escape-time 0
## Enable mouse control (clickable windows, panes, resizable panes)
set -g mouse on
## don't rename windows automatically
set-option -g allow-rename off
@renkun-ken
renkun-ken / active-binding.R
Last active August 25, 2016 12:07
hide-source
e <- new.env()
makeActiveBinding("private_f", function(x) {
if (missing(x)) {
NULL
} else {
sum <- 0
for (i in 1:x) {
sum <- sum + i
}
@renkun-ken
renkun-ken / product-info.csv
Created July 25, 2016 13:18
Demo data of products
id name type class released
T01 SupCar toy vehicle yes
T02 SupPlane toy vehicle no
M01 JeepX model vehicle yes
M02 AircraftX model vehicle yes
M03 Runner model people yes
M04 Dancer model people no
@renkun-ken
renkun-ken / demo.R
Created June 20, 2016 04:52
formattable: color bar + color text
library(formattable)
data <- data.frame(id = 1:10, x = rbinom(10, 100, 0.8))
formattable(data, list(x = formatter("span",
style = x ~ style(
display = "inline-block",
direction = "rtl",
"border-radius" = "4px",
"padding-right" = "2px",
@renkun-ken
renkun-ken / iris-formattable.R
Created January 24, 2016 16:03
iris-formattable
library(shiny)
library(formattable)
data <- iris
species <- levels(data$Species)
ui <- shinyUI(fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarPanel(checkboxGroupInput("species", label = "species", choices = species,
selected = species),
@renkun-ken
renkun-ken / options.R
Created January 13, 2016 04:56
option-tools
payoff <- function(f, class = NULL, type = NULL, ...) {
structure(f, class = c(class, "payoff", "function"),
type = type, args = list(...))
}
Call <- function(K, N = 1, X = 0, C = 0) {
payoff(function(x) N * (pmax(x - K, 0) - X) - abs(N) * C,
class = "option", type = "Call", K = K, N = N, X = X, C = C)
}
@renkun-ken
renkun-ken / demo.Rmd
Last active August 29, 2015 14:19
formattable (bootstrap glyphicons + bar)
```{r,results='hide',echo=FALSE,message=FALSE}
library(formattable)
df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
@renkun-ken
renkun-ken / pipeR.R
Last active August 29, 2015 14:08
simpler pipeR_world
# inspired by the amazing idea of hoxo_m
# see http://qiita.com/hoxo_m/items/3fd3d2520fa014a248cb
# shocked even if you don't understand Japanese!
pipes <- function(expr) {
expr <- substitute(expr)
pipe_expr <- if(expr == "{") {
Reduce(function(pl, p) as.call(list(quote(`%>>%`), pl, p)), expr[-1L])
} else expr
eval(pipe_expr, envir = parent.frame())