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

November 7, 2010

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.


<html>
<head>
<title>Monthly Mortgage Payment Calculator</title>
</head>

<FORM action="/cgi-bin/R.cgi/mortgage_calc.R" method="POST">
<body>
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>

</body>
</html>

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

tag(HTML)
	tag(HEAD)
		tag(TITLE)
			cat("Mortgage Monthly Payment Calculator")
		untag(TITLE)
	untag(HEAD)

tag(h3)
	cat("Mortgage Monthly Payment Calculator")
untag(h3)

lf(2)
tag(BODY)

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

lf(2)
tag(p)
	cat("Monthly Loan Payment: ")
untag(p)

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

untag(BODY)
untag(HTML)

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
    library(CGIwithR,warn.conflicts=FALSE)

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

    R_DEFAULT=/usr/bin/R

    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)
Advertisements

Asynchronicity

November 6, 2010

The world is becoming ever more asynchronous. We do many things together, but this has become increasingly likely to be done at arms length. Face to face meetings have become less likely. Even worse, instead of talking on the phone, an email often suffices. Everything has become asynchronous. Truth be told, we probably like it this way!

There are examples everywhere. Instead of playing cards by sitting around a table, we now play internet poker. The same is true of chess, which is not only online but asynchronous, yours truly being a shining example of succumbing to this phenomenon. Instead of the phone, we send emails. Even TV watching, which used to be a joint family past time is now relegated to individual laptops in separate rooms. Instead of the entire nation watching a TV program at the same time, DVR technology has ensured that we all watch it on our own time. Even sports is watched with time delay in so many locations.

But making it convenient to consume entertainment has made it inconvenient for us to spend time together. We are all running to complete the ingestion of content, leaving little time for blank moments when we might spontaneously interact with each other. Is there no way out of this mess?

Here are some ways to fight this, for in this case the trend is not your friend.

  1. Consume less media. Most media consumption is now asynchronous and done independently of others. We do not watch the news together, not even sports. So just consume less of it. That goes directly to curtailing asynchronous consumption of media. Watch as much live as possible, with someone else. News and sports are ideally suited to this approach.
  2. Stop recording. It isn’t that hard. Just get rid of the DVR. This will also help in reducing the vast amounts of time spent on TV. It will also help you do just one thing at a time. I began taking my ipod along on a walk to listen to podcasts, and as a result stopped looking around and enjoying nature. I just missed out on the peaceful quiet on my night walks, and I did not realize how much I had enjoyed it till I stopped taking the ipod with me.
  3. Switch of all cell phones, computers, and singular distractions after some specific time each evening. This really works. My reading went up three-fold once I took this step. And my sleep was much better. There is plenty of evidence that imperceptibly flickering screens can mess up sleep for several hours. After switching off screens, I was not sleeping much more, but my sleep was of much better quality.
  4. Produce something every day in place of consumption. Instead of only reading, write something, and I do not mean emails. Responding to emails is not “producing” anything, and it does not bring deep satisfaction. But writing, even something trivial like a blog post, feels really good.
  5. Play team sports, and i don’t mean MPOG (multi player online games). Getting exercise this way is much better than the isolating act of going to the gym and pounding a treadmill alone. There is so much more stimulation getting exercise in groups. Even just hiking can be so much more than just an exercise in exercise. Do things with your hands where community is required, for example gardening clubs.
  6. Join a few meet up groups. Meetups are cool, new phenomena where interest groups organize get-togethers using web technology. The meetings are in person and synchronous.

Synchronicity is about community, and community is very important. However, we seem to be slipping into a world of asynchronicity. The good news is that this problem is beatable, one person at a time. As everyone, one by one, starts engaging in synchronous activity, we unwind asychronicity rapidly, because when people do things together, a network builds rapidly, and network effects rebuild synchronicity.


Wracker

October 29, 2010

I love writing and I also love programming, but I am not very good at either. I’d say my skill level is fair. But who says one must be good at the things you love? 

As one grows older, one experiences a growing unease, a loss of anchoring that makes for deep dissatisfaction. Having passed the usual thresholds of ambition and need, happiness comes not from being good at things, or better than others, but from doing what you love. 

This simple realization came to me as I was taking a walk down Broadway in New York. My alter ego tapped me on the shoulder and asked why I did not indulge in writing and hacking? And the truth is, I don’t know. The truth is, I love writing and hacking, but have been distracted with stuff like Internet, TV, work. So maybe I need to be a “wracker”  — someone who writes and hacks!

It’s an act of pure creation, unlike TV, Internet, and some sorts of work that are, in essence, mere consumption. So it seems, real satisfaction in life comes from producing cool new things, not just from consuming. But no economist knows this secret! That a large part of utility comes from the opposite of what goes into the mere consumption of things.

It’s easy to be a wracker nowadays since the internet makes both pursuits available easily, in the form of a blog. It’s like being able to be a short-order cook. So I plan to write my blog more often, but will also open up a new channel with annotated program code that I develop and post to my blog for others to use.

It’s a subtle thing, and fine balance, but when production displaces pure consumption, that’s when we have true satisfaction.
   

 


Saving Time

April 5, 2010

Just a month ago I made a failed attempt to get to New York for Valentine’s Day. You see, my wife lives in NY and I was hoping to be there for V day. But instead of reaching there Tuesday evening, it snowed and snowed, and eventually the airlines told me that I could only get there on Friday evening at the earliest. Since I was to return Monday morning, I decided to scrap the trip altogether as there was no guarantee that flights would work okay. This, after being rebooked and canceled four times. When you are not gonna go, you ain’t gonna go!

So I stayed in California, and had five days clear with no appointments. And I got more work done in that time than any recent time I can remember. I really needed those blocks of time, and it had been impossible for me to get those built into my schedule. Except when the weather came to my rescue!

Lesson: Save time just as you would save money. Save it for a rainy day. When it’s there it gets put to good use. Having a stash of free time is important.

In many ways I feel quite stupid for having missed this simple strategy for years. I am quite adept at saving money but I was hopeless at saving time. With this realization I hope to be better.

Where was i going wrong? I let too many people take my time because I did not place a high value on it. When someone would ask to talk to me I would just check my calendar and if the time slot was free I would happily schedule a meeting, not once stopping to think if it was a good use of time. The cost is only apparent later, when you need the time and it isn’t there. It’s just like spending money willy-nilly and not saving for a rainy day.

Going forward i am going to be saving time aggressively. I have been doing it for a month, and things are much better. I have breathing room, and I feel less pressured. I am also saving others too from wasting their time. If everyone saved time aggressively, there would be so much time left for important things.

So schedule less, talk less, do a few things and do them well. Keep it simple. There is plenty of time for that. Time is money or not, but save both.


Targeting Journals

March 31, 2010

Finding a good home for research papers is hard, and I don’t mean getting the paper past gatekeeping referees. Just deciding the right journal is critical. Making a mistake on this results in poor fit, rejection of the paper, and consequent delays in getting to final publication.

There are four simple criteria that one may use to determine the best journal to which a paper may be sent. These are:

  1. Fit: The paper must be appropriate for the journal. Appropriateness has two aspects to it, that the subject matter must be that of the journal, and the paper must be accessible to the readership of the journal. There is no use sending an empirical paper to a theory journal, nor is there any point in sending a highly abstract, theoretical paper to a practitioner journal.
  2. TimelinessJ: The subject matter of a paper may be time-sensitive, i.e., the topic is a hot one and a quick publication offers a chance to be first and make an impact simply because early work may end up being seminal, and have a long citation list. Sometimes the paper needs a quick turnaround, because the author’s promotion may depend on it. Lead times in my field have become longer and longer. Hence, a journal with a fast turnaround is preferable to one that is known to be slow.
  3. Impact: This is a key criteria. The objective function is to send the paper to the highest impact journal subject to having a reasonable probability of acceptance. It makes very little sense to send a paper to a high-impact journal if the probability of acceptance is zero.
  4. Feedback: This may be also thought of as the potential for improvement. When the acceptance rate of a journal is low, we may still send it there if the refereeing is of high quality. Then even if the paper is rejected, the comments are likely to be of immense value for the next version. But sending a paper to a journal simply for the quality of refereeing is not useful, such an approach comes at the cost of inordinate delay, and papers that become “stale” are much less likely to maintain the author’s enthusiasm for getting them published. Papers that get good reviewing end up being much better in the end, and will be more cited.

