Skip to content

Instantly share code, notes, and snippets.

#' An R function for creating simple D3 javascript directed network graphs.
#'
#' d3SimpleNetwork creates simple D3 javascript network graphs.
#'
#' @param data a data frame object with three columns. The first two are the names of the linked units. The third records an edge value. (Currently the third column doesn't affect the graph.)
#' @param Source character string naming the network source variable in the data frame. If \code{Source = NULL} then the first column of the data frame is treated as the source.
#' @param Target character string naming the network target variable in the data frame. If \code{Target = NULL} then the second column of the data frame is treated as the target.
#' @param height numeric height for the network graph's frame area.
#' @param width numeric width for the network graph's frame area.
#' @param file a character string of the file name to save the resulting graph. If a file name is given a standalone webpage is created, i.e. with a header and footer. If \code{file = NULL} then
@benmarwick
benmarwick / JSTORr-shiny.R
Created July 11, 2013 06:17
basic sketch of a locally-hosted web app using shiney and JSTORr. Depends on having run a few functions to prepare the data in advance...
# to make it work...
setwd("C:\\Users\\marwick\\Downloads\\shiny")
# make sure the 'shiny_data.RData' object is in there
# this is the just the result of unpack1grams() for the
# three journals
# then load the package
library(shiny)
# this line fires it up in a browser
runApp()
#get data from google sheet
# connect to google sheet
require(RCurl)
options(RCurlOptions = list(capath = system.file("CurlSSL", "cacert.pem", package = "RCurl"), ssl.verifypeer = FALSE))
#in google spreadsheet, go to file-> publish to web -> get link to publish to web -> get csv file
goog <- "https://docs.google.com/spreadsheet/pub?key=0As7CmPqGXTzldFRsVi1VZ2EyNXJ1ZEV5SG5GSExwRHc&single=true&gid=5&output=csv"
data <- read.csv(textConnection(getURL(goog)), stringsAsFactors = FALSE)
# extract just data for plotting: pH, SOM, CaCO3, MS-LF, MS-FD
plotting_data <- na.omit(data[,c('Sample.number',
rm(list = ls()) # clear workspace
# see https://gist.github.com/benmarwick/6081694 for data file to use
# with this code
library(phangorn) # load library phangorn 'Phylogenetic analysis in R'
# change the following line to match your computer
setwd("E:\\My Documents\\My Papers\\EurASEAA2010")
b <- read.nexus.data("infileb12.txt.nex") #make sure this nex file is in wd
b.pd <- phyDat(b, type="USER", levels=c(0:2)) # convert nex to phyDat format
b.pd #check what went in....
b.pd.pscore <- pratchet(b.pd, method="fitch")
Phylogenetic Analysis of Material Culture in R
#
# preliminaries...
# get R: http://www.r-project.org/
# get RStudio: http://www.rstudio.org/
#
# useful websites for ideas and instructions...
# http://cran.r-project.org/web/views/Phylogenetics.html annotated list of relevant R packages
# http://www.r-phylo.org/wiki/Main_Page
@benmarwick
benmarwick / scrape-blog-text.r
Last active January 24, 2017 12:03
R code to scrape text and dates of publication from blog posts on a single wordpress blog. Downloads blog pages, extracts URLs for full text of blog posts, downloaded full text from full post, and date at post. Specifically, from http://www.dayofarchaeology.com/
# R code to scrape text and dates of publication from blog
# posts on a single wordpress blog. Downloads blog pages,
# extracts URLs for full text of blog posts, downloaded
# full text from full post, and date at post.
# Specifically, from http://www.dayofarchaeology.com/
library(RCurl)
library(XML)
# get URLs to blog post text for all post
@benmarwick
benmarwick / colocates.r
Last active October 24, 2017 10:18
Analysis of collocation of words in a text with R. Extracts LHS and RHS collocates of a word of interest over a user-defined span. Calculates frequency of collocates and mean distances. Inspired by http://www.antlab.sci.waseda.ac.jp/software.html
# R code for basic collocation statistics on a text corpus.
# Extracts LHS and RHS collocates of a word of interest
# over a user-defined span. Calculates frequency of
# collocates and mean distances.
examp1 <- "When discussing performance with colleagues, teaching, sending a bug report or
searching for guidance on mailing lists and here on SO, a reproducible example is often
asked and always helpful. What are your tips for creating an excellent example? How do
you paste data structures from r in a text format? What other information should you
include? Are there other tricks in addition to using dput(), dump() or structure()?
# load required libraries
library(tm)
library(ggplot2)
library(lsa)
# 1. Prepare mock data
text <- c("transporting food by cars will cause global warming. so we should go local.",
"we should try to convince our parents to stop using cars because it will cause global warming.",
"some food, such as mongo, requires a warm weather to grow. so they have to be transported to canada.",
"a typical electronic circuit can be built with a battery, a bulb, and a switch.",
@benmarwick
benmarwick / termite-test.txt
Last active December 20, 2015 16:39
A quick test of the termite visualisation for topic models (https://github.com/StanfordHCI/termite) Using Windows 7 and cygwin
A quick test of the termite visualisation for topic models
Using Windows 7 and cygwin
https://github.com/StanfordHCI/termite
# prepare text for input, using R (pasted below)
# make txt file with format where 1 line = 1 doc
# each line has ID then tab then text
library(tm)
data(crude)
sink("example-documents.txt")
# Better alternatives to 3-D
# Here are some examples of what we don't want to do:
# http://stackoverflow.com/questions/12765497/create-a-ribbon-plot
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Air passenger data. ts converted to long matrix: