Analyzing Local Data with a Shiny Web App

March 19, 2013
By

(This article was first published on PremierSoccerStats » R, and kindly contributed to R-bloggers)

A great. recent enhancement for the Shiny App is the ability to upload local files.
Now, in addition to users being able to interact with data provided on the host e.g.
Soccer Tables or via the web, Wikipedia Search Rates they can use apps
to view and analyse their own data

I have knocked up an app based on the 09_upload example provided in the Shiny package. It uploads a small .csv spreadsheet file of school pupil’s scores from a local directory, displays the data and does a couple of analyses.

The ui.R  enables a user to upload a csv file with various parameters, seperator etc.  The example file is downloadable. There are then three tabs showing

  1. The raw data in a gVis Table, which allows sorting and paging
  2. A density graph with the spread of results by term and year
  3. A statistical test to see if there is a difference in marks by gender
?View Code RSPLUS
#ui.R
shinyUI(pageWithSidebar(
  headerPanel("Uploaded File Analysis"),
 
  sidebarPanel(
    helpText("This app is shows how a user can update a csv file from their own hard drive for instant analysis.
In the default case, it uses standard format school marks that could be used by many teachers
Any file can be uploaded but analysis is only available
if the data is in same format as the sample file, downloadable below
"),
    a("Pupil Marks", href="http://dl.dropbox.com/u/25945599/scores.csv"),
    tags$hr(),
    fileInput('file1', 'Choose CSV File from local drive, adjusting parameters if necessary',
              accept=c('text/csv', 'text/comma-separated-values,text/plain')),
 
    checkboxInput('header', 'Header', TRUE),
    radioButtons('sep', 'Separator',
                 c(Comma=',',
                   Semicolon=';',
                   Tab='\t'),
                 'Comma'),
    radioButtons('quote', 'Quote',
                 c(None='',
                   'Double Quote'='"',
                   'Single Quote'="'"),
                 'Double Quote'),
    tags$head(tags$style(type="text/css",
                         "label.radio { display: inline-block; margin:0 10 0 0;  }",
                         ".radio input[type=\"radio\"] { float: none; }"))
 
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Pupil Marks",
               h4(textOutput("caption1")),
               checkboxInput(inputId = "pageable", label = "Pageable"),
               conditionalPanel("input.pageable==true",
                                numericInput(inputId = "pagesize",
                                             label = "Pupils per page",value=13,min=1,max=25)),
 
               htmlOutput("raw"),
                value = 1),
      tabPanel("Term Details",
               h4(textOutput("caption2")),
               plotOutput("density"),
               htmlOutput("notes2"),
               value = 2),
      tabPanel("Gender difference",
               h4(textOutput("caption3")),
               plotOutput("genderDensity", height="250px"),
               verbatimTextOutput("sexDiff"),
               htmlOutput("notes3"),
               value = 3),
      id="tabs1")
 
)
))

The server.R takes the file, does some processing and provides a list of data which can be rendered into plots and tables

?View Code RSPLUS
# server.R
shinyServer(function(input, output) {
 
  Data

Finally, global.R loads the requisite libraries and houses the script which generated the sample file

?View Code RSPLUS
#global.R
# load required libraries
library(shiny)
library(plyr)
library(ggplot2)
library(googleVis)
library(reshape2)
 
####creation of example data on local directory for uploading####
 
# #load a list of common first names
# faveNames

The app is viewable on glimmer and the code as a gist (626)

To leave a comment for the author, please follow the link and comment on his blog: PremierSoccerStats » R.

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.