Correctly Uninstall PostgreSQL

Uninstalling PostgreSQL may leave some undesired files. Here’s how to purge it:

First, uninstall all packages from PostgreSQL from the system using

sudo apt-get --purge remove postgresql\*

Then remove all the configuration and library stuff

sudo rm -r /etc/postgresql/
sudo rm -r /etc/postgresql-common/
sudo rm -r /var/lib/postgresql/

And finally, remove the user and group

sudo userdel -r postgres
sudo groupdel postgres

Upgrade PostgreSQL from 9.1 to 9.3 on Kubuntu

This seven steps will perform the upgrade of PostgreSQL from version 9.1 to version 9.3.
This also works in Ubuntu and it can also be used to upgrade between any version numbers.

To upgrade between any versions, just changed the 9.1 for the legacy version number and the 9.3 for the new version number.

First install the necessary dependencies

sudo apt-get update
sudo apt-get -y install python-software-properties

Second, add the PostgreSQL repository

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Third, setup the repository

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" >> /etc/apt/sources.list.d/postgresql.list'

Fourth, install PostgreSQL 9.3.

sudo apt-get install postgresql-9.3 postgresql-server-dev-9.3 postgresql-contrib-9.3

Fifth, perform the upgrade process.
The upgrade process is performed by having both servers running at the same time. Note that the new 9.3 version will run on a different port, 5433 as specified in the script bellow, and it will be set to the default port latter when the legacy version is uninstalled and the default port becomes available.

sudo su -l postgres
psql -d template1 -p 5433
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
\q #logout from database
service postgresql stop
/usr/lib/postgresql/9.3/bin/pg_upgrade -b /usr/lib/postgresql/9.1/bin -B /usr/lib/postgresql/9.3/bin -d /var/lib/postgresql/9.1/main/ -D /var/lib/postgresql/9.3/main/ -O " -c config_file=/etc/postgresql/9.3/main/postgresql.conf" -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf"
exit # logout postgresql back to previous user

Sixth, remove the 9.1 version.

sudo apt-get remove postgresql-9.1

Seventh, set the new version server port back to the default value and restart the service.

sudo vim /etc/postgresql/9.3/main/postgresql.conf # find old port of 5433 and change it to 5432
sudo service postgresql restart

Change Keyboard Layout on Ubuntu Server Permanently

When working on Ubuntu Servers, sometimes the defined keyboard layout is not aligned with the physical keyboard one’s using.

In order to permanently change it, just execute the following two commands.

First, configure the keyboard:

sudo dpkg-reconfigure keyboard-configuration

Test the keyboard, in particular the characters like slash, asterisk, etc.. If things are not ok, just configure it again with different options.

Next, configure the console:

sudo dpkg-reconfigure console-setup

And that’s it.

If you want to configure the size of your TTY, i. e. the text console resolution, check ChangeTTYResolution.

R Package with S4 Objects

When performing R packages, Linux may be the best choice because R packaging in Windows implies to install a lot of base Linux applications and tools on Windows. For those that wish to go that road, here’s what one has to do: read Making tutorial about R Packages Under Windows: A Tutorial by Peter Rossi.
For everyone else, just boot your Linux.

Before starting, one should take a look at Writting R Extensions. This is the first step for all that wish to build R extensions. A fast and easy way to know how to pack, is to read An Introduction to the R package mechanism.

When developing using S4 objects, packaging may become a big headache.
I’ve suffered this headache. I had all kinds of warnings and errors during the package tests and installation.

I’ve started by posting to the R-Help list asking for help. Here’s the post transcript:

Here's what I do:

1. in R console, I do and get:
> package.skeleton(name='remora')remora-package
Creating directories ...
Creating DESCRIPTION ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './remora/Read-and-delete-me'.
Warning messages:
1: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R",  :
deparse of an S4 object will not be
source()able
2: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R",  :
deparse of an S4 object will not be
source()able
3: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R",  :
deparse of an S4 object will not be
source()able
4: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R",  :
deparse of an S4 object will not be source()able

I don't know why I get these warnings. 
I've followed R implementation rules and the S4 objects work fine.
2. Performing the 'R CMD build remora' command I get:

* checking for file 'remora/DESCRIPTION' ... OK
* preparing 'remora':
* checking DESCRIPTION meta-information ... OK
* removing junk files
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building 'remora_1.0.tar.gz'

And the remora_1.0.tar.gz file seems ok.
3. Performing the 'R CMD check remora' command I get:

* checking for working pdflatex ...sh: pdflatex: not found
NO
* checking for working latex ...sh: latex: not found
NO
* using log directory '/home/fmm/thesis/R/src/remora.Rcheck'
* using R version 2.8.1 (2008-12-22)
* using session charset: UTF-8
* checking for file 'remora/DESCRIPTION' ... OK
* checking extension type ... Package
* this is package 'remora' version '1.0'
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking for executable files ... OK
* checking whether package 'remora' can be installed ...
ERROR
Installation failed.
See '/home/fmm/thesis/R/src/remora.Rcheck/00install.out'
for details.
4. the log file contains:

* Installing *source* package 'remora'
...
**
R

**
data

** preparing package for lazy
loading
Error in parse(n = -1, file = file) : unexpected '-> code2LazyLoadDB
-> sys.source -> parse Execution halted
ERROR: lazy loading failed for package
'remora'
** Removing
'/home/fmm/thesis/R/src/remora.Rcheck/remora'
fmm@Darkmaster:~/thesis/R/src$ grep -i __C__remoraConfiguration *
fmm@Darkmaster:~/thesis/R/src$ grep -i __C__remoraConfiguration */*
remora.Rcheck/00install.out:745: `.__C__remoraConfiguration`

But, unfortunately, no help came from there…

In the quest for the solution I’ve found out that many others were having similar problems with S4 object packaging, but no solutions were provided.
After a lot investigation, I’ve finally found it and the solution is actually quite easy.
Instead of packing it from the current environment, I’ve just passed to the package.skeleton the list of source files to build the package. As an example, here’s the small R script I’ve done to automate the packaging procedure for my “Remora” package:

cat('\nPacking Remora...\n')

file_lst <- character(5)
file_lst[1] <- '/home/m6/thesis/R/src/1_classes.r'
file_lst[2] <- '/home/m6/thesis/R/src/2_common.r'
file_lst[3] <- '/home/m6/thesis/R/src/3_model.r'
file_lst[4] <- '/home/m6/thesis/R/src/4_predict.r'
file_lst[5] <- '/home/m6/thesis/R/src/5_main.r'   

package.skeleton(name = "remora", force = TRUE, namespace = TRUE,
code_files = file_lst)

cat('\nDone.\n')

Now it's time to go to the directory created, with the name of the package,from now on {package}, and edit the following files:

  • {package}/DESCRIPTION, the description of the package;
  • {package}/NAMESPACE, the list of functions and classes to export to the user;
  • {package}/man/{package}-package.Rd, the package help file, the \examples section must provide executable code, since R check command will execute this code;
  • {package}/man/{class_name}-class.Rd, the classes help files;
  • {package}/man/{function_name}.Rd, the functions help files;

All files under /man/ are tex files and will be compiled to provide the functions help when invoked by the user.
It's only necessary to document the classes and functions that will be exported, i. e. exported in the NAMESPACE file, since all the others will not be visible to the user. All the other .Rd files may be deleted.

After the package has been created, I've tested with the "R CMD check {package}" command. My package name is "remora", so my command was "sudo R CMD check remora".
I had to run this command with the administration role, so I prefixed it with the"sudo" command. This is a characteristic my Kubuntu installation and one may not require to perform this with administrator privilegies.

Finally I've build the package with the "R CMD build {package}" command that created the tar.gz file for distribution.

To install it, just use the "R CMD INSTALL {package}" command. I've entered R and it worked fine.
To uninstall just execute "R CMD REMOVE {package}".

Install R on Kubuntu

Installing R on Kubuntu, or Ubuntu, with an editor is quite easy. The following steps will possibly work on the most common Linux distributions. Check the official information for more information.

To get R, start by updating the repositories and the get r-base:

$ sudo apt-get update
$ sudo apt-get install r-base

When one needs to develop a R package, the development stuff should also be installed:

$ sudo apt-get install r-base-dev r-recommended

As for an editor, one can use JGR. In order to use it, just follow the official installation under Linux.
JGR is Java based, so when there’s no Java installed, get it using:

$ sudo apt-get install sun-java6-jdk

To install JGR, it’s necessary to enable Java support in R:

$ sudo R CMD javareconf

And then install the JGR library:

$ sudo R
&gt; install.packages('JGR')

After installed, run:

&gt; library(JGR)
&gt; JGR()

JGR has a small problem though, it has been installed as root and it seems unusable with any other user. One can set the correct user permissions for that.

Automating the startup of R with JGR is simple when using a shell script. Name it runr.sh and include:

R -f /home/myUser/bin/jgr.r

While the jgr.r file contains the JGR start commands

library(JGR)
JGR()

Now, running ./runr.sh starts R and JGR all in one.

There are alternatives to JGR. Check Tinn-R and R Commander.