More explorations of Shiny

June 11, 2013
By

(This article was first published on Econometrics by Simulation, and kindly contributed to R-bloggers)

I have continued to explore the functionality of the Shiny package released by the Rstudio team and I have been increasingly impressed.  The code fits together very clean and easy to manipulate or add to.  If you have some knowledge of html or java shiny makes an excellent opportunity for developers of web apps with backgrounds in R.

In this post I present my recent explorations of shiny which include a simple yet effective hit counter, a new screen layout allowing for three panels, and a demonstration of how to add a simple html line break to a shiny app as well as a link.  I will copy excerpts of the code dealing with these features below, though the entire code for the app can be found at https://github.com/EconometricsBySimulation/2013-06-11-Shiny-Exploration
(You will need the shiny package and the shiny incubator package)

1. Make a hit counter
The hit counter is easily made with the following code in the server.R file:
SP <- list() # Server parameters
  # Record the number of poeple who have used the app 
  #    since initiation on the server
  SP$npers <- 0

shinyServer(function(input, output) {
  # shinyServer is Started up every time the domain is called.
  # Use <<- to assign to the global server environment.
  SP$npers <<- SP$npers+1
...
}

# ui.R
...
With the ui.R file there is a single function at the appropriate place:
# Display the total number of hits on the app.
  h5(textOutput("hits")),
...

2. Allow for three panels in the user interface.  In order to accomplish this I simply modified the pageWithSidebar function replacing 'div(class = "row-fluid", sidebarPanel, mainPanel)' with 'div(class = "row-fluid", left,  middle, right)'.  I tried to write a function that more generally took ... but could not figure out the exactly right syntax.  See where the new function is defined below:

# This is a UI page with three panels
threepage <- function(headerPanel,left,middle,right) {
  bootstrapPage(div(class = "container-fluid", div(class = "row-fluid",
  headerPanel), div(class = "row-fluid", left,  middle, right)))}

3. Insert an HTML break and an external link.  Both of these things are extremely easy.  For many HTML tags there are already programmed up shiny functions which handle them.  However, there are quite a few so not all of them have a function associated with it.  A typical HTML function is br which returns:
> br()
[1] "<br/>"
attr(,"html")
[1] TRUE
if called.  In terms of HTML this command will insert a line break.  If you are interested in including HTML in which there is no function already programmed you can use the function HTML
>HTML"<hr/>"
[1] "<hr>"
attr(,"html")
[1] TRUE
In order to insert a link into shiny you could either use the HTML"<a href=>..." type html sequence or one could use the built in functions.
a(Text, href="link.com")

Personally I prefer the built in function because they are very appealing and concise for an R programmer.

Take a look at the app at:
App Link

To leave a comment for the author, please follow the link and comment on his blog: Econometrics by Simulation.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.