Building a Simple Web App using R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I’ve been interested in building a web app using R for a while, but never put any time into it until I was informed of the Shiny package. It looked too easy, so I absolutely had to try it out.
First you need to install the package from the command line .
options(repos=c(RStudio="http://rstudio.org/_packages", getOption("repos")))
install.packages("shiny")
The version in the tutorial uses a two R files, won the front end(ui.R) and the other being the server side (server.R). However, I wanted a refresher in HTML, so I built it with one HTML and one R file. The structure is defined here.
For the data, I’m using MLS (home sales) data for Philadelphia, which I’ve been sitting on for quite a while. The thought behind the app is to be able to examine monthly average listing prices to monthly average prices, and in the end it turned out rather well.
At first, I had the server.R try to do everything within the reactive function, but it literally took minutes to load a new graph. The solution was to run the data through the following and just have the reactive function call from the data frames:
##raw data
d ##convert to numeric from factored variable
zip<-as.numeric(as.character(d$area))
#round the listing date to first of the month
listed<-paste(format(as.POSIXlt(d$listdate), format="%Y-%m"), "01", sep="-")
#round the sales date to first of the month
solded<-paste(format(as.POSIXlt(d$solddate), format="%Y-%m"), "01", sep="-")
#create the time period
period<-seq(as.Date("2010-02-01"), length=24, by="1 month")
#create empty data frame for average monthly listing and sales data
listing<-data.frame(period=period)
sales<-data.frame(period=period)
##list of all zip codes in Philly that we'll examine
a<-list("19103","19111","19114","19115","19116","19119","19120",
"19124","19125","19128","19130","19131","19134","19135","19136",
"19138","19142","19143","19144","19145","19146","19147","19148",
"19149","19152","19154")
#find the average monthly listing and sales figures for each zip code
for(z in a){
listing[[z]]<- sapply(period, function(x) mean(d[x >= listed & x <= solded & zip==z, 'listprice']))
sales[[z]]<-sapply(period, function(x) mean((d[ x== solded & zip==z, 'soldprice'])))
}
##save both files because the server will have to call it
save(sold, listing, file="shiny.RData")
Once the data is saved, the structure of the UI and Server can be defined. The UI in HTML was very easy (found here), All did was change font, add background pic to the body , add more zip code choices to the drop down box (