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
iter_index_matrix <- function(dims, chunk_size = 1L) { | |
f_split <- cumprod(dims) <= chunk_size | |
has_next <- TRUE | |
if (all(f_split)) { | |
next_element <- function() { | |
if (!has_next) stop("StopIteration") | |
has_next <<- FALSE | |
expand.grid(lapply(dims, seq_len)) | |
} | |
} else { |
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(purrr) | |
library(furrr) | |
plan(multiprocess) | |
x <- iris$Sepal.Length | |
n_cores <- parallel::detectCores() - 1 | |
N <- length(x) | |
k <- ceiling(N / n_cores) | |
split_factor <- gl(n_cores, k) %>% head(N) | |
x_list <- split(x, split_factor) |
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
head.mcmc <- function(x, n = 6L, ...) { | |
mcpar <- attr(x, "mcpar") | |
start <- mcpar[1] | |
max_end <- mcpar[2] | |
thin <- mcpar[3] | |
niter <- nrow(x) | |
end <- start + niter - 1 | |
attr(x, "mcpar")[2:3] <- c(end, 1) | |
out <- window(x, end = min(start + n - 1, end)) |
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(shiny) | |
library(DT) | |
library(sparkline) | |
library(tidyverse) | |
ui <- fluidPage( | |
titlePanel("Sparkline in DT"), | |
sidebarLayout(NULL, | |
mainPanel( | |
dataTableOutput("table_with_sparkline") |
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(tidyverse) | |
library(rvest) | |
qiita_get_organization_member <- function(organization) { | |
target_url <- str_glue("https://qiita.com/organizations/{organization}/members") | |
sess <- html_session(target_url) | |
member_ids <- c() | |
while (!is.null(sess)) { | |
ids <- sess %>% |
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(dplyr) | |
library(ggplot2) | |
data <- iris %>% filter(Species != "setosa") %>% | |
select(Petal.Width, Sepal.Length, Species) %>% | |
sample_frac(1) | |
ggplot(data, aes(Petal.Width, Sepal.Length)) + | |
geom_point(aes(color = Species), size=4) + xlab("") + ylab("") + | |
scale_x_continuous(label=NULL) + |
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
f <- function(X) { | |
X <- as.matrix(X) | |
limit <- qchisq(0.5, df = ncol(X)) | |
apply(X, 1, function(row) { | |
if(sum(row^2) > limit) 1 else -1 | |
}) | |
} | |
D <- 2 | |
M <- 2200 |
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
f <- function(X) { | |
X <- as.matrix(X) | |
limit <- qchisq(0.5, df = ncol(X)) | |
apply(X, 1, function(row) { | |
if(sum(row^2) > limit) 1 else -1 | |
}) | |
} | |
D <- 2 |
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
insert_data_frame <- function(df, df_added, col) | |
{ | |
index <- which(col == names(df)) | |
data.frame(append(df, list(df_added = df_added), index))[, -index] | |
} |
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
prophet_shf <- function(model, periods, freq = "d", k = 3) { | |
N <- nrow(model$history) | |
if (periods %% 2 == 1) periods <- periods + 1 | |
preserve <- (k + 1) * periods / 2 | |
n_history <- N - preserve - 1 | |
if (n_history < periods * 2) warning("History is too short.") | |
result <- data.frame() | |
while (n_history < N - periods) { | |
data_hist <- head(model$history, n_history) | |
m <- prophet(data_hist, growth = model$growth, |
NewerOlder