Skip to content

Instantly share code, notes, and snippets.

@phewson
Created November 27, 2014 11:16
Show Gist options
  • Save phewson/5633f1cd2c984504a082 to your computer and use it in GitHub Desktop.
Save phewson/5633f1cd2c984504a082 to your computer and use it in GitHub Desktop.
OneSample
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
Data <- reactive({
# output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
output$contents <- renderTable({
my.df <- Data()
if (is.null(my.df)){return(NULL)}
my.df})
output$fivenum <- renderTable({
my.df <- Data()
if (is.null(my.df)){return(NULL)}
quantiles <- quantile(my.df[,1], c(0.25, 0.5, 0.975))
mins <- min(my.df[,1])
maxs <- max(my.df[,1])
fivenum <- data.frame(min = mins, lower=quantiles[1],
median=quantiles[2],upper=quantiles[3],
max = maxs)
return(fivenum)
})
output$fivenumlogs <- renderTable({
my.df <- Data()
if (is.null(my.df)){return(NULL)}
if (!is.null(my.df)){
if (min(my.df[,1])>=0){
my.df[,1] <- log(my.df[,1])}}
quantiles <- quantile(my.df[,1], c(0.25, 0.5, 0.975))
mins <- min(my.df[,1])
maxs <- max(my.df[,1])
fivenum <- data.frame(min = mins, lower=quantiles[1],
median=quantiles[2],upper=quantiles[3],
max = maxs)
return(fivenum)
})
output$parametric <- renderTable({
my.df <- Data()
if (is.null(my.df)){return(NULL)}
means <- mean(my.df[,1])
sds <- sd(my.df[,1])
ses<- sd(my.df[,1])/sqrt(length(my.df[,1]))
parametric <- data.frame(mean = c(means[1]), sd=c(sds[1]), se=c(ses[1]))
return(parametric)
})
output$parametriclogs <- renderTable({
my.df <- Data()
if (is.null(my.df)){return(NULL)}
if (!is.null(my.df)){
if (min(my.df[,1])>=0){
my.df[,1] <- log(my.df[,1])}
}
means <- mean(my.df[,1])
sds <- sd(my.df[,1])
ses<- sd(my.df[,1])/sqrt(length(my.df[,1]))
parametric <- data.frame(mean = c(means[1]), sd=c(sds[1]), se=c(ses[1]))
return(parametric)
})
# Expression that generates a histogram. The expression is
# wrapped in a call to renderPlot to indicate that:
#
# 1) It is "reactive" and therefore should re-execute automatically
# when inputs change
# 2) Its output type is a plot
output$distPlot <- renderPlot({
my.df <- Data()
if (is.null(my.df)){return(NULL)}
x <- my.df[,1] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'salmon', border = 'white')
})
output$distPlotlogs <- renderPlot({
my.df <- Data()
if (is.null(my.df)){return(NULL)}
if (min(my.df[,1])>=0){
my.df[,1] <- log(my.df[,1])}
x <- my.df[,1] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$logbins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'brown', border = 'white', main = "Log transformed data")
})
})
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Single Variable"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30),
selectInput("uselogs", "Use logs", c(Yes="Yes", No="No"), "No"),
conditionalPanel(condition = "input.uselogs== 'Yes'",
sliderInput("logbins",
"Number of bins on log plot:",
min = 1,
max = 50,
value = 30))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
p("The min, lower quartile, median, upper quartile and max are:"),
tableOutput('fivenum'),
p("The observed sample statistics were:"),
tableOutput('parametric'),
conditionalPanel(condition = "input.uselogs== 'Yes'",
plotOutput("distPlotlogs"),
p("The min, lower quartile, median, upper quartile and max for the log transformed data are:"),
tableOutput('fivenumlogs'),
p("The observed sample statistics for the log transformed data are:"),
tableOutput('parametriclogs'))
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment