Skip to content

Instantly share code, notes, and snippets.

View codegordi's full-sized avatar

C M Gutierrez codegordi

View GitHub Profile
@codegordi
codegordi / gist:1e93b509b4629bb3adba
Created January 20, 2015 19:38
Connect to Postgres using R on MacOS Mavericks + later
# install_postgres.notes
# Notes on install RPostgreSQL on MAC OS X Mavericks & later
# original source:
# http://computersandbuildings.com/how-to-install-rpostgresql-on-osx-mavericks/:
1) install.packages('devtools') (if don't already have them)
2) install.packages('DBI')
3) download source RPostgreSQL tarball from:
http://cran.r-project.org/web/packages/RPostgreSQL/index.html
# Install Bash 4 using homebrew
brew install bash
# Or build it from source...
curl -O http://ftp.gnu.org/gnu/bash/bash-4.2.tar.gz
tar xzf bash-4.2.tar.gz
cd bash-4.2
./configure --prefix=/usr/local/bin && make && sudo make install
# Add the new shell to the list of legit shells
## install Haskell Platform on Mac OS
(1) Download from https://www.haskell.org/platform/
> Contains the GHCi REPL shell (console-interpreter), common vetted libraries, Cabal, etc.
> See https://www.haskell.org/platform/doc/2014.2.0.0/start.html for more information
(2) Um ... that should be it on Mavericks (v. 10.9.+). You may run into problems if you had GHC/i installed already on your Mac and then upgraded to Mavericks. Look up @cartazio or @katchuang gists in these cases.
(3) Do you a Haskell for a Great Good!
# alteRyx_install_packages.R
# > code to install packages via R Developer tool in Alteryx(R) module
# > note call to custom Alteryx-R function wrte.Alteryx()
altx.repo <- getOption("repos")
altx.repo["CRAN"] <- "http://cran.rstudio.com" # set your primary repo if you haven't already
options(repos = altx.repo)
#write.Alteryx(getOption("repos"), 1) # DEBUG
install.packages("XML")
@codegordi
codegordi / filegdb2shp
Last active August 29, 2015 14:03 — forked from gislars/filegdb2shp
1) Get the FileGDB API http://www.esri.com/apps/products/download/
2) Extract it somewhere on your system and remember the path :)
3) Do:
> mkdir build #directory where we are playing around
> cd build
> git clone https://github.com/OSGeo/gdal.git
> cd gdal
> ./configure --with-fgdb=/path/to/your/FileGDB_API
@codegordi
codegordi / cumRelFreqDistn.py
Created October 21, 2013 20:50
Python function to calculate cumulative relative frequency distribution (for contexts where numpy/scipy/etc not available, e.g. in Pig pre-v.0.12). Originally designed to work as a User Defined Function for Pig on Hadoop.
def cumRelFreqDistn(tups):
# create bins of increment 0.01
a = [i*-0.01 for i in range(100)]
a = a[1:len(a)]
b = [i*0.01 for i in range(101)]
a.extend(b)
a.sort()
bins = a
@codegordi
codegordi / Rneo4j
Created June 4, 2013 13:16
Connect to neo4j and send Cypher query (R on MacOS)
library('bitops')
library('RCurl')
library('RJSONIO')
query = function(querystring) {
h = basicTextGatherer()
curlPerform(url="http://<your host IP>/db/data/ext/CypherPlugin/graphdb/execute_query",
postfields=paste('query',curlEscape(querystring), sep='='),
writefunction = h$update,
verbose = FALSE
@codegordi
codegordi / R_multicore_example
Last active December 17, 2015 23:08
Use multicore package (R on MacOS) to grep a (Very Large Data) file-as-dataframe.
### manage memory on a large data set using multicore library
library(multicore)
# read in tab-delimited file from working dir
df = read.table(getwd(), sep="\t", header=T)
d.lines = as.character(df$charvar) # $charvar is character-class variable you want to grep
grep_wrap <- function (pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE, invert = FALSE) {
ret = rep(0, length(x))
ret[grep(pattern, x, ignore.case, perl, value, fixed, useBytes, invert)] = 1
@codegordi
codegordi / RJDBC_example
Created May 31, 2013 18:07
Example using RJDBC to connect to a MS SQL Server database (R on MacOS)
# JDBC driver library for R
library("RJDBC")
user <- "cgutierrez" # enter your own username
#pwd <- # enter in URL str below
# I chose open-source JTDS for a particular reason in this example (not related to R); you might choose another one
# Check that appropriate .jar is in indicated directory
drv <- JDBC("net.sourceforge.jtds.jdbc.Driver", "/usr/share/java/drivers/jdbc/jtds-1.2.7.jar", identifier.quote="`")
cxn <- dbConnect(drv, "jdbc:jtds:sqlserver://MYSERVER01ABC;useNTLMv2=true;domain=MY.DOMAIN.COM", user, #####) # enter pwd at runtime
@codegordi
codegordi / write.to.read.from.LIST
Created May 31, 2013 17:51
Read to / write from list - from / to flat file
# use plyr package
library(plyr)
### READ all files in working directory into a list of dataframes
d.list=lapply(list.files(getwd(), full = F), FUN = read.table, header=T, sep="\t", fill=T) # name data
### SEPARATE out data frames from list -- using a for-loop in this example
i=0
f.coll=list.files()