This chapter contains the following information:
- About WarehousePG PL/R
- Installing R
- Installing PL/R
- Uninstalling PL/R
- Enabling PL/R Language Support
- Examples
- Downloading and Installing R Packages
- Displaying R Library Information
- Loading R Modules at Startup
- References
About WarehousePG PL/R
PL/R is a procedural language. With the WarehousePG PL/R extension you can write database functions in the R programming language and use R packages that contain R functions and data sets.
For information about supported PL/R versions, see the WarehousePG Release Notes.
Installing R
For RHEL/Oracle/Rocky, installing the PL/R package installs R in $GPHOME/ext/R-<version> and updates $GPHOME/greenplum_path.sh for WarehousePG to use R.
Note You can use the gpssh utility to run bash shell commands on multiple remote hosts.
To install R, run these
aptcommands on all host systems.$ sudo apt update && sudo apt install r-base
Installing
r-basealso installs dependent packages includingr-base-core.To configure WarehousePG to use R, add the
R_HOMEenvironment variable to$GPHOME/greenplum_path.shon all hosts. This example command returns the R home directory.$ R RHOME /usr/lib/R
Using the previous R home directory as an example, add this line to the file on all hosts.
export R_HOME=/usr/lib/R
Source
$GPHOME/greenplum_path.shand restart WarehousePG. For example, run these commands on the WarehousePG coordinator host.$ source $GPHOME/greenplum_path.sh $ gpstop -r
Installing PL/R
The PL/R extension is available as a package.
Installing the Extension Package
Before you install the PL/R extension, make sure that your WarehousePG is running, you have sourced greenplum_path.sh, and that the $COORDINATOR_DATA_DIRECTORY and $GPHOME variables are set.
Download the PL/R extension package.
Copy the PL/R package to the WarehousePG coordinator host.
Install the software extension package
Source the file
$GPHOME/greenplum_path.sh.Restart WarehousePG.
$ gpstop -r
Enabling PL/R Language Support
For each database that requires its use, register the PL/R language with the SQL command CREATE EXTENSION. Because PL/R is an untrusted language, only superusers can register PL/R with a database. For example, run this command as the gpadmin user to register the language with the database named testdb:
$ psql -d testdb -c 'CREATE EXTENSION plr;'
PL/R is registered as an untrusted language.
Uninstalling PL/R
When you remove PL/R language support from a database, the PL/R routines that you created in the database will no longer work.
Remove PL/R Support for a Database
For a database that no longer requires the PL/R language, remove support for PL/R with the SQL command DROP EXTENSION. Because PL/R is an untrusted language, only superusers can remove support for the PL/R language from a database. For example, run this command as the gpadmin user to remove support for PL/R from the database named testdb:
$ psql -d testdb -c 'DROP EXTENSION plr;'
The default command fails if any existing objects (such as functions) depend on the language. Specify the CASCADE option to also drop all dependent objects, including functions that you created with PL/R.
Uninstall R (Ubuntu)
For Ubuntu systems, remove R from all WarehousePG host systems. These commands remove R from an Ubuntu system.
$ sudo apt remove r-base $ sudo apt remove r-base-core
Removing r-base does not uninstall the R executable. Removing r-base-core uninstalls the R executable.
Examples
The following are simple PL/R examples.
Example 1: Using PL/R for single row operators
This function generates an array of numbers with a normal distribution using the R function rnorm().
CREATE OR REPLACE FUNCTION r_norm(n integer, mean float8, std_dev float8) RETURNS float8[ ] AS $$ x<-rnorm(n,mean,std_dev) return(x) $$ LANGUAGE 'plr';
The following CREATE TABLE command uses the r_norm() function to populate the table. The r_norm() function creates an array of 10 numbers.
CREATE TABLE test_norm_var
AS SELECT id, r_norm(10,0,1) as x
FROM (SELECT generate_series(1,30:: bigint) AS ID) foo
DISTRIBUTED BY (id);Example 2: Returning PL/R data.frames in Tabular Form
Assuming your PL/R function returns an R data.frame as its output, unless you want to use arrays of arrays, some work is required to see your data.frame from PL/R as a simple SQL table:
Create a
TYPEin a WarehousePG with the same dimensions as your Rdata.frame:CREATE TYPE t1 AS ...
Use this
TYPEwhen defining your PL/R function... RETURNS SET OF t1 AS ...
Sample SQL for this is given in the next example.
Example 3: Hierarchical Regression using PL/R
The SQL below defines a TYPE and runs hierarchical regression using PL/R:
--Create TYPE to store model results
DROP TYPE IF EXISTS wj_model_results CASCADE;
CREATE TYPE wj_model_results AS (
cs text, coefext float, ci_95_lower float, ci_95_upper float,
ci_90_lower float, ci_90_upper float, ci_80_lower float,
ci_80_upper float);
--Create PL/R function to run model in R
DROP FUNCTION IF EXISTS wj_plr_RE(float [ ], text [ ]);
CREATE FUNCTION wj_plr_RE(response float [ ], cs text [ ])
RETURNS SETOF wj_model_results AS
$$
library(arm)
y<- log(response)
cs<- cs
d_temp<- data.frame(y,cs)
m0 <- lmer (y ~ 1 + (1 | cs), data=d_temp)
cs_unique<- sort(unique(cs))
n_cs_unique<- length(cs_unique)
temp_m0<- data.frame(matrix0,n_cs_unique, 7))
for (i in 1:n_cs_unique){temp_m0[i,]<-
c(exp(coef(m0)$cs[i,1] + c(0,-1.96,1.96,-1.65,1.65,
-1.28,1.28)*se.ranef(m0)$cs[i]))}
names(temp_m0)<- c("Coefest", "CI_95_Lower",
"CI_95_Upper", "CI_90_Lower", "CI_90_Upper",
"CI_80_Lower", "CI_80_Upper")
temp_m0_v2<- data.frames(cs_unique, temp_m0)
return(temp_m0_v2)
$$
LANGUAGE 'plr';
--Run modeling plr function and store model results in a
--table
DROP TABLE IF EXISTS wj_model_results_roi;
CREATE TABLE wj_model_results_roi AS SELECT *
FROM wj_plr_RE('{1,1,1}', '{"a", "b", "c"}');Downloading and Installing R Packages
R packages are modules that contain R functions and data sets. You can install R packages to extend R and PL/R functionality in WarehousePG.
WarehousePG provides a collection of data science-related R packages that can be used with the WarehousePG PL/R language. For information about the packages, see Data Science Package for R.
Note If you expand WarehousePG and add segment hosts, you must install the R packages in the R installation of the new hosts.
For an R package, identify all dependent R packages and each package web URL. The information can be found by selecting the given package from the following navigation page:
https://cran.r-project.org/web/packages/available_packages_by_name.html
As an example, the page for the R package arm indicates that the package requires the following R libraries: Matrix, lattice, lme4, R2WinBUGS, coda, abind, foreign, and MASS.
You can also try installing the package with
R CMD INSTALLcommand to determine the dependent packages.For the R installation included with the WarehousePG PL/R extension, the required R packages are installed with the PL/R extension. However, the Matrix package requires a newer version.
From the command line, use the
wgetutility to download thetar.gzfiles for the arm package to the WarehousePG coordinator host:wget https://cran.r-project.org/src/contrib/Archive/arm/arm_1.5-03.tar.gz
wget https://cran.r-project.org/src/contrib/Archive/Matrix/Matrix_0.9996875-1.tar.gz
Use the gpsync utility and the
hosts_allfile to copy thetar.gzfiles to the same directory on all nodes of the WarehousePG cluster. Thehosts_allfile contains a list of all the WarehousePG segment hosts. You might require root access to do this.