Skip to content

Instantly share code, notes, and snippets.

View trcook's full-sized avatar

Tom Cook trcook

View GitHub Profile
@trcook
trcook / rakefile.rb
Last active August 29, 2015 14:09
This is an easy rakefile to get rmd files in a directory, parse them through knitr and get them ready for processing by pandoc or something else as markdown.
### THE SCRIPT TO RUN -- MAKE SURE IT'S NOT INDENTED -- script goes bretween EOS's @input_file and @output_file are replaced with input and output files.
task :default=>"brownbag:install"
desc %{builds slides.md for use by reval}
task :reveal=>"brownbag:reveal"
namespace :brownbag do
###
SCRIPT_TO_RUN =<<EOS
Rscript -e 'require(knitr);options("revealout"="n");render_markdown();knit("@input_file", output="@output_file")'
@trcook
trcook / python extract regex matches in file
Created August 18, 2014 02:36
this will change directories as directed, open a file, match by a regex and then print out the results ot a file as directed
#! #!/usr/bin/env python3
import io, os, re
os.chdir('/Users/tom/Desktop/tmp')
e=open('questiondb.xml','rt')
search_string=e.read()
e.close()
outfile=open("out.txt",'w')
matches=re.findall(r"(<item d2l_2p0:id=.*\n(.+?\n(?!</item))+?.*?questiontype(.*?\n)+?.*Multiple Choice\n(.*\n)+?.*/item>.*?\n)",search_string)
@trcook
trcook / Dates
Last active August 29, 2015 14:01
This is a quick and easy script that will generate a list of dates for each workday over an interval. I use it to generate a list of dates for classes in a semester and keep track of the number of weeks in the semester
#!/usr/bin/env ruby
require 'date'
dates=Array.new()
c = Date.new(2014,07,04)
b= Date.new(2014,06,02)
max_weeks= (c-b)/7.to_f
max_weeks=max_weeks.ceil
course_day=0
(c-b).to_int.times do |t|
@trcook
trcook / rakefile.rb
Last active August 29, 2015 14:01
push knitr output to jekyll, including images
require "rubygems"
require "bundler/setup"
require "stringex"
## REQUIRED OPTIONS
octopress_home = '~/Desktop/jb' # Path go jekyll site goes here. This shoudl work eqally well on octopress as on jb or vanilla jekyll
my_rmd_files=FileList[Dir.glob("./**/*.Rmd", File::FNM_CASEFOLD)]
# This assumes the rakefile is in the base of the directory where knitr docs are
@trcook
trcook / gist:5502688
Created May 2, 2013 14:43
setting up a notification for my phone in R
# First, you need to install the boxcar app,
# then subscribe to the generic feed by running this in your 'os x' terminal
curl -d "[email protected]" http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications/subscribe
# then, in the boxcar app, you need to confirm that you want to get notifications from the generic monitoring provider (it will ask in a popup)
# now, you can put the following commands in your R script and it will send you a notification when that command runs (this is all one line of code):
@trcook
trcook / Data_generation_schemes.R
Created April 10, 2013 19:35
Types of Data Generation
#-------------------------------------------------------------------------------
# #generate periodic data:
#-------------------------------------------------------------------------------
#{{{
#set number of data points to generate
n<-500
x<-runif(n,1,5)
# coefficient
# next line is the key for the DGP of Y:
@trcook
trcook / get-R-args.rb
Created April 3, 2013 14:21
Ruby get R Args
#!/usr/bin/env ruby
require 'rserve'
import rserve
require 'rserve'
include Rserve
c = Connection.new # from here out, all calls to R will occur in the method: "c.eval()"
out_args = c.eval %{
@trcook
trcook / Asset.rb
Created April 3, 2013 13:58
read a file into a string
my_table=File.open("this.txt","r")
@trcook
trcook / Asset.R
Last active December 15, 2015 18:09
Graphics export R
# Save file as eps
setEPS()
postscript(file="testplot.eps",
)
## PLOT COMMAND GOES HERE: FOR EXAMPLE:
plot(hist(prestige,freq=FALSE),main=NULL,ylab="Density",xlab="Occupational Prestige")
dev.off()
@trcook
trcook / dummy_factor.R
Last active December 15, 2015 18:09
dummy a factor variable in R
#assume data.frame(dat)
data.frame( model.matrix(~lp_legor-1,data=dat))->tmp
names(dat)->tmpnames1
names(tmp)->tmpnames2
data.frame(dat,tmp)->dat
names(dat)<-c(tmpnames1,tmpnames2)