




Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!





Version 0.50 (5 May 2003)
Abstract
CGIwithR is a package for use with the R statistical computing environment, to facilitate processing of information from web-based forms, and reporting of results in the Hypertext Markup Language (HTML), through the Common Gateway Interface (CGI). CGIwithR permits the straightforward use of R as a CGI scripting language. This paper serves as an extended user manual for CGIwithR, supplementary to the R help pages installed with the package.
Key words: online calculator.
Information entered on web-based forms is often processed by a CGI script, a program called by the web server which takes the data from the form as its input and typically returns its output encoded in HTML for display on the client browser. CGI scripts can be written in any suitable programming language; Perl is especially popular. The purpose of CGIwithR is to enable use of the S language, as implemented in the open-source R project (Ihaka and Gentleman, 1996), to write CGI scripts. This is useful for at least two reasons: (i) to make easily available the extensive statistical facilities of R in CGI scripts, and (ii) to allow statisticians and others familiar with the S language to write form-handlers without having first to learn another language such as Perl. The CGIwithR package has two main components:
The R.cgi wrapper should work on most Unix-like operating systems. It ought to be possible to adapt it for use also in conjunction with web servers running under Windows NT and its successors, but the author has no plans for that. The R functions for handling CGI data and for writing output in HTML are platform-independent. At least two other existing projects, Rcgi and Rweb, make R available via a web interface; see ‘R Web Interfaces’ in R’s Frequently Asked Questions document for more information. The emphasis of CGIwithR is on making R available as a scripting language, rather than on providing a user interface to R: see section 5.3 below for some further related remarks. The CGIwithR software is provided free under the terms of the Gnu Public License, version 2 or later. There is absolutely no warranty: CGIwithR is not fit for any purpose, and any use made of it is entirely at the risk of the user.
The CGIwithR package is available on the Comprehensive R Archive Network (CRAN), and should be installed locally in the same way as other R packages. Various methods are available for this; see R’s “Installation and Administration” documentation for up-to-date information. The inst subdirectory of the CGIwithR package contains the further subdirectories cgi-bin doc examples
To make CGI scripts written in R available to the local web server, the files cgi-bin/.Rprofile and cgi-bin/R.cgi must be moved or copied to a directory in which the web server is permitted to look for CGI executables (such directories are often named cgi-bin); this may need the help of a system administrator. The first few lines of R.cgi should be edited to set certain configuration options, in particular to locate R on the local system, and to locate the local installation of Ghostscript if graphical output is to be allowed. Full configuration instructions are contained in R.cgi itself. File permissions should be set such that .Rprofile is readable by the web server, and R.cgi is both readable and executable by the web server. For example, if those two files have been placed in directory /var/www/cgi-bin, the necessary permissions can be set (by the system administrator) as follows:
chmod a+r /var/www/cgi-bin/.Rprofile chmod a+rx /var/www/cgi-bin/R.cgi
Suppose that the URL for the directory permitted to contain CGI scripts is /cgi-bin, say. (There may be more than one such directory available at any given installation; this depends on the local configuration of the web server.) Then any web page containing form elements can pass its data to a script written in R (let us call such a script myscript.R) by using a tag like the following:
This passes the data first to the R.cgi script, which in turn passes it to R for processing by myscript.R. Note that R.cgi here is not a directory. In general, the parts of a URL between the slashes are not directory names, but symbols to tell the HTTP server where to find a particular document. In most cases, the server will look for a directory of the same name as the symbol in question, and if there is one, go into that directory and proceed to the next symbol. But if the server sees that a particular symbol matches the name of an executable program (here R.cgi), it will run that program and pass the remainder of the URL (which here is simply myscript.R) to the program as an argument. The form data are always made available in R as an object named formData, a list whose components have the names of data items collected in the form; see section 5 below for some examples. In most applications, each data component has a unique name which is specified in the HTML code for the form (using NAME=). An exception to this is the use of a ‘multiple select’ box in the HTML form, for example
tag(BODY, bgcolor = "yellow") lf(2) tag(h3) cat("A large heading") untag(h3)
Note that optional arguments to HTML tags, such as the bgcolor argument here, are specified in an obvious way. The form data are made available through a list in R called formData. In the example the supplied data are just sent back with minimal processing:
tag(p) cat("Your words in italics:") tag(i) cat(formData$words) untag(i) untag(p) lf(2) tag(p) cat("Your numbers:") tag(pre) numbers <- as.numeric(scanText(formData$numbers)) print(numbers) untag(pre) untag(p)
The use of scanText here converts the form’s text field into a vector. The expression lf(2) simply inserts two line feeds into the resultant HTML for readability. Graphics can be included using the function img. In the example, a plot in PNG format is constructed and displayed by
cat("Here is a graph:") ; br()
For ways of making R available via the web see instead the Rcgi and Rweb projects referred to in the ‘R Web Interfaces’ section of R’s Frequently Asked Questions document.
The ‘wrapper’ shell script R.cgi should work with most Bourne-family shells (including sh, bash, ksh, zsh) on Unix-like systems. The author is aware of success in using CGIwithR on Mac OS X, Linux and Solaris systems with the Apache web server, and would appreciate reports of success or failure on other systems.
The data entered on HTML forms is passed in an encoded string which is not suitable for direct manipulation in R. The formData variable made available to all CGI scripts written in R is a decoded version created in the .First.lib function of the CGIwithR package. An alternative and widely used decoding device is the uncgi program by Steven Grimm. The design of CGIwithR is such that it is compatible with uncgi: if uncgi is used, no further decoding is done by CGIwithR.
As distributed in the inst/cgi-bin directory, the file .Rprofile contains a single R command,
library(CGIwithR, warn.conflicts=FALSE)
which makes the CGIwithR package available in the instance of R started by the web server when responding to a CGI request. For this to work, the .Rprofile file needs to be placed in the same location on the web server as the shell script R.cgi.
The author is grateful to pod at Oxford University Computing Services for some very helpful insights on the workings of shell scripts, to Henrik Bengtsson (via Jean-Pierre M¨uller) for an ASCII/hexadecimal translation table, and to two anonymous Journal of Statistical Software reviewers for helpful comments. Thanks also to Steven Grimm for making available uncgi, which gave some important clues.
Firth, D. (2000). Quasi-variances in Xlisp-Stat and on the web. Journal of Statistical Software 5.4, 1–13.
Ihaka, R. and Gentleman, R. (1996). R: A language for data analysis and graphics. Journal of Computational and Graphical Statistics 5 , 299–314.