Tuesday 29 December 2009

The best R tricks

This time, the post has been written by others. The community has chosen the best R tricks and made a poll whose results can be browsed at

http://stackoverflow.com/questions/1295955/what-is-the-most-useful-r-trick

Friday 18 December 2009

Running BOINC on a Centos 5.4 server through command line

I wanted to install BOINC on a Centos 5.4 server with no GUI. I could not find BOINC on any Centos RPM repositories and the newest Linux version did not seem to work.

So I went to the BOINC download page and chose the older 6.6.41 version for x86 Linux with CLI only, and downloaded it with wget to my server. 

The downloaded file was a Bash executable script that expanded a whole directory with a number of programs inside, among which, boinc and boinccmd.

To have BOINC running, you need just do

nohup boinc &

However, the process is not attached to any BOINC project. I wanted to attach to Ibercivis, so I used the following command:

boinccmd --project_attach http://registro.ibercivis.es d44xxxxxxxxxxxx7c3

Of course, I have masked my authorization string, d44xxxxxxxxxxxx7c3. I got it from the file account_registro.ibercivis.es.xml in another installation I had on another machine.

In a matter of seconds, the command 

boinccmd --get_state

indicated that BOINC was already running on my machine. The boinccmd command also allows you to configure plenty of other parameters, but that is way another topic.

Connecting R and Postgres via JDBC

I need to make a note of this for reference:

library( RJDBC ) 
postgres <- JDBC( "org.postgresql.Driver", "/path/to/postgresql-8.3-604.jdbc4.jar")
con <- dbConnect(postgres, "jdbc:postgresql://localhost:5432/mydbname", user = "myusername", password = "xxx" )

From here on, I can read

my.r.df <- dbReadTable( con, "myschema.mytablename" )

and save

dbWriteTable( con, "myschema.mytablename", my.r.df )

Thursday 17 December 2009

Left padding of string field in Kettle

I had to left pad a field using Kettle. It was a postal code mistakenly coded as a double in an Access database and it had to be transformed. In my country, postal codes consist of 5 digits, the first of which could be "0".

Mapping the field to "string" was not good enough: blanks were used for padding. In order to use "0", I had to use the JavaScript node in Kettle. The following graphic contains my little code snippet:

For some reason, the code did not "validate" in the node (there is a "validation" button), but the data flow worked as a charm.

Sunday 29 November 2009

Attach a projector to Xubuntu with Nvidia cards

The other day I faced a problem just before my talk in the first meeting of R users in Spain: I could not project to the big screen. The usual Settings > Display menu was of no help.

What you need to do is to run

sudo nvidia-settings

in your terminal and then go to X Server Display Configuration.

There, after pluging in the video cable for the projector, you should click on the Detect Displays button for the video card to detect the new screen. The Configuration: Separate X Screen button allows you to decide where to project your X, locally, on the projector or in both displays.

Tuesday 17 November 2009

Restrict ssh access to Linux server to members of a group

You may want to restrict ssh access to a server to members of a group. I faced this problem when I only wanted members in the "consultants" group to be able to log into our CentOS server.

In order to achieve this, you need only edit the file sshd_config (in CentOS, it is located in /etc/ssh, but this may be distribution-dependent) and add the following line to it:

AllowGroups consultants

Mind that this line, contrary to intuition, does not grant members of the "consultants" group the right to log in, but restricts this right to any other member in any other group. So, perhaps you may want to use

AllowGroups consultants root

instead so as to allow root logins (just in case).

Sunday 18 October 2009

Notepad-compatibility for copy and paste in vim and gvim

Unfortunately, even us, Linux die-hards, get used to other OS conventions, such as copy-pasting with Control-C and Control-V. Maybe because in some other OS, by design, you need to spend hours copying and pasting.

In any case, if you want your Vim and GVim on your Linux box responding to Control-C and Control-V for these operations you need to map these key combinations to their internal counterparts, "+y and "+gP respectively.

In order to achieve this, you need just go to your home directory and add to the .vimrc file there the following three lines:

nmap <C-V> "+gP
imap <C-V> <ESC><C-V>i
vmap <C-C> "+y

A final piece of advice: use it with care! Copy/paste operations do not increase the amount of information but increase its size.

Friday 2 October 2009

Five levels of purpose

I copy from another source because I want to keep them handy. Here are the five levels of purpose:

  1. Check present position
  2. Set goals
  3. Affirm priorities
  4. Forecast future position
  5. Create change

Too many open files in Alfresco

Recently, my Alfresco installation on Centos 5.3 started to behave randomly and some functionalities became suddenly unavailable. A quick view in the error logs showed a pattern, a string of "too many open file" complaints.

The reason is that there is a limit on the maximum number of open files a user may keep open. The user running Alfresco had just 1024 of them, too few, it seems, for its normal operations. You can check the number of open files allowed per user with the command

cat /proc/sys/fs/file-max

Increasing this value is simple: first, edit the file /etc/sysctl.conf adding a row such as

fs.file-max = 4096

Then, run 

sysctl -p

to make the changes in the file available. Then, finally, you will need to restart your Alfresco installation.


Sunday 9 August 2009

Toggling keyboards in Xubuntu

I recently switched to Xubuntu from Ubuntu. I wanted to retain the ability to switch from ES to US keyboard by pressing my right control key. For this purpose, I created a new file at /usr/bin called togglekeyboard. The file contains:

#!/bin/bash
setxkbmap -option grp:switch,grp:rctrl_toggle us,es

us and es stand for US and Spanish keyboards. You will better know the id of your desired keyboard flavour.

Then, rctrl_toggle asks setxkbmap to toggle the keyboard on the right-control-key-pressed event. I chose this key as I am not using it for anything else. I got used to press the left control key for all properly controlish purposes. Other options that may suit you are:

  • toggle – right alt key
  • ctrl_shift_toggle – ctrl+shift
  • alt_shift_toggle – alt+shift
  • ctrl_alt_toggle – ctrl+alt
  • caps_toggle – CapsLock
  • lctrl_toggle – left control key
  • switch – switch while the right alt is pressed
  • lwin_switch – switch while the left Windows key is pressed
  • rwin_switch – switch while the right Windows key is pressed
  • win_switch – switch while the any Windows key is pressed

Thursday 6 August 2009

Restricting access to PostgreSQL databases to users

I have created some postgres databases and I want given users to have access only to them (and no others) from any network. For that purpose, in the pg_hba.conf configuration file, under the IPv4 local connections header, I have created the following entries:

host all postgres 0.0.0.0/0 password
host all cjgb 0.0.0.0/0 password

These are the entries that provide unrestricted access to myself (impersonated by either the postgres or the cjgb user) as administrator of the database to connect to "all" databases from "0.0.0.0/0", this is, any host, provided I use the right password.

Access for the other users is restricted as follows:

host varios varios 0.0.0.0/0 password
host postgres varios 0.0.0.0/0 password
host riesgo_desviacion riesgo_desviacion 0.0.0.0/0 password
host postgres riesgo_desviacion 0.0.0.0/0 password

Users varios and riesgo_desviacion can only access tables varios and riesgo_desviacion (respectively) from any network if they provide the right password. I also had to grant them access to the postgres database (for reasons I still have to investigate, but that I am afraid stem from the fact that I mistakenly used the postgres database as template to create the new ones).

Monday 27 July 2009

Write Matlab files without Matlab via Java and JMatIO

I had to interface a shiny new analytical suite built on KNIME with some legacy applications in Matlab we are unfortunately still using. This demanded exporting tables into Matlab's proprietary data format from Java.

I used the JMatIO library and everything worked like a charm. The following code snippet (taken from here) best illustrates the use of the library:

//1. First create example arrays
double[] src = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
MLDouble mlDouble = new MLDouble( "double_arr", src, 3 );
MLChar mlChar = new MLChar( "char_arr", "I am dummy" );

//2. write arrays to file
ArrayList list = new ArrayList();
list.add( mlDouble );
list.add( mlChar );

new MatFileWriter( "mat_file.mat", list );

This is equivalent to the following Matlab code:

double_arr = [ 1 2; 3 4; 5 6];
char_arr = 'I am dummy';
save('mat_file.mat', 'double_arr', 'char_arr');

Neat, isn't it?

Friday 24 July 2009

Adding external Java libraries to new KNIME plugins

Sorry, it is a blunt copy-paste plagiarism, but I needed to keep it written somewhere. Here it goes:

In order to add external jars to new KNIME plugins you need to: 

  1. Create a lib directory in your KNIME plugin.
  2. Copy the file(s) into the lib directory. (Java libraries are packed either as a zip or jar archive.)
  3. Edit the META-INF/MANIFEST.MF file with the "Plug-in Manifest Editor".
  4. Go to the "Runtime" tab and add all necessary libraries to the "Classpath" list on the bottom-right corner using the "Add..." button.
  5. Go the to "Build" tab and add the files to the list contained in the section "Extra classpath entries".
  6. Make sure that the lib directory is selected in both the "Binary Build" and "Source Build" list (in the same tab).

Finally note that adding jar files to the plugins build path, i.e. project context menu -> "Java Build Path" ->"Libraries" is not necessary.

Saturday 18 July 2009

Dealing with filenames with spaces in bash scripts

My war against spaces in filenames is lost. Even if I would never dare type one, my hardrive keeps receiving them from computer illiterate colleagues.

Admittedly, they are not a big pain until I want to make backups. My backup script contained a 

for file in `find . -type f`
do
...
done

kind of script. But whenever the found filename contained an space, my variable $file contained part of the filename and a huge mess unleashed.

Here is, however,  a neat solution to my troubles:

find . -type f -name '*' | while read file
do
...   
done

Friday 17 July 2009

Problems with Alfresco Share running on non-standard ports

There is almost no web application which does not want to listen on port 8080. Alfresco is one of them. But, sorry, on my machine the port is already taken!

So I had to move it to an anagram (I am running short of them!) and edited the file

$ALFRESCO_HOME/tomcat/conf/server.xml

replacing the string 8080 by 8800.

However, Alfresco Share refused to start. After trying to log in, I got a "Failed to Login" error followed by "The remote server may be unavailable or your authentication details have not been recognized". But my user/password was correct...

There was another obscure quirk to update in order that the port migration were effective: replacing every occurrence of the old port number in file

$ALFRESCO_HOME/tomcat/webapps/share/WEB-INF/classes/alfresco/webscript-framework-config.xml

After that, Alfresco Share was fully functional.