Emmabuntus, Ubuntu, Derivate, Linux, Open Source BackTrack, Linux, distributions, Ubuntu, derivate, securuty, forensic VirtualBox, Linux, Ubuntu, Raring Ringtail synaptic, Ubuntu, Linux, software packages jwplayer, multimedia, Linux, Ubuntu, flash Meshlab, graphic, software, Ubuntu, open source, Linux Synapse, Linux, Ubuntu, raring, Quantal Gimp, Ubuntu, Linux FreeMind, Linux, open source Linux, infographic, history

rApache Web Application Development with R and Apache: Difference in r-handler and r-script (Chapter III).

Rapache is a simple tool for remotely managing and configuring an apache2 server.

If you need a simpler software just to start/stop apache2 and mysql, check localhost-indicator.

 

3.6.2 Difference in r-handler and r-script.

r-handler, r-script, and r-info are valid arguments to Apache's SetHandler directive ("r-info" is already described in section 2.3). Calling SetHandler forces url's to be parsed through the named handler.

 

r-handler is used when you want to call an R function without arguments. You can also specify a particular file as well (see below). It is generally used within Location directives.

 

Currently we don't have a dedicated website.

rapache-can-fix-it

 

r-script is used when you want to call an R function with 2 arguments: the first is the full path to the file, and the second is an R environment. It is generally used within Directory directives.

 

Using either of these requires that the function or script return a suitable HTTP response code. For most cases this will be the value OK, which sends an HTTP response code of 200 to the browser. If the function or script would like to signal an error condition, then it should return an object with an S3 class of 'try-error' (read the R documentation for try and tryCatch for further info on the 'try-error' class).


3.6.3 RHandler.

RHandler is used to specify an R function to handle incoming web requests. The function must exist either in an attached package or it must be found on the R search path. You can use the "::" to preface the function with the package name, just as in R.

 

Examples:

# Specify foo as the function to run. Probably created
# by REvalOnStartup or RSourceOnStartup

RHandler foo

# Run the function bar located in the package foo

RHandler foo::bar

3.6.4 RFileHandler

RFileHandler is used to specify a file and/or the function to handle incoming web requests. The "::" notation is used to specifiy the file and the function together. Absolute paths to files are expected. Examples:

# Hello world example. equivalent to calling source('/var/www/R/hello.R')
# on each request.

RFileHandler /var/www/R/hello.R

# Call the function foo within the file bar.R

RFileHandler /var/www/R/bar.R::foo

 

Another note about RFileHandler. Each file specified is parsed only when its timestamp changes. This is useful for debugging, and once you are happy with the functionality, you may want to place it in a package use RSourceOnStartup and turn it into a function call instead for more efficiency.


3.6.5 REval and RFileEval.

These two will take arbitrary R expressions and run them on each request. They are similar to RHandler and RFileHandler. For instance the above RHandler configuration example can be converted to use REval:

# Specify foo as the function to run. Probably created
# by REvalOnStartup or RSourceOnStartup

REval "foo()"

# Run the function bar located in the package foo

REval "foo::bar()"

and similarly for RFileHandler:

# Call the function foo within the file bar.R

RFileEval "/var/www/R/bar.R:foo()"

Note that a single colon is used to separate the filename from the expression to be evaluated. Note also that r-script is ignored in this case and r-handler is presumed.

3.6.6 Example Configurations.

By far the easiest configuration will be the brew example. Each file under /var/www/brew is treated as a brew script:

# Any file under /var/www/brew is passed through the function brew located in
# the package brew.
<Directory /var/www/brew>
    SetHandler r-script
    RHandler brew::brew
</Directory>

Another option is to use sys.source:

# Any file under /var/www/R-files is passed through the function sys.source.
<Directory /var/www/R-files>
    SetHandler r-script
    RHandler sys.source
</Directory>

Hello World for the url at /test/helloworld:

# Runs the R expressions in helloworld.r for every request
# that matches /test/helloworld, including /test/helloworld/foobar

<Location /test/helloworld>
        SetHandler r-handler
        RFileHandler /path/to/R/scripts/helloworld.r
</Location>

Deploying a Rook application:

# Run the Rook application named 'app'. On each request, the expression
# 'Rook::Server$call(app)' is evaluated in an environment populated by
# rookapp.R. 'app' is expected to be found in that environment.
<Location /test/RookApp>
        SetHandler r-handler
        RFileEval /path/to/Rook/App/rookapp.R:Rook::Server$call(app)
</Location>

 

4. rApache Functions.

The following functions can be used inside R handlers.


4.1 setHeader.

Add HTTP Response Headers (RFC2616) to the response. All headers must be added before the first output from print() or cat().

Example:

setHeader(header='X-Powered-By',value='rApache')

Arguments:

    header Character. Name of the header: case-insensitive.
    value Any valid R object of length 1 that can be converted to type character. Value of the header.

Returns:

    the value passed in or NULL if name or value are not of type character.

 

4.2 setContentType.

Allows handler to set the content type of the request. Must be called before output with print or cat().

Example:

setContentType(type='image/png')

Arguments:

    type character vector of length 1. Valid MIME type.

Returns:

    type or NULL on error.

 

4.3 setCookie.

Add HTTP Cookies to the response headers. In the simplest case, calling setCookie('foo','bar') sets the cookie 'foo' to the value 'bar'. Calling setCookie('foo') will delete the cookie 'foo'. Any non-standard key value pairs can be appended by using ...

Example:

setCookie(name='sessionID',value=paste(rnorm(1)))

Arguments:

    name Character. Name of the cookie.
    value Any valid R object of length 1 that can be converted to type character. Value of the cookie.
    expires POSIXct object. Determines when the cookie expires.
    path Character. URL to which the cookie pertains.
    domain Character. Domain to which the cookie pertains.
    ... Any additional values appended to the cookie.

Returns:

    Either the string representation of the cookie or NULL if name was not provided.

 

4.4 urlEncode and urlDecode.

Percent encoding and decoding (url for short) of character vectors.

Example:

urlEncode(str='hello world@example.com')
urlDecode(str='hello+world%40example.com')

Arguments:

    str Character vector of strings to encode or decode.

Returns:

    Character vector the same length as str.

 

4.5 RApacheInfo.

Print out a report (sample) about rApache. Should be the only call in your R handler. Equivalent to using "SetHandler r-info".

Example:

RApacheInfo()

Arguments:

    None

Returns:

    NULL.

If you liked this article, subscribe to the feed by clicking the image below to keep informed about new contents of the blog:

rss_trappola

Share on Google Plus

About Hugo Repetto

Ubuntu is a Linux distribution that offers an operating system predominantly focused on desktop computers but also provides support for servers. Based on Debian GNU / Linux, Ubuntu focuses on ease of use, freedom in usage restriction, regular releases (every 6 months) and ease of installation.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment