Skip to content

Instantly share code, notes, and snippets.

View yihui's full-sized avatar

Yihui Xie yihui

View GitHub Profile
@yihui
yihui / knitr-alert.Rnw
Last active April 30, 2018 22:12
A collection of demos for knitr hooks
\documentclass{beamer}
\usepackage{url}
\begin{document}
<<setup, include=FALSE, tidy=FALSE, highlight=FALSE>>=
# do not use the highlight package; will do it manually
opts_chunk$set(highlight = FALSE, tidy = FALSE)
tokens = c('dlmMLE', 'dlmModReg') # a list of words to be highlighted by structure{}
knit_hooks$set(
chunk = function(x, options) {
@yihui
yihui / stream_graph.R
Created May 2, 2012 03:07
a quick and dirty function to draw stream graphs
# data is a non-negative matrix; x is the timeline vector
stream_graph = function(data, x=1:nrow(data)) {
m = ncol(data); n = nrow(data)
data[is.na(data)] = 0
data = t(apply(cbind(0, as.matrix(data)), 1, cumsum))
data = sweep(data, 1, rowMeans(data))
plot(range(x), range(data, na.rm = TRUE), type = 'n', ann = FALSE)
for (j in 1:m) {
y = c(data[, j], rev(data[, j + 1]))
polygon(xspline(c(x, rev(x)), y, shape = -1, draw = FALSE), col = j, border = NA)
@yihui
yihui / knitr-slides.html
Created May 1, 2012 17:58
dzslides made by knitr
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="author" content="Yihui Xie" />
<title>Writing beautiful and reproducible slides quickly</title>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
@yihui
yihui / github-upload
Created May 1, 2012 16:05 — forked from wereHamster/README.md
Script to upload files to github: ./github-upload <file> [<repo>]
#!/usr/bin/env ruby
# under Ubuntu, you should install ruby1.9.1 or higher (default 1.8 does not work)
require 'json'
require 'net/https'
require 'pathname'
# Extensions
@yihui
yihui / main.Rnw
Created April 24, 2012 02:23
use knit() with a loop to write reports for subsets of data
\documentclass{article}
\begin{document}
<<run-all, results='hide', message=FALSE>>=
library(xtable)
out = NULL
for (g in unique(mtcars$gear)) {
out = c(out, knit_child('template.Rnw', sprintf('gear-%s.tex', g)))
}
@yihui
yihui / readme.md
Created April 22, 2012 16:36
use a chunk option to control output width in knitr
@yihui
yihui / knitr-breaklines.Rnw
Created March 26, 2012 21:15
How to break lines in knitr output with the listings package
% see http://yihui.name/knitr/demo/listings/ for output
\documentclass{article}
% define a new environment to break lines for error messages
\lstnewenvironment{Serror}{\lstset{style=Routstyle,breaklines=true}}{}
<<setup, include=FALSE>>=
render_listings()
@
@yihui
yihui / animation-useR2010-Xie.R
Created March 23, 2012 03:33
My Talk on Animations at useR! 2010 (NIST, Gaithersburg)
pdf.options(width = 7, height = 5, family = "AvantGarde")
library(RColorBrewer)
library(animation)
# title page
# pdf('titlepic.pdf', width=7, height=4)
ani.options(nmax = 66, interval = 0)
data(HuSpeech)
par(mar = c(4, 4, 0.1, 0.1))
@yihui
yihui / NpptoR-auto-completion.R
Created March 21, 2012 03:09
An R script to generate R.xml for auto-completion in Notepad++
# this script will generate a file 'R.xml' for auto-completion in Notepad++; I
# wrote it when my programming style was pretty bad, but this script seems to be
# still functional anyway
# R.xml will be generated under your current work directory: getwd()
# load some packages to the search path:
pkg <- installed.packages()[, 'Priority']
sapply(names(pkg)[!is.na(pkg)], library, character.only = TRUE)
# you may load any installed packages here