Saturday, February 11, 2012

How to redirect output to screen and a file

With the command tee (in Linux) or the command wtee (in Windows)

Example (in Linux)

ls | tee output.txt

Example (in Windows)

dir | wtee > output.txt

Wednesday, February 8, 2012

RMySQL - How to connect R with MySQL in Windows

Following the instructions of

Important note 1: The installation works when using MySQL for 32bit computers and R for 32bit computers. I have tried to install it to the 64bit version of R but I did not succeed.
  • Install RTools
  • Install MySQL or header and library files of mysql
  • Create or edit file C:\Program Files\R\R-2.12.1\etc\ and add two lines like CYGWIN=nodosfilewarning
  • Copy libmysql.lib from mysql/lib to mysql/lib/opt to meet dependencies.
  • Copy libmysql.dll to C:\Program Files\R\R-2.12.1\bin or to windows/system32 directory.
  • Run install.packages('RMySQL',type='source') and wait while compilation will end.
Important note 2: My computer contains Spanish language Windows, and I had several problems with the accent in the folder named "Configuración local", where the downloaded packages are stored and compiled.
I solved the problem in the following way. I changed the environment variable TMP to another local folder without containing any accent.
After the installation I rolled back the change in the environment variable.

Sunday, February 5, 2012

MySQL - Get row number on select

SET @rank=0;
SELECT @rank:=@rank+1 AS rank, itemID, COUNT(*) as ordercount
FROM orders
ORDER BY ordercount DESC;