Skip to content

Instantly share code, notes, and snippets.

@bearloga
bearloga / Embed Plot in HTML.R
Created February 6, 2015 20:33
Sometimes you just want to send a report to someone as a single HTML file (e.g. when PDF isn't an option) and you want to avoid the hassle of external images. This code lets you convert a plot in R into base64 string that you can include in the HTML source.
library(base64) # install.packages('base64')
pngfile <- tempfile()
png(pngfile,width=600,height=600,pointsize=12) # adjust as needed
# Plotting code...
dev.off()
# Prints the HTML code to console
cat(img(pngfile))
# Copy and paste the whole output into your HTML source to embed the image
@bearloga
bearloga / Distributions.md
Last active August 29, 2015 14:14
During the character creation process in Dungeons & Dragons (D&D), if one chooses to roll for their attributes (dexterity, charisma, constitution, wisdom, intelligence, strength), they must roll 4 6-sided dice 6 times, each time taking the total of the top three dice with the highest outcomes. This is a simulation study (N=10,000) using R of dis…

Dice Distribution

Dice Combination Proportion
14, 13, 12, 11, 10, 9 0.0015
16, 15, 14, 13, 12, 11 0.0015
15, 14, 13, 12, 10, 9 0.0014
16, 15, 14, 13, 11, 10 0.0014
14, 13, 13, 12, 11, 10 0.0013
15, 14, 13, 12, 12, 11 0.0013
@bearloga
bearloga / tabletools.R
Created January 9, 2014 00:40
This shows how to add a DataTables plug-in (in this case TableTools) to your Shiny app.
library(shiny)
library(ggplot2)
# Download DataTables JS file and TableTools full package from http://datatables.net/download/
# Change the paths appropriately:
addResourcePath('datatables','/Users/yourusername/Downloads/DataTables-1.9.4/media')
addResourcePath('tabletools','/Users/yourusername/Downloads/TableTools-2.1.5/media')
runApp(list(
ui = basicPage(
@bearloga
bearloga / Parallel RJags with Progress Monitoring.R
Last active December 27, 2015 04:58
par.trace.samples() runs independent MCMC chains in parallel on a multicore/multiCPU system. It breaks up a single long run into several smaller chunks and that allows it to report the simulation's progress. At the end of the run, all the chunks are combined into a single mcmc object. The mcmc objects from the chains are then combined into a sin…
# This file contains two functions:
# - combine.samples (used by par.trace.samples)
# - par.trace.samples
# and an example at the bottom.
## Author: Mikhail Popov (mikhail [at] mpopov.com)
# install.packages("rjags") # JAGS must be installed on system
# install.packages("doMC") # Unix only
@bearloga
bearloga / notify.R
Last active December 26, 2015 18:49 — forked from hrbrmstr/notify.R
notify <- function(msg="Operation complete") {
in.osx <- (Sys.info()['sysname'] == "Darwin")
in.rstudio <- (Sys.getenv("RSTUDIO") == "1")
in.rgui <- (Sys.getenv("R_GUI_APP_REVISION") != "")
if (in.rstudio) { # hack to see if running in RStudio
title <- "RStudio"
sender <- activate <- "org.rstudio.RStudio"
}