Skip to content

Instantly share code, notes, and snippets.

@mplourde
mplourde / get current script directory in python
Created November 4, 2014 18:30
get current script directory in python
import os
import sys
def script_dir():
if '__file__' in globals():
return os.path.abspath(os.path.dirname(__file__))
elif sys.argv[0]:
return os.path.abspath(os.path.dirname(sys.argv[0]))
else:
os.path.abspath(os.curdir)
@mplourde
mplourde / git commit steps
Created October 1, 2014 21:26
git commit steps
# after making a series of changes to a file or files, open git bash and use the following commands to commit and sync
# if you've added files to the repository, call 'git add' to add them to the repo
git add /path/to/file
# to commit all your changes, open git bash and change directory to the project folder. Use "pushd //28-arajohnsmit/myproject"
# to change to a directory on someone else's machine. Then enter the following command
git commit -a
# this will open a vim session for you to enter a message about your changes.
# In vim hit the 'i' key to go into insert mode. Type a brief explanation of your changes, then hit esc
@mplourde
mplourde / git setup
Created October 1, 2014 20:58
git setup
git config --global http.proxy http://webproxy.phila.gov:8080
git config --global user.name "Firstname Lastname"
git config --global user.email "[email protected]"
git config --global github.user mplourde
git config --global github.token 2332424your3232442
## R
# install possible missing dependencies for R packages
apt-get -y install libcurl4-gnutls-dev
apt-get -y install libcairo2-dev
apt-get -y install libgdal1-dev libproj-dev
apt-get -y install libxt-dev
apt-get -y install libiodbc2 libiodbc2-dev
apt-get -y install libgdal-dev libproj-dev
# then install these packages
shiny # devtools::install_github('shiny', 'rstudio')
options(shiny.reactlog=TRUE, warn=2, show.error.locations=TRUE, error=recover)
# interactive session
browser()
# print message
message('message')
# error handling
err.hanlder <- function(e) message(e)
@mplourde
mplourde / shiny url variables
Created August 14, 2014 14:13
controlling shiny app page with URL variables
ui <- navbarPage('TEST', id='page', collapsable=TRUE, inverse=FALSE,
# define a message handler that will receive the variables on the client side
# from the server and update the page accordingly.
tags$head(tags$script("
Shiny.addCustomMessageHandler('updateSelections',
function(data) {
var nav_ref = '#page a:contains(\"' + data.nav + '\")';
var tabpanel_id = data.nav == 'Alpha' ? '#alpha_tabs' : '#beta_tabs';
var tab_ref = tabpanel_id + ' a:contains(\"' + data.tab + '\")';
$(nav_ref).tab('show');
@mplourde
mplourde / plotly snippets
Created August 1, 2014 15:20
connecting R to plotly
options(RCurlOptions=list(proxy='web.proxy.com:8080'))
py <- plotly("me", "mec3g0e1z7")
p <- ggplot(mtcars, aes(x=mpg, y=cyl)) + geom_point()
res <- py$ggplotly(k, kwargs=list(auto_open=FALSE))
@mplourde
mplourde / gist:153f021018e361315916
Last active August 29, 2015 14:02
building a data.frame column wise
num.cols <- 10
# allocate space for your ten columns
cols <- vector('list', num.cols)
# add key column, such as timestamp
cols[[1]] <- 1:5
for (i in 2:num.cols) {
cols[[i]] <- rnorm(5) # add the next column, 5 random numbers
@mplourde
mplourde / gist:05af18f121fa416d2f38
Created June 10, 2014 18:20
multiple lines in ggplot
library(ggplot2)
library(reshape2) # gives you the melt function
d1 <- data.frame(time=1:10, x1=1:10)
d2 <- data.frame(time=1:10, x2=1:10/2)
d <- merge(d1, d2, by='time')
d.molten <- melt(d, id.var='time')
@mplourde
mplourde / gist:a75ea54db4f7209b58b9
Created June 3, 2014 13:33
using the R layout command
layout.mat <- matrix(c(1,1,1,1,1,1,2,3,3), nrow=3)
layout.mat
layout(layout.mat)
plot(1:10)
plot(1:10)