This post follows this post and this other post that, respectively, described how to create a Graphical User Interface with R and the package RGtk2 and how to build an R package that includes this graphical user interface. Here, I use the previous functions (which aim at helping lazy students to perform easily what their teachers want: the package provides basic statistics for all variables included in a chosen dataset provided in a ‘csv’ file) to create a new version of the original wnaetw package. This version now includes a web user interface, using the R package shiny.

Update! The application is now hosted on rstudio server and can be directly used online here (since I wrote this post, I have also added boxplots and a button to download the data file and the results; also, I updated the code in July 2013 to fit the new syntax of shiny). You can have a look at the code on my github

git clone https://github.com/tuxette/wnaetw.git


I thank Nicolas who pointed me out the package.

## Foreword…

I programed and documented several online applications with shiny. The ones that are currently running are:

• the toy example ‘wnaetw’ which is used to provide simple statistics on a given dataset. It can be tested with this file (that contains basic statistics on my former first year students) and the source code is provided in this post as well as on github, using:
git clone https://github.com/tuxette/wnaetw.git

git clone https://github.com/tuxette/fbs.git

• the more serious ‘NiLeDAM’ WUI, which is a graphical user interface for the NiLeDAM package, currently hosted on R-Forge. This interface is dedicated to geologists to help them date monazites from electron microprobing. The source code is provided on github, using:
git clone https://github.com/tuxette/niledam.git

• an application to illustrate some important concepts of my course M1102 (univariate descriptive statistics, DUT STID): view application. The source code is provided on github, using:
git clone https://github.com/tuxette/m1102.git


## Installing shiny

First, you have to install shiny. As the package is not an official CRAN package, you first have to add the rstudio repository to your repository list. In R, run the following command line:

options(repos=c(RStudio='http://rstudio.org/_packages', getOption('repos')))
install.packages('shiny')

and chose the first repository of the list (its name starts with ‘0’).

## Preparing the shiny interface

In the directory inst of your package source, create a subdirectory shiny-WUI where you will create two files ui.R and server.R. The ui.R file contains the description of the user interface. Mine is simply

# Set the working directory and list all csv files inside
# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Welcome lazy student! You're currently using the program
'What Nicolas's teacher wants' "),

# Left hand side panel
sidebarPanel(
h2("Data importation"),

# Button to import data
fileInput('file1', 'Choose CSV/TXT File',
accept=c('text/csv', 'text/comma-separated-values,text/plain')),
# Various checkboxes and input fields to specify the data file format
checkboxInput('rownames', ' Row names?', FALSE),
selectInput('sep', 'Separator:',
c(Comma=',',Semicolon=';',Tab='\t', Space=' '), 'Comma'),
selectInput('quote', 'Quote:',
c(None='','Double Quote'='"','Single Quote'="'"),
'Double Quote'),
selectInput('dec', 'Decimal mark', c(Dot='.', Comma=','), 'Dot'),
br(),
# Simple texts
p(HTML("Get the sources on github:

git clone
https://github.com/tuxette/wnaetw.git</code>")),
p(HTML("This application is kindly provided by

Natty with the generous help of
Nicholas,
Arthur P. and
the licence WTFPL."))
),

# Main panel (on the right hand side)
mainPanel(
tabsetPanel(
tabPanel("Data",
h3("Basic user guide"),
p(HTML("To run the application, import your data set using the
import button on the left hand side panel. You data must
be supplied on the form of a text/csv file. An example
of a properly formatted file is provided at <a href=
>here</a> (it contains simple data on my former
first-year students): this file is formatted using the
default options of the left panel. If the importation is
done properly, the data are displayed on the main panel
and analyzed on the two other panels.")),
p(HTML("

Warning! 'wnaetw' is a free program provided
without any guarantee: please note that it does not
replace your brain. In particular, the dev team is not
responsible if a lazy student is not able to interpret
the function's outputs properly!!! (and if he thinks
that an average zip code is somehow informative...)

")),br(),
h3("The dataset you want to use is displayed below:"),
p("(only the first 50 first rows if the dataset contains more
than 50 rows, and the first 10 columns if the dataset contains
more than 10 columns)"),
tableOutput("view")
),

# Numerical summary of the dataset,
# coming from the function output$summary in server.R tabPanel("Summary",downloadButton('downloadSummary', 'Download Summary'), br(),br(),tableOutput("summary")), # Graphic # coming from the function output$boxplots in server.R
tabPanel("Boxplots",
textInput("main",strong("Graphic title:"), "Boxplots"),
textInput("xlab",strong("X axis label:"), "Variables"),
textInput("ylab",strong("Y axis label:"), ""),
textInput("color","Color:","pink"),
checkboxInput(inputId = "scale", label = " Scale variables?",
value = TRUE),
plotOutput("boxplots")
)
))
))


The server.R file contains the process that should be run when the user acts on the interface and send back the output$...results to ui.R. Mine is: source("scripts/scripts.R") library(e1071) library(ineq) shinyServer(function(input, output) { # Function that imports the data file dInput = reactive({ in.file = input$file1

if (is.null(in.file))
return(NULL)

if (input$rownames) { read.table(in.file$datapath, header=input$header, sep=input$sep,
quote=input$quote, row.names=1, dec=input$dec)
} else {
read.table(in.file$datapath, header=input$header, sep=input$sep, quote=input$quote, dec=input$dec) } }) # Function that render the data file and passes it to ui.R output$view = renderTable({
d.input = dInput()
if (is.null(d.input)) return(NULL)
if (ncol(d.input>10)) d.input = d.input[,1:10]
})

# Function that calculates the output sent to the main panel in ui.R
output$summary = renderTable({ d.input = dInput() t(apply.wmtw(d.input)) }) # Function that creates a download button output$downloadSummary = downloadHandler(
filename = "summary.csv",
content = function(file) {
write.csv(apply.wmtw(dInput()), file)
})

# Function that makes a boxplot for the numeric variables in the data set
output$boxplots = renderPlot({ make.boxplot(dInput(),main=input$main,xlab=input$xlab,ylab=input$ylab,
scale=input$scale, col=input$color)
})
})


You can test your package in R by loading the shiny package and runing the following command line

runApp("PATH-TO-R-PACKAGE/inst/shiny-WUI/")

your web browser should open and with the following interface (in the image below, a dataset has been previously selected and thus the WhatMyTeacherWants been used):

## Finishing the package

To finish the package, you simply have to add in your R/ directory a function that enables the application to be started:

calculateWUI

Make sure that this function is properly documented in the man/ directory with a corresponding .Rd file. Then, the package can be build and use. Do you want to test it. Simply download it below:

Note (July, 2013): I haven’t updated this package with the new shiny syntax yet. It is probably not working anymore, though I did not test it.

### wnaetw: What Nicolas’s Teacher Wants

This package does what Nicolas’s teacher wants with numerical variables. It seems pretty clear with just the title.

Version: 2.0
Date: 2012-12-02
Author: Nicolas A. Edwards, Arthur Gomez, Jonathan Mahe and Nathalie Villa-Vialaneix
Maintainer: Nathalie Villa-Vialaneix
Depends: e1071, ineq, graphics, stats

Package source: wnaetw_2.0.tar.gz
Windows binary: wnaetw_2.0.zip
Reference manual: wnaetw.pdf

starts R and run:

library(wnaetw)
calculateWUI()

Enjoy, lazy student 😉

## Pros and Cons

In summary, to create a graphical user interface, which solution is the best: RGtk2 or shiny?

• Pro shiny: shiny is much, much easier to use than RGtk2. Coding with shiny is straightforward. Also, shiny does not require the Gtk2 environment to be installed on your computer which makes it easier to use for Windows and Mac users at least.
• Cons shiny: shiny is not is now an official CRAN package and thus can not be listed as a dependancy package for packages you want to put on CRAN (and thus, this has to be removed of the “cons” section). Also, the functionalities proposed in shiny are far less flexible than the ones you have with RGtk2.