Skip to content

Instantly share code, notes, and snippets.

View lgatto's full-sized avatar

Laurent Gatto lgatto

View GitHub Profile
@lgatto
lgatto / test.Rmd
Last active August 29, 2015 14:05
Inline and external apps in Rmd
---
title: " Embedded Shiny Apps"
runtime: shiny
output:
html_document:
theme: united
---
## Setup
> isIn <- function(x, y) {
+ sel <- match(x, y)
+ y[sel]
+ }
> x <- sample(LETTERS, 5)
> isIn(x, LETTERS)
[1] "Q" "Z" "K" "V" "U"
>
> x
[1] "Q" "Z" "K" "V" "U"
@lgatto
lgatto / insert-code-chunk.el
Last active January 7, 2021 18:28
Insert appropriate code chunk in emacs buffer
(defun insert-md-code-chunk ()
"Insert Rmd code chunk"
(interactive)
(insert "```\n\n```")
(backward-char 4))
(defun insert-rmd-code-chunk ()
"Insert Rmd code chunk"
(interactive)
(insert "```{r}\n\n```")
## l1 is a list with 20 numeric elements of length 1e4 each
set.seed(123)
l2 <- l1 <- replicate(20,rnorm(1e4),simplify=FALSE)
length(l1)
sapply(l1,length)
## l2 is the same list with names
names(l2) <- paste0("X", 1:length(l2))
;; grep -h X-Key */*/cur/* | sed -e 's/X-Keywords: //; s/, /\n/g' | sort | uniq > xkeys.txt
(setq mu4e-xkeys "~/Maildir/xkeys.txt")
(defun read-lines (f)
"Return a list of lines of a file at f."
(with-temp-buffer
(insert-file-contents f)
(split-string (buffer-string) "\n" t)))
library(base64enc)
library(RJSONIO)
library(httr)
default_key <- function () {
key <- Sys.getenv("POSTMARKAPP_API_KEY")
if (key == "") {
stop("Either provide key or set envvar POSTMARKAPP_API_KEY", call. = FALSE)
}
key
@lgatto
lgatto / getDependencies.R
Last active December 10, 2015 21:18
Get/install an R package's dependencies.
getDependencies <- function(p,
rep = c("BioCsoft", "BioCann", "BioCexp", "BioCextra", "CRAN"),
biocVersion = "2.12",
depLevels = c("Depends", "Imports", "Suggests"),
filter = TRUE) {
rep <- match.arg(rep)
if (rep == "CRAN") {
rep <- getOption("repos")["CRAN"]
} else {
biocMirror <- getOption("BioC_mirror", "http://bioconductor.org")
@lgatto
lgatto / deseq-vs-edger.R
Created September 19, 2012 09:38 — forked from stephenturner/deseq-vs-edger.R
DESeq vs edgeR comparison
## http://gettinggeneticsdone.blogspot.co.uk/2012/09/deseq-vs-edger-comparison.html
library(DESeq)
library(edgeR)
library(VennDiagram)
# Read in data ------------------------------------------------------------
## Use pasilla data
datafile = system.file( "extdata/pasilla_gene_counts.tsv", package="pasilla" )
@lgatto
lgatto / ksvm-models.R
Created June 8, 2012 21:29
Different ksvm models
> library(kernlab)
> data(iris)
> irismodel1 <- ksvm(Species ~ ., data = iris)
Using automatic sigma estimation (sigest) for RBF or laplace kernel
> irismodel2 <- ksvm(Species ~ ., data = iris)
Using automatic sigma estimation (sigest) for RBF or laplace kernel
> table(predict(irismodel1, iris[,-5]), predict(irismodel2, iris[,-5]))
setosa versicolor virginica
setosa 50 0 0
@lgatto
lgatto / scatterhist.R
Created June 4, 2012 12:31
scatterhist
scatterhist <- function(x, y, xlab="", ylab="", ...){
zones <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
layout(zones, widths=c(4/5,1/5), heights=c(1/5,4/5))
xhist <- hist(x, plot=FALSE)
yhist <- hist(y, plot=FALSE)
top <- max(c(xhist$counts, yhist$counts))
par(mar=c(3,3,1,1))
plot(x,y, ...)
par(mar=c(0,3,1,1))
barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)