Overall, each of the four criteria: fit, impact, timeliness, and quality feedback must all be present to make one journal a better outlet than others. My personal view is that these are ordered in sequence, from most important to least. Fit is the most important criterion, then impact, etc. But the relative importance of each is determined by the preferences of each individual author.


Nomacademic

January 14, 2010

It is now exactly one and a half years since I saw both, my wife and son, off to school. My son left home to start as a a freshman at UC San Diego, and my wife became a Full Professor at NYU in New York. So what am I still doing here in the Bay area?

I love it here. But more so, it is home for the entire family and someone needed to stay here to keep it that way. Even though we cover three locations: East Bay, Upper West Side, and La Jolla, home is mainly in the Bay area. It’s where all the “stuff” remains. It’s where the extended family comes together for festivals and where we spend all our holidays.

My wife and I spend time in both NY and the Bay area. We try to spend this time together, moving between locations. Whether this is a commuting situation I do not know, but it hardly feels like that. A commute usually means one fixed point and the other one moves. In our case we both move back and forth, a lot together. And our son drives up and down from San Diego, setting various land-speed records I presume!

This seems pretty unique–I know of no other family with our situation, though I am sure there are plenty out there. People often ask me, what is this like? Did you ever think you would end up living this way? How long can you keep this up?

It has been interesting, this nomadic life. There are many questions I don’t have answers for, so the best I could do was to come up with a new term for someone like me–“nomacademic” which is an amalgam of a nomad and academic. Hence the title of this post.

I am still developing my understanding of this new state I am in. Yes, I hardly would have anticipated this life, which is very interesting of course, so let me give you my impressions. It may help me to understand things better too.

  1. It is tiring and costly. Living in more than one place, two in my case, is more than twice as tiring. Not only do you deal with two of everything, especially mortgages and bills, but it adds up to more than that because you can only be in one place at a time. So things slip and then one needs to fix them now and then.
  2. You get used to being minimalist. Our place in NY has very little and it feels spacious and bright. I hope we never end up filling it to the brim with things. Feels light and easy. Who would have thought the simple life could be so easy to settle into?
  3. You learn to travel very light. I get on the plane between NY and CA with what I would call a laptop bag without the laptop. I have a laptop stored in NY and so I do not take one with me when I fly. I also have a lot of things “in the cloud” and can work from any machine from almost anywhere. And my iPhone lets me do a lot from most locations anyway. I keep a few clothes in both homes and so nothing ever needs to be carried. The best part is, I do not have to pack. I have also become adept at making it from door-to-door using only public transport (walk, bus, air, subway–I use them all). Maybe I should take the ferry one day just to make sure I use all five modes of transport in one trip.
  4. You get used to living anywhere any time. When my wife is in NY and I am in CA, I do not always come home from work. I have cousins and good friends covering various points of the compass in the Bay area and I simply drop in and stay with them. It saves me a fifty mile drive each way to and from campus but more, it lets me spend time with many people I enjoy very much. The trunk of my car always has a fresh set of clothes and other clothing for various kinds of weather, making it easy for me to “crash” anywhere. It’s easy, just keep it simple, don’t get too dependent on too many comforts, and life is really quite simple. Most important–you can make home extend to the people you love.
  5. You get used to a lot of solitude. I do spend more quiet evenings at home than expected, and it can get awfully quiet. I thought I would write a lot more, but it does not work that way as I learnt. I am not a hermit, so that does not work for me. I ended up watching a lot of TV, but have managed to wean myself off, and its been nice after that. Reading and writing for pleasure is all that I need. So much better than reading and writing for work.
  6. You don’t have space. Strangely, you think you will get a lot of “space”–and keenly look forward to it. I realized that I already had the space I needed. Luckily my wife always respected my space, even though I am not sure I did hers. I also realize now that having your space is more a mind thing than a physical thing. You can feel like you have your space even in the most crowded subway car, if only your mind is free to roam and do its own thing. That I always had, and so living alone matters much less.
  7. I spend a lot more time at odd hours in the office. I quickly found that I got more work done in the office when no one was there, and so I stay later than usual and come in a little later too. It also helps me beat all the traffic, which in the Bay area, is a real nuisance.
  8. I also tend to spend more time in other academic locations now as we tend to go together to these places. We just spent an entire month in India. I managed to travel with just one small handbag, and it made things really easy. It forced me to rule out shopping which I did none of, as it would have meant buying an additional bag. Avoided that. Laundry is a wonderful thing.
  9. I am getting less work done than I was when the entire family was here. Its been hard. When we are all in different locations, you spend a lot of energy on managing locations and trying to communicate, many times quite unsuccessfully. It can be quite frustrating. So there is stress, let no one doubt that. I am in the early stages of learning to deal with that, and I think I will. But it’s been hard, and stress is an odd thing, it kills your concentration and makes you horribly inefficient. So am working on that to get back my smooth, efficient work rhythm.
  10. I sleep late a lot. There is a vacuüm in the house that makes me potter around, reading, writing, clearing, etc., all in an effort to fill the place with activity. I have to learn to develop a new routine and go to bed on time. But nowadays, I sleep around 2am, surface whenever and then mosey on to the university. It feels good, even though every morning I get up regretting yet another late night.
  11. There’s is lots more to write about being a nomacademic, and I will return to this theme again. But now, it’s close to midnight and I have only two hours more of time to binge on books and songs and other distractions, till sleep just takes over and shuts me down involuntarily.


