Skip to content

Instantly share code, notes, and snippets.

View yihui's full-sized avatar

Yihui Xie yihui

View GitHub Profile
@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 / 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 / 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-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 / french.lyx
Created May 16, 2012 21:51
knitr i18n tests
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass article
\use_default_options true
\begin_modules
knitr
\end_modules
\maintain_unincluded_children false
@yihui
yihui / knitrout-parskip.Rnw
Created May 21, 2012 03:03
customize the knitrout environment
\documentclass{article}
% change the space between paragraphs in chunks
\parindent = 0.0in
\parskip = 0.125in
\ifdefined\knitrout
\renewenvironment{knitrout}{
\setlength{\parskip}{0in}
}{
@yihui
yihui / petris-style.Rnw
Created May 25, 2012 20:21
I love Sweave
\documentclass{article}
\DefineVerbatimEnvironment{Sinput}{Verbatim}{xleftmargin=2em,
fontsize=\footnotesize, fontshape=sl}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
fontsize=\footnotesize}
\fvset{listparameters={\setlength{\topsep}{0pt}}}
\renewenvironment{Schunk}{\vspace{\topsep}}{\vspace{\topsep}}
\begin{document}
@yihui
yihui / grid-table.Rnw
Created May 25, 2012 22:26
draw a table by gridExtra in knitr
\documentclass{article}
\begin{document}
We can draw a table with the gridExtra package, and one problem is to
pre-determine the size of the table plot to remove the extra white
margin. Below is an example that shows you how to do it (courtesy of
baptiste auguie).
<<setup>>=
@yihui
yihui / knitr-cairo.Rnw
Created May 27, 2012 16:27
Custom graphics devices in knitr
\documentclass{article}
\begin{document}
This demo shows you how to use the Palatino font in the Cairo PDF device.
<<setup>>=
my_cairo = function(file, width, height) {
cairo_pdf(file, width, height, family = 'Palatino')
}