<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/">
  <channel rdf:about="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806">
    <title>Main</title>
    <link>http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806</link>
    <description />
    <items>
      <rdf:Seq>
        <rdf:li resource="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Installation_Mac" />
        <rdf:li resource="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=FrontPage" />
        <rdf:li resource="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Installation_FreeBSD" />
        <rdf:li resource="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Installation+Tips" />
        <rdf:li resource="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Bytea+Graphing+Example" />
      </rdf:Seq>
    </items>
  </channel>
  <item rdf:about="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Installation_Mac">
    <title>Installation_Mac 1.2 (minor-edit)</title>
    <link>http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Installation_Mac</link>
    <description>== Installation Tips on a Mac ==

=== Snow Leopard ===

I know almost nothing about 'building' but after some searching, time (//&amp; tears//) I have been able to build plr (I think it's working) on Snow Leopard. I'm posting this to hopefully help those who are struggling to get it, so it is in one place.

==== Background ====
* Using William Kyngesbury's Frameworks for PostgreSQL [[http://www.kyngchaos.com/]]
* Using R 2.11.1 from [[http://r.research.att.com/]]
** This provides a 64 bit build.
* Using the 8.3.0.10 pl/r build


====1. I installed ====

 * PostgreSQL (8.4.4-2) using Will Kyngesbury's Binaries (http://www.kyngchaos.com) and
 * R 2.11 using the branch 64 bit builds : http://R.research.att.com/R-2.11-branch-leopard.pkg


====2. Get to the contrib directory====

{{{
$ cd /usr/local/pgsql/share/contrib
}}}


====3. get plr====

{{{
# sudo curl -O http://www.joeconway.com/plr/plr-8.3.0.10.tar.gz
	Password:
}}}

* Actually, using sudo the whole way can be tedious. You might prefer to su - I did this and it was much easier in the end, but you have to **Edit &gt; Enable Root User**  in Directory Utility (which is now at /System/Library/CoreServices/Directory Utility.app
* You also have to be prepared to be careful, as you can do horrendous damage if you are not careful. ** You have been warned!**


====4. Extract package ====

{{{
sh-3.2# tar -xzf plr-8.3.0.10.tar.gz
}}}


====5. look inside====

{{{
sh-3.2# cd plr
}}}


====6. set environment variables: ====

(actually, I've now put these into  ~/.profile or /etc/profile)

{{{
sh-3.2# export USE_PGXS=1
sh-3.2# export R_HOME="/Library/Frameworks/R.framework/Resources"
sh-3.2# export CUSTOM_COPT="-arch x86_64"
}}}


====7. Make &amp; Install====

{{{
sh-3.2# make
sh-3.2# make install
}}}


====8. restart postmaster with the appropriate environment variables :====


{{{
sh-3.2# su postgres
    Password:
sh-3.2$ pg_ctl -D /usr/local/pgsql/data stop
waiting for server to shut down.... done
server stopped
sh-3.2$ export R_HOME="/Library/Frameworks/R.framework/Resources"
sh-3.2$ pg_ctl -D /usr/local/pgsql/data start
server starting
sh-3.2$ exit
}}}


====9. load database====

From the command line:

{{{
$ psql -d mydatabase -U myowner -f /usr/local/pgsql/share/contrib/plr.sql
}}}

or reading the file into the database:

{{{
$ psql mydatabase
mydatabase=# \i /usr/local/pgsql/share/contrib/plr.sql
}}}</description>
    <dc:creator>Ben Madin</dc:creator>
    <dc:date>2010-08-05T01:44:03Z</dc:date>
  </item>
  <item rdf:about="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=FrontPage">
    <title>FrontPage 2.5 (minor-edit)</title>
    <link>http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=FrontPage</link>
    <description>== Welcome to the PL/R Wiki ==

This wiki contains user documentation, how-tos, and tips 'n' tricks related to PL/R. It also serves as a collaboration area for PL/R contributors.

[[http://www.joeconway.com/plr/|You can find the primary PL/R documentation here]] 

[[http://github.com/jconway/plr|New: PL/R Source repo available via github]]

* Be sure to define the R_HOME environment variable as to include $R_HOME/bin in your $PATH
* Also be sure all R library DLLs and any other dependent DLLs are in your $PATH
*[[http://www.bostongis.com/PrinterFriendly.aspx?content_name=postgresql_plr_tut01|There is a really good tutorial here, especially for Windows users]]

=== New Win32 DLLs Available ===
* [[http://www.joeconway.com/plr/plr-8.3.0.10-pg83.win32.zip]]
* [[http://www.joeconway.com/plr/plr-8.3.0.10-pg84.win32.zip]]

=== Links to Examples ===
* [[http://www.varlena.com/GeneralBits/Tidbits/bernier/art13mar04/graphingWithR.html|A Graphing Example using files]]
* [[http://www.bostongis.com/PrinterFriendly.aspx?content_name=postgresql_plr_tut02|PL/R and PostGIS]]
*[[http://blog.credativ.com/en/2010/07/postgresql-topic-of-the-day---advanced-analytics.html|Efficiently Aggregating Seismic Data with PL/R]]</description>
    <dc:creator>Joseph Conway</dc:creator>
    <dc:date>2010-07-12T23:02:21Z</dc:date>
  </item>
  <item rdf:about="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Installation_FreeBSD">
    <title>Installation_FreeBSD 1.0</title>
    <link>http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Installation_FreeBSD</link>
    <description>== Installation Tips on FreeBSD ==

=== FreeBSD 8.0 ===

==== Background ====
* Using PostgreSQL 8.4.4 from ports

Tip: After other updates, I appeared to be left without a gettext library, libintl.so.8, which is specifically required by postgresql. After much searching I found a tip on the web suggesting a symlink as such:
{{{
# cd /usr/local/lib
# ln -s libintl.so.9 libintl.so.8
}}}
So far so good!

* Using R 2.11.1 from ports
** This provides a 64 bit build.
* Using the 8.3.0.10 pl/r build


====1. I installed ====

 * Install the ports as above

====2. Get to the contrib directory====

{{{
$ cd /usr/local/share/postgresql/contrib
}}}


====3. get plr====

{{{
# sudo curl -O http://www.joeconway.com/plr/plr-8.3.0.10.tar.gz
	Password:
}}}

* Actually, using sudo the whole way can be tedious. You might prefer to su - I did this and it was much easier in the end.
* You also have to be prepared to be careful, as you can do horrendous damage if you are not careful. ** You have been warned!**


====4. Extract package ====

{{{
sh-3.2# tar -xzf plr-8.3.0.10.tar.gz
}}}


====5. look inside====

{{{
sh-3.2# cd plr
}}}


====6. set environment variables: ====

(actually, I've now put these into  ~/.profile)

{{{
sh-3.2# setenv USE_PGXS 1
}}}


====7. Make &amp; Install====

The trick here is to use gmake (gnu make), not the normal make command, which invokes FreeBSD's entire make process.

{{{
sh-3.2# gmake
sh-3.2# gmake install
}}}


====8. restart postmaster with the appropriate environment variables :====


{{{
sh-3.2# su pgsql
    Password:
sh-3.2$ pg_ctl -D /usr/local/pgsql/data stop
waiting for server to shut down.... done
server stopped
sh-3.2$ pg_ctl -D /usr/local/pgsql/data start
server starting
sh-3.2$ exit
}}}


====9. load database====

From the command line:

{{{
$ psql -d mydatabase -U myowner -f /usr/local/share/postgresql/contrib/plr/plr.sql
}}}

or reading the file into the database:

{{{
$ psql mydatabase
mydatabase=# \i /usr/local/share/postgresql/contrib/plr.sql
}}}</description>
    <dc:creator>Ben Madin</dc:creator>
    <dc:date>2010-06-15T12:58:33Z</dc:date>
  </item>
  <item rdf:about="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Installation+Tips">
    <title>Installation Tips 1.1 (minor-edit)</title>
    <link>http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Installation+Tips</link>
    <description>== Platform specific tips &amp; tricks for installation ==

In most cases the normal build commands work fine - sometimes there are a few tricks for beginners...

[[Installation_FreeBSD|FreeBSD]]

[[Installation_Mac|Mac OS X]]

[[http://www.bostongis.com/PrinterFriendly.aspx?content_name=postgresql_plr_tut01|Windows]] (Note: updated binary installers for PostgreSQL 8.3 and 8.4 are now available at [[http://www.joeconway.com/plr/]])</description>
    <dc:creator>Mike Leahy</dc:creator>
    <dc:date>2010-02-22T06:12:03Z</dc:date>
  </item>
  <item rdf:about="http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Bytea+Graphing+Example">
    <title>Bytea Graphing Example 1.5 (minor-edit)</title>
    <link>http://joeconway.com/web/guest/pl/r?p_p_id=36&amp;p_p_lifecycle=0&amp;p_r_p_185834411_nodeId=10806&amp;p_r_p_185834411_title=Bytea+Graphing+Example</link>
    <description>The following example returns values from the [[http://en.wikipedia.org/wiki/Logistic_map|logistic map]] that are indexed by a dummy date variable.  The final result is a png of a heatmap showing the behavior over "time" for various values of r.  The result is returned as a bytea object.  This bytea object can be passed directly to a webserver for display in a browser. All off the following commands are to be entered into a postgresql terminal, or saved in a file and sourced in.

First, a new type is created.  This allows functions to return sets of this type.
{{{
create type t_logistic as (r numeric, x numeric, index date);
}}}

Next, a new function is created that takes an initial value (x0, between 0 and 1), the length of the desired logistic map, and the multiplying factor (r, between 0 and 4).  The return value is a set of rows of the type defined above.
Note the index must be passed to as.character() before it's returned to postgresql.
{{{
create function r_logistic(x0 numeric, length integer, r numeric) returns setof t_logistic as $$
    ret = rep(x0, length)
    index = as.character(as.Date(1:length, origin='1970-01-01'))
    for (i in 2:length) { 
        ret[i] = r * ret[i-1] * ( 1 - ret[i-1] )
    }
    ret  = cbind(r, ret, index); 
    return(ret)
$$ language 'plr';
}}}

The next function calls the previous function over a range of r values and joins the results together into a single array, which is returned as a set of rows as above.  Note the use of sprintf to easily construct the query using input variables.
{{{
create function r_logistic_set(x0 numeric, length integer, r_from numeric, r_to numeric, r_by numeric) returns setof t_logistic as $$
    r_seq = seq(r_from, r_to, r_by)
    for (i in 1:length(r_seq)) { 
        my.qry = sprintf("select * from r_logistic(%s, %s, %s);", x0, length, r_seq[i])
        tmp =  dbGetQuery('', my.qry)
        if (i ==1) {
            ret = tmp
        } else {
             ret = rbind(ret, tmp)
        }
    }; 
    return(ret)
$$ language 'plr';
}}}

Calling these functions would look like this:
{{{
select * from r_logistic(.5, 10, 3.5);
select * from r_logistic_set(.5, 10, 3.5, 3.6, 0.02);
}}}

Now we can use the above function to populate a new table with the above function using postgresql's [[http://www.postgresql.org/docs/8.3/interactive/sql-createtableas.html|CREATE TABLE AS]].  
{{{
create table test_logistic as select * from r_logistic_set(.5, 250, 0, 4, 0.02);
}}}
The above call is computationally intensive.  The resulting table can be rapidly queried.

Finally, the resulting table is queried from within the plotting function.  Note the query of the above table returns a data frame whose column names are determined by the initial type definition.  Also, as.Date() must be used to convert the character vector back into dates for plotting. 
{{{
create function r_plot_heat_logistic(r_from numeric, r_to numeric, width integer, height integer) returns bytea as $$
    ## uses plr functions r_logistic_set() and r_logistic()
    ## function to make jpeg as bytea to hand out of database
    ## Uses RColorBrewer to make nice color paletter
    ## and lattice to make levelplot
    ## try width = 1600 and height = 1200

    ## necessary libraries
    require(lattice)
    require(cairoDevice)
    require(RColorBrewer)
    require(RGtk2)

    ## create colormap and plotting device
    mycuts = 11
    mypal = brewer.pal(mycuts, 'Spectral')
    pixmap &lt;- gdkPixmapNew(w=width, h=height, depth=24)
    asCairoDevice(pixmap)

    ## get data
    my.qry = sprintf("select * from test_logistic where r between %s and %s;", r_from, r_to)
    tmp = dbGetQuery('', my.qry)
    tmp$index = as.Date(tmp$index)

    ## plot data
    myplot = levelplot( x ~ index *r, data=tmp,
            col.regions=mypal, cuts=(mycuts-1)
    )
    print(myplot)
    plotPixbuf &lt;- gdkPixbufGetFromDrawable(NULL, pixmap, # 0, 0, 0, 0, width, height)
               pixmap$getColormap(), 0, 0, 0, 0, width, height)
    buffer &lt;- gdkPixbufSaveToBufferv(plotPixbuf, 'png', character(0), character(0))$buffer
    return(buffer)
$$ language plr;
}}}

Using a python MVC framework such as pylons, the above would be called from a function that looks something like this:
{{{
    def plotlogistic(self):
        response.headers['Content-type'] = 'image/png'
        c.plotheat = meta.Session.bind.execute("select plr_get_raw(r_plot_heat_logistic(2.5, 4, 1200, 800))").fetchone().values().pop()
        return c.plotheat

}}}
Here, a session is already bound to the database.  An sql query is executed on the database, where our function is wrapped in a call to plr_get_raw().  A header is set on the result, and it's ready to be rendered. A form can be used to pass values into r_plot_heat_logistic.

The final result is shown below:
{{plotlogistic.png}}

To remove the relations in the above example, use the following:
{{{
drop type t_logistic cascade;  -- also drops all functions that use this type
drop table test_logistic ;
drop function r_plot_heat_logistic ( numeric, numeric, integer, integer) ;
}}}</description>
    <dc:creator>Christian Gunning</dc:creator>
    <dc:date>2010-01-13T10:36:59Z</dc:date>
  </item>
</rdf:RDF>