xPhone

June 20, 2009

Everyone remembers the Matrix movie where the phone is one’s connection into the grid and you can hyper-transport to anywhere you want to go. It seems great till you realize that the phone is why the grid has its tentacles into you all the time. The iPhone is the umbilical cord that has insidiously eaten away your freedom. Brothers and Sisters, Big and Small, are always watching. How horrible is that?

It gets worse. The cell phone will drive us all to distraction. Of course, driving distracted with a phone is a leading vehicular problem, but more than that, the phone disrupts your life tremendously, and not just when driving. The cell phone is the leading cause of interruption in one’s daily routine. Why do we allow it to be so? If a person interrupted you ever so often while you were reading, talking or just trying to get things done, you would consider it rude. Yet we are infinitely forgiving of this inanimate object, showering it with gratitude for making a mess of our day!

The segment of the population most impacted are teens to young adults, who have been raised by a cell phone. They spend more time interacting with it than with humans, books, or nature. Being raised on a steady diet of information driblets, much of it vacuous talk, is the most unhealthy foundation on which to grow one’s mind.

It is a parasite that is destroying young minds. It sucks away useful time and destroys concentration. My casual empiricism suggests that even if there are two five minute calls each hour, each of which lead to a further loss of five minutes each in terms of interruption of some other tasks, we lose one-third of our waking hours to this scourge. For students, in terms of study time and quality of life, this is a humungous cost. In terms of mental development, the long term costs are catastrophic.

A recent study in the Journal of Environmental Psychology showed that excessive cell phone usage results in a 25% drop in a student’s grades. So a student with a 4.0 GPA falls off to a 3.0 GPA. Its become that easy to depreciate an A-student to a B-level one.

The study also showed that interruptions from a cell phone not only impacted the receiver’s performance, but it also distracted others and impacted them negatively too. Its become as bad as second-hand smoking! Maybe even worse, because while the number of smokers is declining, the number of young cell-phone addicts is climbing exponentially.

The parallels with smoking hardly end there. Have you noticed how funny it is that the orbital region of 20 feet from the entrance to a building is mostly populated by smokers or cell phone users? Sometimes the poor victim has succumbed to both scourges. Have you noticed that the way people walk when smoking or cell-phoning seems strangely similar? Self-absorption with a glazed-over look. Blissfully and self-importantly unaware of impending doom. The only problem is that we have not yet invented a “patch” for cell phone quitters.

Cell phones are of course lawful. There is no legal age before one can get a cell phone. I’d like to propose disallowing it for anyone below the age of 25. It would save an entire generation from academic deterioration and lay the groundwork for a better society. My advice to all you college undergrads is this: make the cell phone the xPhone.