DOC PREVIEW
Berkeley STAT 133 - Dynamic Web Content

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Dynamic Web Content:HTML FormsCGIWeb Servers and HTTPDuncan Temple LangDept. of StatisticsUC Davis1Dynamic ContentWe are all used to fetching pages from a Web server.Most are prepared by a human and put on the Web site for others to read. Each reader sees the same page.The Web browser (client) can perform client-side computations on the page to present it differently.And the content can be dynamic via Javascript code in the page that operates as the page is being rendered (e.g. layout) and as it is being viewed (e.g. mouse operations)We, however, are interested in server-side dynamic content.2Server-side Dynamic PagesA request to the Web server returns a new, machine-generated page, e.g. google’ search result page, travelocity’s flight purchase page, ...The server receives the request and executes some code to create a new HTML document (or other type) and returns that.What makes this interesting is that the request can specify inputs that govern how the page is created.This is very similar to calling an R function with different inputs and generating output in the form of an HTML page3Client-Server componentsWe are in a client-server setup, like with relational databases.One application acts as a client and connects to the server and sends a request; the server sends a reply.Our client is a browser and specifically an HTML form that provides ways to specify inputs to the server.Our “server” is an R script on the Web server that gets the inputs and generates an HTML page based these inputs. To provide dynamic access to R functionality via the Web, you need to create both pieces.4OutlineDemo of an HTML formThe elements of HTML form processingHTML forms,CGI scripts & CGIwithR package.Security issues.Basics of HTTP - how this all works.Details for project.accounts, server, Web server directory layout5Basic DemoA basic simulation to illustrate the Central Limit Theorem.User specifies inputs for the sample size, the distribution from which to sample,the number of repetitions,the statistic to compute for each sample,whether to create numerical and/or graphical summary.6...<form method="GET" action="/cgi-bin/R.cgi/simulation.R">....<input type="text" name="n" value="3" size="4">.....<select name="distribution" size="1"> <option value="rnorm">Normal <option value="rbinom">Binomial <option value="rpois">Poisson </select>.....Histogram: <input type="checkbox" name="output" value="histogram" checked="checked">Summary: <input type="checkbox" name="output" value="summary" checked="checked"><input type="hidden" name="showAttribution" value="TRUE">.....<input type="submit"></center></form>7Basics of HTML FormsCreate a form in an HTML document with <form> ...</form>Specify the action which is the location (URI) of the script to run when the form is submitted.Also specify how the requested is to be submitted: GET or POST.Also add enctype attribute if necessary (more later).8<INPUT> TagsThe elements of the form are created via <INPUT type=””>, <SELECT>/<OPTION> and <TEXTAREA> tags in the HTML formDifferent type values TEXT, PASSWORD, CHECKBOX, RADIO, SUBMIT, RESET, IMAGE, HIDDEN, FILE.HTML Form Tutorial (http://www.w3schools.com/html/html_forms.asp) & another: these describe the different characteristics of these HTML elements to control their appearance and behavior.O’Reilly books: HTML - The Definitive Guide (chap. 8), and CGI Programming.9Key Aspects of <INPUT>...Each form element has a name attribute.This is the name of the parameter that is sent in the HTTP request.The selected value(s) for the element is sent as the value(s) for that parameter.In our example, get n = “3”, output = c(“histogram”, “summary”), distribution = “rnorm”, statistic = “median”.The method attribute of <form> tag specifies how these name-values are sent to the Web server in the request.10The CGI scriptWhen a FORM is submitted, a request is sent to the Web server which identifies the URI as a script to run. The Web server calls that script passing it the inputs.This mechanism is called the Common Gateway Interface - CGITypically, CGI scripts are located in the a cgi-bin area of the Web server’s file system.Only these can be run via a Web request.The cgi-bin directory is not directly accessible via a Web browser, but in a directory parallel to the Web documents. 11Script Programming LanguagesThe scripts can be written in any language, e.g. C, Perl, Python, shell or R.Each language has utilities that make it easier to process the inputs into usable `arguments’.In R, there is a package called CGIwithR that makes using R scripts via CGI significantly easier.In the cgi-bin/ directory, we place the R.cgi and .Rprofile from that package.Then, we write our R script, say myCGI.R and put that into the cgi-bin/ directory. 12Accessing the scriptTo make use of this script (myCGI.R) in the HTML form, specify the script as in <form ... action=”/cgi-bin/R.cgi/myCGI.R”>This runs myCGI.R via the shell script R.cgi.Note R.cgi is an existing file, not a directory.You don’t need to do anything to use it.All printed output (to the console/screen) generated in the R script is treated as HTML and displayed in the resulting document in the client Web browser,i.e. all output from cat() and print().13source("simulate.S") # Load work function for simulation.ans = simulate(as.integer(formData$n), as.integer(formData$NumRep), formData$statistic, formData$distribution)cat("<h2>Sample distribution of", formData$statistic, " for n =", formData$n, "from ", formData$distribution, "</h2>")# Generate numerical summary if requested.if("summary" %in% formData$output) { invisible(capture.output(library(R2HTML))) HTML(summary(ans), file = stdout())}# Generate density plot if requested.if("histogram" %in% formData$output) { webPNG("hist.png", type = "jpeg", graphDir = "../htdocs/tmp/") dens = density(ans) hist(ans, prob = TRUE, ylim = c(0, max(dens$y)), main = "Density of sample values") points(dens, col = "red") img(src = "hist.png", graphURLroot = "/tmp/")}14Accessing the form inputsHow does the R CGI script get the inputs from the form?When the script is run, there is already an R object - formData - that is a named list containing the elements of the form.formData$n yields “3” - convert it to a number with as.integer(formData$n)For the output, there may be multiple entries (e.g. “output” and “summary”) so formData$output would be


View Full Document
Download Dynamic Web Content
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Dynamic Web Content and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Dynamic Web Content 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?