Sunday, August 29, 2010

R - Vectors, Lists, and DataFrames

  • A vector can be thought as contiguous cells containing elemental data.
Creation of vectors: x <- c(1,2,3)
Indexing: x[i], x[i,j]
Concatenating vectors x <- c(x,x)

  • A list (generic vector) has elements, each of which can contain any type of R object.
Creation of lists: x <- list(name_1=object_1, ..., name_m=object_m)
Indexing: x[[i]], x[[i,j]], x$a, x$"a", x[["a"]]
Concatenating lists: list.ABC <- c(list.A, list.B, list.C)
  • A data frame is a list with class "data.frame". There are restrictions:
    • The components must be vectors (numeric, character, or logical), factors, numeric matrices, lists, or other data frames.
    • Matrices, lists, and data frames provide as many variables to the new data frame as they have columns, elements, or variables, respectively.
    • Numeric vectors, logicals and factors are included as is, and character vectors are coerced to be factors, whose levels are the unique values appearing in the vector.
    • Vector structures appearing as variables of the data frame must all have the same length, and matrix structures must all have the same row size.
Creation of dataframes: accountants <- data.frame(home=statef, loot=incomes, shot=incomef)
Attach and dettach: (For lists in general). A useful facility would be somehow to make the components of a list or data frame temporarily visible as variables under their component name, without the need to quote the list name explicitly each time.The attach() function takes a `database' such as a list or data frame as its argument.

Thursday, August 26, 2010

Django - Deploy an application to (French web site) is a French web site which allows free django hosting (and others) for websites up to 10Mb.
The site includes a fantastic but in French documentation which explains step by step how to publish a django site:

  • ssh and ftp connections are possible.
  • There is an admin site from where we need to enable the ssh user before using it.
  • The database must be created from the admin site.
  • When we make changes we may need to restart the fcgi process. This must be done from the admin web site, with the processes option, by killing the process. Immediately it is restarted.
  • Notice, as it is explained in the instructions, that we create a public directory. Some content, specially the static files, must be in that directory to be served, although the development server does not use them, it uses the files in the typical directory.
  • Important! Don't forget to redirect all queries to the main domain to the specific folder /appname/public where the project relies. This must be done from the admin panel.
  • Instructions for working with the development server are in

HTML: How do we include one HTML file into another?

Here it is explained:

It depends on the way we do it: server-side, client-side, etc.
The way I used was by client side (I needed to add the page to a blogger page).
The code I used worked fine!

Saturday, August 21, 2010

R - Fitting data using Linear Models

Linear Model explanation

Formula object explanation


x <- seq(0, 10, 0.1)

y <- 1 + x + cos(x) + rnorm(x)
yy <- 1 + x + cos(x)

plot(x, y, lwd = 3)
lines(x, yy, lwd = 3)

# 1 (default) includes intercept - 0 no
fit <- lm(y ~ 1 + x + I(x^2) + I(cos(x)))

newdf <- data.frame(x <- seq(11,2000,0.1))
newdf <- data.frame(newdf,y <- 1 + newdf$x + cos(newdf$x) + rnorm(newdf$x))
newp <- predict(fit,newdf)


Wednesday, August 18, 2010

Django - Deploy an application to

I followed these steps to deploy a Django application into
  • Register to and enter to the Panel (
  • Click into the installdjango option and create a project (it is called application in the webpage).
  • Run python startapp appname (the application should be called equal to our project's application).
  • Access with ssh and sftp to our space and put our original application into the corresponding folder. Overwrite all files except and
  • Merge the files from and the one of our application.
  • Copy the content of to
  • Run python syncdb.
  • Modify file lighttpd/django.conf to be correct.
  • Syncronize with init/appname restart, init/lightppd reload and init/lightppd restart.
Useful utilities when working are the commands
zip -r directory
which compresses the directory into the file, and
which uncompresses the directory.
Caution: if we are in folder a/ and we execute unzip b/, the content of will be extracted in our working directory a/!

Sunday, August 15, 2010

How to find the package where a file is?

You can search for an ubuntu package containing a specific file (such as "algorithm.sty") by going to Scroll down to where it says "Search the contents of packages".

Wednesday, August 11, 2010

Django - Utilities for a beginner

In this post I recopile useful information I have found when looking for help.

  • How to specify a multiple field primary key? It is complicated, but what we can do is to specify a set of fields to be unique in the meta class of our model.
  • Dynamic Django Queries

  • How can I make request.session parameters visible to a template without passing them as a parameter?

  • A good way to customize templates for visualizing forms and form errors:

  • A free online book which explains Django:

  • How to navigate in a template in a one-to-many relation:

  • A tutorial to build a first application in Django, in four steps:

  • Query sets in Django. How can we use a field of a related object inside a filter? With object1.filter(obect2__field="aaa")

  • A person who gave up Django explains its reasons:

  • The Catalan Django group:

  • A Django users Google Groups group

  • A comparison of CMSApplications for Django

  • How to pass initial values to a form without validating it?

The general explanation of form fields is here:

  • Where can we host our Django application? A list of the most popular webs and its prices:

  • Using reverse() in HttpResponseRedirect to avoid having to hardcode a URL in the view function:

  • Changing the Model Choice Field Queryset:
from django import newforms as forms
from django.contrib.auth.models import User

class ComplaintForm(forms.Form):
user = forms.ModelChoiceField(queryset=User.objects.none())
message = forms.CharField(widget=forms.Textarea())

def __init__(self, *args, **kwargs):
super(ComplaintForm, self).__init__(*args, **kwargs)
self.fields["user"].queryset = User.objects.filter(is_staff=False)

  • Official documentation about generic views

Saturday, August 7, 2010

Django - Deploy an application

Once we have a Django application finished, we probably want to put it in a "real" web server.

This page explains how we can do it, using Apache and mod_wsgi, a module that allows Apache to support any Python application which supports the Python WSGI interface, like Django. We briefly summarize the steps in this post.
sudo aptitude install libapache2-mod-wsgi
  • We can test it by creating a wsgi script and modifying the Apache configuration file httpd.conf to allow the access to this file. For example we can make an script called /usr/var/www/scripts/myapp.wsgi
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]

and adding some commands to the httpd.conf file, found in Ubuntu inside /etc/apache2 folder.
  • To deploy our Django application, we must create a file called django.wsig inside a folder, like mysite/apache, with the following content:
import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

and the httpd.conf file must have the content explained in this page.

  • We must take into accout several things:
    • Make sure the folders have the correct permissions. Specifically, they must have read and execute permissions for the www-data user, the apache server user.
    • If we are using sqlite databases, the database file and its directory must have writing permissions, as it is explained here. Also we must ensure that the db name contains the full path.