Tuesday, March 20, 2012

informative discussion on variable scope control in R

http://stackoverflow.com/questions/6216968/r-force-local-scope
This somehow magically works, but I need to comprehend.
environment(fun) = parent.env(environment(fun))

Saturday, March 10, 2012

my experience with installing Rgraphviz on Ubuntu (10.10)

make sure libgraphviz-dev is installed. It is needed for some header files (e.g. gvc.h)
then
biocLite("Rgraphviz", configure.args=c("--with-graphviz=/usr"))
the reason is that at least on my computer the dot program was in /usr/bin, but not in /usr/local/bin as Rgraphviz defaults

Tuesday, December 13, 2011

Accessing SQL Server DB from R on Linux using RODBC package

First of all you'll need to install FreeTDS package.

brew update
brew install unixodbc
brew install freetds
It looks like there is no need to specify --with-unixodbc any more.


I would add only that I had to edit not
/etc/odbcinst.ini
but
/usr/local/etc/odbcinst.ini,

On Linux it looks like this
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
Threading = 1
CPTimeout =
CPReuse = 
FileUsage = 1

On Mac paths are a bit different
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsS.so
Threading = 1
CPTimeout =
CPReuse = 
FileUsage = 1


then it worked just fine.
Here is a sample code:
library(RODBC)
# replace server.name, database.name, user.id, password and T_my_table correspondingly
con <- odbcDriverConnect("DRIVER={FreeTDS};SERVER=server.name;DATABASE=database.name;UID=user.id;PWD=password;")
strSql <- "select * from T_my_table"
x <- sqlQuery(con, strSql)
close(con)
cat(x)

Friday, September 23, 2011

Grouping data.frame by column name pattern in R

This is kind of brutal force solution. First you transpose the data.frame Then use aggregate function Finally transpose back. An example...

x = data.frame(a1=rnorm(10), a2=rnorm(10), b1=rnorm(10), b2=rnorm(10))
g=sapply(colnames(x), function(xx) {strsplit(xx,split='')[[1]][1]})
y = t(aggregate(t(x),by=list(g),mean, simplify=T))
yy = data.frame(y[-1,])
colnames(yy) = y[1,]
rownames(yy) = rownames(x)

Thursday, September 22, 2011

sorting in python by arbitrary key

E.g. we have a list of dictionaries

x = [{'a':1,'b':3}, {'a':3,'b':2}, {'a':2,'b':1}]
x.sort(key=lambda i: i['b'])

sorts it by value at the 'b' key

How to use grep to search within a particular file type

Example finding sort in python files. 3 options:
grep -r --include=*.py "sort" /home/vlad/work
find . -name "*.py" -print0 | xargs -0 grep "sort"
findstr /pinsc:"sort" *.py

Tuesday, September 6, 2011

setting up Notepad++ on Win7

In case I forget where the settings are: C:\Users\MY_NAME\AppData\Roaming\Notepad++ Running IPython ipython qtconsole --pylab=inline --ConsoleWidget.font_family="Courier New" --ConsoleWidget.font_size=10 --c="run $(FULL_CURRENT_PATH)"

Checking font availability on Mac   fc-list : family | grep "Fira Code"   or   system_profiler -json SPFontsDataType | grep \...