Skip to content

Instantly share code, notes, and snippets.

View yihui's full-sized avatar

Yihui Xie yihui

View GitHub Profile
@yihui
yihui / cos-thumbnail.r
Created November 24, 2010 17:41
生成统计之都主站的占位缩略图
## setwd('path/to/magnificent/images/noimage')
set.seed(123)
png("thumbnail%d.png", width = 73, height = 73)
par(mar = rep(0, 4), ann = FALSE, xaxt = "n", yaxt = "n")
hist(rnorm(100), main = "", col = heat.colors(8))
boxplot(list(rnorm(100), runif(100)), col = 2:3)
sunflowerplot(iris[, 1:2], pch = 20, col = "gold", size = 1/20)
@yihui
yihui / check_R_version.R
Created February 9, 2011 04:25
check the version of R on CRAN
x = readLines("http://cran.r-project.org/sources.html")
# the version number is in the next line of 'The latest release'
rls = x[grep("latest release", x) + 1L]
newver = gsub("(.*R-|\\.tar\\.gz.*)", "", rls)
oldver = paste(getRversion(), collapse = ".")
# new version available?
compareVersion(newver, oldver) == 1L
@yihui
yihui / kde2d-gwidgets.R
Created December 13, 2011 04:55
change the binwidth of 2D KDE with rgl and gWidgets
## adpated from demo('bivar') in rgl
rgl.demo.bivar = function(x, y, bw, ngrid = 25, ...) {
library(MASS); library(rgl)
n = length(x)
denobj = kde2d(x, y, bw, n = ngrid)
den.z = denobj$z; xgrid = denobj$x; ygrid = denobj$y
bi.z = dnorm(xgrid) %*% t(dnorm(ygrid))
zscale = 20; clear3d()
spheres3d(x, y, rep(0, n), radius = 0.1, color = "#CCCCFF")
surface3d(xgrid, ygrid, den.z * zscale, color = "#FF2222", alpha = 0.5)
@yihui
yihui / highlight-to-css.R
Created January 17, 2012 04:56
Turn Andre Simon's highlight themes to CSS files for knitr
## put Simon's theme files under ./themes-andre/, and CSS files under ./themes/
## then run generate_css()
## author: Ramnath Vaidyanathan
#' Parses theme files from Highlight v3.06 by Andre Simon
#'
#' @param theme_name name of the theme to parse
#' @return a list containing the theme specifications
theme_to_list <- function(theme_name){
theme <- readLines(sprintf('themes-andre/%s.theme', theme_name))
@yihui
yihui / reverse.c
Created February 7, 2012 01:31
a C function for demo purpose
void reverse(double *a, int *na, double *b)
{
int i;
for(i = 0; i < *na; i++)
b[i] = a[*na - i - 1];
}
@yihui
yihui / 3rd-ChinaR-gWidgets-Yihui-Xie.R
Created February 7, 2012 01:34
talk on gWidgets at 3rd ChinaR conference
# install.packages(c('gWidgetsRGtk2', 'gWidgetstcltk', 'gWidgetsrJava'))
library(gWidgets)
## different interfaces
options(guiToolkit = 'RGtk2')
g = gwindow('RGtk2')
gtext('RGtk2', container = g)
gbutton('RGtk2', container = g)
@yihui
yihui / ChinaR-Conference-Intro-2008-Yihui-Xie.R
Created February 7, 2012 02:43
R scripts used in the 1st China R conference
# 参会单位
x = c(2, 1, 3, 2, 1, 1, 2, 2, 2, 3, 1, 2, 2, 2, 2,
1, 1, 1, 1, 4, 2, 2, 3, 1, 1, 2, 2, 2, 2, 3, 1, 1, 3, 1,
1, 1, 1, 1, 1, 2, 2, 1, 1, 3, 3, 3, 1, 2, 1, 2, 3, 1, 2,
1, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 1, 3, 1, 1, 1, 2, 2,
1, 1, 1, 1, 1, 3, 1, 1, 3, 1, 2, 1, 2, 1, 1, 2, 3, 2, 2,
2, 3, 1, 2, 2, 3, 2, 1, 3, 2, 1, 1, 1, 1, 2, 2, 1, 3, 3,
1, 1, 1, 2, 1, 2, 2, 1, 3, 2, 1, 1, 1, 1, 2, 2, 2, 2, 1,
2, 2)
# pdf("institute.pdf", width = 7, height = 3, family = "GB1")
@yihui
yihui / footprint.R
Created February 7, 2012 03:11
draw footprints for fun...
foot = function(x, y, ...) {
points(x, y, cex = 10, ...)
points(x, y + 2, cex = 5, ...)
points(x - 0.4, y + 1.2, cex = 7, ...)
points(x + 0.4, y + 1.2, cex = 4, ...)
}
for (j in 1:3) {
x = sample(seq(1, 30, 3))
plot(x)
for (i in 1:10) {
@yihui
yihui / knitr-beamer2.Rnw
Created February 11, 2012 20:07
Another minimal beamer example for knitr
% this is a simplified version of
% https://github.com/yihui/knitr/blob/master/inst/examples/knitr-beamer.Rnw
\documentclass{beamer}
\begin{document}
<<setup, include=FALSE>>=
# smaller font size for chunks
opts_chunk$set(size = 'footnotesize')
@
\title{A Minimal Demo of knitr}
\author{Paul Hiemstra \and Yihui Xie}
@yihui
yihui / brush-loader.html
Created February 11, 2012 22:53
SyntaxHighlighter Brush for the R Language
<script type="text/javascript">
SyntaxHighlighter.autoloader(
"r  path/to/your/syntaxhighlighter/scripts/shBrushR.js",
"plain  path/to/your/syntaxhighlighter/scripts/shBrushPlain.js",
"sql  path/to/your/syntaxhighlighter/scripts/shBrushSql.js",
"js  path/to/your/syntaxhighlighter/scripts/shBrushJScript.js",
"html xml  path/to/your/syntaxhighlighter/scripts/shBrushXml.js"
);
SyntaxHighlighter.defaults["toolbar"] = false;
SyntaxHighlighter.all();