Skip to content

Instantly share code, notes, and snippets.

View wush978's full-sized avatar
👶
Working with my baby

Wush Wu wush978

👶
Working with my baby
View GitHub Profile
@wush978
wush978 / CMakeLists.txt
Created January 29, 2013 08:03
CMakeLists.txt for Rcpp Package Project
cmake_minimum_required(VERSION 2.8)
project(RcppPackage)
find_package(LibR)
if(${LIBR_FOUND})
else()
message(FATAL_ERROR "No R...")
endif()
message(STATUS ${CMAKE_SOURCE_DIR})
execute_process(
COMMAND ${LIBR_EXECUTABLE} "--slave" "-e" "stopifnot(require('Rcpp'));cat(Rcpp:::Rcpp.system.file('include'))"
@wush978
wush978 / jekyll.R
Last active December 12, 2015 12:09
add " " before ```
#! /usr/bin/Rscript
# convert from:
#
# ```
# xxxxx....
# ```
#
# to:
#
# ```txt
@wush978
wush978 / outline.R
Last active December 13, 2015 16:48
# outline <- c("a", "b")
# print_outline(outline):
# - a
# - b
# print_outline(outline, 1):
# - **a**
# - b
# ---
# outline <- list("a" = c("b", "c", "d"), "e" = NULL, "f" = NULL)
# print_outline(outline):
a <- "X123456789050102550703
A123456987050102660703
B123456789050102"
gregexpr("(?<id>[A-Z]\\d{9})(?<b_date>\\d{6})(?<d_date>\\d{6}){0,1}", a, perl=TRUE)
# [[1]]
# [1] 1 24 47
# attr(,"match.length")
# [1] 22 22 16
# attr(,"useBytes")
# [1] TRUE
detect_continuous <- function(src, gap = 1) {
index.bool <- diff(src) == gap
is.in_sequence <- FALSE
start <- c()
end <- c()
for(i in 1:length(index.bool)) {
if(!index.bool[i]) {
is.in_sequence <- FALSE
}
if(index.bool[i] & !is.in_sequence) {
@wush978
wush978 / html_table_popup_comment.R
Last active December 15, 2015 04:50
Key word: R, xtable, pop-up comment
#'@title Generate HTML table from data.frame and add a pop-up comment on specific table entry
#'@param r_dataframe a data.frame object
#'@param js_show a list object whose name is the column names of r_dataframe, value is the added pop-up comment
html_table_popup_comment <- function(r_dataframe, js_show, ...) {
stopifnot(require(xtable))
global.env <- environment()
r_dataframe.xtable <- xtable(r_dataframe)
global.env$sanitize <- function(str) {
result <- str
result <- gsub("&", "&amp ", result, fixed = TRUE)
@wush978
wush978 / batch_fetch.PostgreSQLConnection.R
Last active December 17, 2015 14:39
change tb.name to query for generalization
batch_fetch.PostgreSQLConnection <- function(db, query, n, m) {
require(data.table)
start.index <- 0
pb <- txtProgressBar(max = n)
dbSendQuery(db, "START TRANSACTION READ ONLY")
dbSendQuery(db, sprintf('DECLARE res CURSOR FOR %s', query))
res <- dbSendQuery(db, sprintf("fetch %d from res", m))
tb.part <- fetch(res, m)
tb.class <- sapply(tb.part, class)
@wush978
wush978 / extract_subpattern.R
Created May 28, 2013 05:22
extract the subpattern of the perl-based regular expression
extract_subpattern <- function(text, pattern) {
gregexpr.result <- gregexpr(pattern=pattern, text=text, perl=TRUE)
retval <- list()
for(i in 1:length(gregexpr.result)) {
result <- gregexpr.result[[i]]
retval[[i]] <- list()
for(name in attr(result, "capture.names")) {
start <- attr(result, "capture.start")[,name]
len <- attr(result, "capture.length")[,name]
retval[[i]][[name]] <- substr(text[i], start=start, stop = start + len - 1)
# Escape Zombie Land!
# This is a simulation an escape from a hot zombie zone. It freezes and gives an error if you get get killed so you had best not. You attempt to navigate the zone by constructing waypoints.
# This is not a very clean set up and I would like to clean it up. However, I have spent way more time on it than I intended. So I might come back to it another day.
# Zombies are distributed on a 10 x 10 grid.
gridxy = c(10,10)
# The number of zombies on the map
RMD_FILES = $(wildcard *.Rmd)
HTML_FILES = $(RMD_FILES:.Rmd=.html)
all: $(HTML_FILES)
(cd img && make)
%.html: %.md
pandoc -s -S -i -t slidy --mathjax -V slidy_url=slidy --base-header-level=3 $(@:.html=.md) -o $@
%.md: %.Rmd