Web-enabling R functions with CGI on a Mac OS X desktop

I write many R functions for my own use and for use in class. I have been making these functions available from a web page for some time, and finally decided to just post a simple example to make it easy for others to do the same. This is just an example based on the “Rcgi” package from David Firth, and for full details of using R with CGI, see http://www.omegahat.org/CGIwithR/. Download the document on using R with CGI. It’s titled “CGIwithR: Facilities for Processing Web Forms with R”.

Of course, if you don’t have R at all, then download R and install it from http://www.r-project.org/. Then use the R package manager to install the Rcgi package.

You need two program files to get everything working.
(a) The html file that is the web form for input data.
(b) The R file, with special tags for use with the CGIwithR package.

Our example will be simple, i.e., a calculator to work out the monthly payment on a standard fixed rate mortgage. The three inputs are the loan principal, annual loan rate, and the number of remaining months to maturity.

But first, let’s create the html file for the web page that will take these three input values. We call it “mortgage_calc.html”. The code is all standard, for those familiar with html, and even if you are not used to html, the code is self-explanatory.

<title>Monthly Mortgage Payment Calculator</title>

<FORM action="/cgi-bin/R.cgi/mortgage_calc.R" method="POST">
Loan Principal: <INPUT name="L" value="" size=5><p>
Annual Loan Rate: <INPUT name="rL" value="" size=5><p>
Remaining months: <INPUT name="N" value="" size=5><p>

<P><INPUT type="submit" size=3>


Notice that line 06 will be the one referencing the R program that does the calculation. The three inputs are accepted in lines 08–10. Line 12 sends the inputs to the R program.

Next, we look at the R program, suitably modified to include html tags. We name it “mortgage_calc.R”.

#! /usr/bin/R

			cat("Mortgage Monthly Payment Calculator")

	cat("Mortgage Monthly Payment Calculator")


	L = as.numeric(scanText(formData$L))
	cat("Loan Principal: ")
	rL = as.numeric(scanText(formData$rL))
	cat("Annual Loan Rate: ")
	N = as.numeric(scanText(formData$N))
	cat("Remaining months: ")

	cat("Monthly Loan Payment: ")

r = rL/12
mp = r*L/(1-(1+r)^(-N))


We can see that all html calls in the R program are made using the “tag()” construct. Lines 22–35 take in the three inputs from the html form. Lines 43–44 do the calculations and line 45 prints the result. The “cat()” function prints its arguments to the web browser page.

Okay, we have seen how the two programs (html, R) are written and these templates may be used with changes as needed. We also need to pay attention to setting up the R environment to make sure that the function is served up by the system. The following steps are needed:

  1. Make sure that your Mac is allowing connections to its web server. Go to System Preferences and choose Sharing. In this window enable Web Sharing by ticking the box next to it.
  2. Place the html file “mortgage_calc.html” in the directory that serves up web pages. On a Mac, there is already a web directory for this called “Sites”. It’s a good idea to open a separate subdirectory called (say) “Rcgi” below this one for the R related programs and put the html file there.
  3. The R program “mortgage_calc.R” must go in the directory that has been assigned for CGI executables. On a Mac, the default for this directory is “/Library/WebServer/CGI-Executables” and is usually referenced by the alias “cgi-bin” (stands for cgi binaries). Drop the R program into this directory.
  4. Two more important files are created when you install the “Rcgi” package. The CGIwithR installation creates two files:
    (a) A hidden file called “.Rprofile”
    (b) A file called R.cgi

    Place both these files in the directory: /Library/WebServer/CGI-Executables

    If you cannot find the .Rprofile file then create it directly by opening a text editor and adding two lines to the file:

    #! /usr/bin/R

    Now, open the R.cgi file and make sure that the line pointing to the R executable in the file is showing


    The file may actually have it as “#! /usr/local/bin/R” which is for Linux platforms, but the usual Mac install has the executable in “#! /usr/bin/R” so make sure this is done.

    Make both files executable as follows:

    chmod a+rx .Rprofile
    chmod a+rx R.cgi

  5. Finally, make the ~/Sites/Rcgi/ directory write accessible:

    chmod a+wx ~/Sites/Rcgi

Just being patient and following all the steps makes sure it all works well. Having done it once, it’s easy to repeat and create several functions. You can try this example out on my web server at the following link.

The inputs are as follows:

  • Loan principal (enter a dollar amount)
  • Annual loan rate (enter it in decimals, e.g., six percent is entered as 0.06)
  • Remaining maturity in months (enter 300 if the remaining maturity is 25 years)

3 Responses to Web-enabling R functions with CGI on a Mac OS X desktop

  1. Ger says:

    Hey, I just got a script working like this on my local server. I was wondering how difficult this would be to host online? Would I need some special type of hosting package?

  2. teapigs says:

    Howdy very cool internet site! Gentleman. Superb. Superb. Let me save your site plus use the for additionally? Now i am willing to find loads of helpful details in this article in the set up, we require come up with far more approaches in this connection, we appreciate you giving.

  3. Mohammad says:

    Hey! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I ended up losing several weeks of hard work
    due to no data backup. Do you have any solutions to prevent

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: