Garyu
November 14th, 2007, 05:48 PM
There is a neat little thing called PL/R that you can use to access R statistical functions from an PostgreSQL database.
sudo aptitude install postgresql-8.2-plr
Then, either in your template1 to make it default for all databases, or in the database you want to use PL/R in:
CREATE FUNCTION plr_call_handler()
RETURNS LANGUAGE_HANDLER
AS '$libdir/plr' LANGUAGE C;
CREATE LANGUAGE plr HANDLER plr_call_handler;
Now you should have PL/R working. For code examples, look at
http://joeconway.com/plr/
You really need to know some R code to make it work well.
http://www.varlena.com/GeneralBits/Tidbits/bernier/art_66/graphingWithR.html
This page has a short tutorial on how to use PL/R to create graphs of firewall hits and so forth. Unfortunately, in Ubuntu it doesn't work.
It seems, PL/R requires the X server for displaying data. That does not work in Ubuntu at least. So you have to use a fake X server to go around this problem:
sudo aptitude install xvfb
gksudo gedit /etc/postgresql/8.2/main/environment
DISPLAY = ':5.0'
R_HOME = '/usr/lib/R'
/usr/X11R6/bin/Xvfb :5 -screen 0 1024x768x16 >& /dev/null &
(maybe this should be run as postgres user- sudo su postgres - or even put in the .bash_profile of postgres user)
sudo /etc/init.d/postgresql-8.2 restart
(to restart postgresql, or you may have to reboot the computer)
Now you will be able to create graphs like this:
CREATE OR REPLACE FUNCTION test_graph() RETURNS text AS
'
X11(display='':5'');
png(''test_graph.png'');
plot(rnorm(50), rnorm(50));
dev.off();
print(''done'');
'
LANGUAGE plr;
IMPORTANT NOTE:
If you give an absolute path to save the image, you will get an error:
# select test_graph();
ERROR: R interpreter expression evaluation error
DETAIL: Error in X11(paste("png::", filename, sep = ""), width, height, pointsize, :
unable to start device PNG
CONTEXT: In PL/R function test_graph
So you can specify a file name, but not a path. All of the images will be saved in this folder:
/var/lib/postgresql/8.2/main/
So you will have to write a script to copy the graphs if you want them in any other place.
EDIT: You can use any path to where the user postgres has rights. Easiest way of doing this is to use /tmp
All of the PNG's are owned by user postgres, so you have to chown and chmod them before using.
sudo aptitude install postgresql-8.2-plr
Then, either in your template1 to make it default for all databases, or in the database you want to use PL/R in:
CREATE FUNCTION plr_call_handler()
RETURNS LANGUAGE_HANDLER
AS '$libdir/plr' LANGUAGE C;
CREATE LANGUAGE plr HANDLER plr_call_handler;
Now you should have PL/R working. For code examples, look at
http://joeconway.com/plr/
You really need to know some R code to make it work well.
http://www.varlena.com/GeneralBits/Tidbits/bernier/art_66/graphingWithR.html
This page has a short tutorial on how to use PL/R to create graphs of firewall hits and so forth. Unfortunately, in Ubuntu it doesn't work.
It seems, PL/R requires the X server for displaying data. That does not work in Ubuntu at least. So you have to use a fake X server to go around this problem:
sudo aptitude install xvfb
gksudo gedit /etc/postgresql/8.2/main/environment
DISPLAY = ':5.0'
R_HOME = '/usr/lib/R'
/usr/X11R6/bin/Xvfb :5 -screen 0 1024x768x16 >& /dev/null &
(maybe this should be run as postgres user- sudo su postgres - or even put in the .bash_profile of postgres user)
sudo /etc/init.d/postgresql-8.2 restart
(to restart postgresql, or you may have to reboot the computer)
Now you will be able to create graphs like this:
CREATE OR REPLACE FUNCTION test_graph() RETURNS text AS
'
X11(display='':5'');
png(''test_graph.png'');
plot(rnorm(50), rnorm(50));
dev.off();
print(''done'');
'
LANGUAGE plr;
IMPORTANT NOTE:
If you give an absolute path to save the image, you will get an error:
# select test_graph();
ERROR: R interpreter expression evaluation error
DETAIL: Error in X11(paste("png::", filename, sep = ""), width, height, pointsize, :
unable to start device PNG
CONTEXT: In PL/R function test_graph
So you can specify a file name, but not a path. All of the images will be saved in this folder:
/var/lib/postgresql/8.2/main/
So you will have to write a script to copy the graphs if you want them in any other place.
EDIT: You can use any path to where the user postgres has rights. Easiest way of doing this is to use /tmp
All of the PNG's are owned by user postgres, so you have to chown and chmod them before using.