Skip to content

Instantly share code, notes, and snippets.

View yihui's full-sized avatar

Yihui Xie yihui

View GitHub Profile
@emitanaka
emitanaka / collapseoutput.js
Created July 20, 2019 04:43
Collapsible Code Output for `xaringan`
<script>
(function() {
var divHTML = document.querySelectorAll(".details-open");
divHTML.forEach(function (el) {
var preNodes = el.getElementsByTagName("pre");
var outputNode = preNodes[1];
outputNode.outerHTML = "<details open class='output'><summary>Output</summary>" + outputNode.outerHTML + "</details>";
})
})();
(function() {
@kleinschmidt
kleinschmidt / knit.sh
Created April 17, 2013 19:55
Little shell script to streamline using knitr from the command line, or in case you want to use it with something like emacs which can call a shell script to compile a document.
#!/bin/bash
# knit.sh -- Dave Kleinschmidt, April 2013
# streamline knitting of Rnw files from the command line.
usage="Usage: $0 input-filename.Rnw [-nolatex] [-notangle]"
if [ $# -lt 1 ]; then
echo $usage
exit 1
fi
@ramhiser
ramhiser / find_peaks.r
Last active July 2, 2021 06:08
Find local maxima (peaks) in a vector
#' Finds the local maxima (peaks) in the given vector after smoothing the data
#' with a kernel density estimator.
#'
#' First, we smooth the data using kernel density estimation (KDE) with the
#' \code{\link{density}} function. Then, we find all the local maxima such that
#' the density is concave (downward).
#'
#' Effectively, we find the local maxima with a discrete analogue to a second
#' derivative applied to the KDE. For details, see this StackOverflow post:
#' \url{http://bit.ly/Zbl7LV}.
@johnmyleswhite
johnmyleswhite / gist:5248212
Created March 26, 2013 19:06
The Joys of Sparsity: Forward Stagewise Regression
# Generate (x, y) data with a sparse set of active predictors
# prob controls the frequency of predictors having zero effect
function simulate_date(n::Integer, p::Integer, prob::Real)
x = randn(n, p)
beta = randn(p)
for j in 1:p
if rand() < prob
beta[j] = 0.0
end
end
@kbroman
kbroman / sprintf_fix.R
Created March 21, 2013 23:04
R function to deal with sprintf("%.2f", ...) returning -0.00
# a function to deal with sprintf("%.2f", ...) returning -0.00
# see https://twitter.com/hspter/status/314858331598626816
f <- function(..., dig=2) {
g <- sprintf(paste0("%.", dig, "f"), ...)
z <- paste0("0.", paste(rep("0", dig), collapse=""))
g[g==paste0("-",z)] <- z
g
}
@even4void
even4void / Makefile
Created October 25, 2012 18:35
A sample demo of R Markdown with pandoc
RMDFILE=demo-rmd-pandoc
PANDOC=~/.cabal/bin/pandoc
all:
Rscript -e "require(knitr); require(markdown); knit('$(RMDFILE).rmd', '$(RMDFILE).md'); purl('$(RMDFILE).rmd')"
${PANDOC} --mathjax --toc -B header.html -A footer.html --bibliography refs.bib --css markdown.css -s $(RMDFILE).md -o $(RMDFILE).html
@kohske
kohske / script.R
Created June 22, 2012 21:44
report generation by spin
#' # Rスクリプトからレポート生成
#' # `spin`ning in knitr package
#'
#' ### @kohske
#'
#' Rスクリプトはこちら
#' https://gist.github.com/gists/2975392
#'
#' ---
#'
@richfitz
richfitz / Rd2Knitr2HTML.R
Created May 10, 2012 21:36
Convert Rd files to HTML, knit()'ing examples
#!/usr/bin/Rscript
library(tools)
library(knitr)
library(sowsear)
opts_knit$set(progress = FALSE, verbose = FALSE)
## We need a list of files and a package to start.
args <- commandArgs(TRUE)
package <- args[[1]]
files <- args[-1]
@ramnathv
ramnathv / gfm-table.md
Created March 16, 2012 16:08
GFM Table with knitr + ascii

Here is how you can make a table in GFM format using knitr + ascii

render_gfm()
gfm_table <- function(x, ...) {
    require(ascii)
    y <- capture.output(print(ascii(x, ...), type = "org"))
 # substitute + with | for table markup
@ramnathv
ramnathv / ext-env.tex
Created February 26, 2012 03:39
Collection of knitr Hooks
% Full Screen Environment for Beamer Slides
\newenvironment{changemargin}[2]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%