Skip to content

Instantly share code, notes, and snippets.

View webbedfeet's full-sized avatar

Abhijit Dasgupta webbedfeet

View GitHub Profile
This file has been truncated, but you can view the full file.
{
"metadata": {
"name": "demo"
},
"nbformat": 3,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
@webbedfeet
webbedfeet / gist:5352113
Last active December 16, 2015 00:59
pandas DataFrame to R data.frame conversion
from collections import OrderedDict
import numpy as np
py2ri_orig = rpy2.robjects.conversion.py2ri
def conversion_pydataframe(obj):
if isinstance(obj, pandas.core.frame.DataFrame):
od = OrderedDict()
for name, values in obj.iteritems():
if values.dtype.kind == 'O':
od[name] = rpy2.robjects.vectors.StrVector(values)
else:
@webbedfeet
webbedfeet / lcts.R
Created May 17, 2013 02:12
Ensure left continuity of a function
# Ensure left continuity of a decreasing step function
# First find discontinuities, based on minimum jump size, then make sure there
# is a data point to ensure left continuity there.
lcts <- function(dat, min.jump){
for(i in 2:nrow(dat)){
if(dat[i+1,2] - dat[i,2] > min.jump){ # identify discontinuity
dat <- rbind(dat, c(dat[i+1,1], dat[i,2])) # generate left-cts data point
@webbedfeet
webbedfeet / index.html
Created June 12, 2013 15:56
Scatterplot
<!doctype HTML>
<html>
<head>
<script src='http://polychart.com/s/third_party/polychart2.standalone.js' type='text/javascript'></script>
<style>
.rChart {
display: block;
margin-left: auto;
@webbedfeet
webbedfeet / index.html
Created June 21, 2013 02:55
Scatterplot
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<script src='http://polychart.com/s/third_party/polychart2.standalone.js' type='text/javascript'></script>
<style>
.rChart {
display: block;
package mostDownloads bestRank week tot rank
shiny 1018 70 2013-06-10 1018 70
RColorBrewer 4376 4 2013-04-29 3408 4
scales 3976 6 2012-12-10 1571 6
scales 3976 6 2012-12-03 1720 6
reshape2 4330 6 2012-12-17 1312 6
reshape2 4330 6 2013-01-07 2199 6
reshape2 4330 6 2013-05-13 3660 6
proto 3902 3 2012-12-24 963 3
ggplot2 5291 1 2013-03-25 3160 1
xlsxToR <- function(file) {
require(XML)
require(plyr)
suppressWarnings(file.remove(tempdir()))
file.copy(file, tempdir())
new_file <- list.files(tempdir(), full.name = TRUE, pattern = basename(file))
new_file_rename <- gsub("xlsx$", "zip", new_file)
file.rename(new_file, new_file_rename)
#'% How to format plots for publication using `ggplot2` (with some help from Inkscape)
#'% Rosemary Hartman
#'% 13-11-20 19:51:47
#'
#' ***The following is the code from a presentation made by Rosemary Hartman to
#' the [Davis R Users' Group](http://www.noamross.net/davis-r-users-group.html).
#' I've run the code through the `spin` function in `knitr` to produce this post.
#' Download the script to walk through [here](https://gist.github.com/noamross/7576436)***
#'
#' First, make your plot. I am going to use the data already in R
{
"scope": "source.r",
"completions":
[
"abline",
"abs",
"anova",
"anova.glm",
"anova.lm",

This is a short demo of how to use brew and knitr in combination with each other to get the best of the templating facilities in brew and the literate programming functions in knitr. The main idea is to write a function brew_knit

# Preprocess template using brew and then run knit on the output
brew_knit <- function(template, params, ...){
 brew::brew(template, envir = list2env(params))
 input = gsub(".Rnwe", '.Rnw', template)
 knitr::knit(input)
}