Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Last active April 29, 2016 13:58
Show Gist options
  • Save aagarw30/6a4b5dc685a3aa5a40054297d1140036 to your computer and use it in GitHub Desktop.
Save aagarw30/6a4b5dc685a3aa5a40054297d1140036 to your computer and use it in GitHub Desktop.
Create a frequency distribution
library(shiny)
shinyServer(function(input, output) {
# Split the elements input from the user
v <- reactive({
as.numeric(unlist(strsplit(input$vec1,",")))
})
# Print a message just before printing the vector values
output$text1 <- renderText({
paste("Ustedes entrado los", length(v()), "valores", sep=" ")
})
# Print the numbers added by the user
output$vectors<-renderText({
v()
})
# Calculate the range
range <- reactive({
max(v()) - min(v())
})
output$text2 <- renderText({
paste("El Rango (R) este", range(), sep=" ")
})
# Calculate the number of classes by Sturges method
nclass <- reactive({
round(1 + 3.322*log10(length(v())))
})
# Calculate the interval size
size <- reactive({
round(range()/nclass(),1)
})
output$text3 <- renderText({
paste("Numero de intervalos de clase (m) por Sturges rule este", nclass(), sep=" ")
})
output$text4 <- renderText({
paste("El amplitud (c) este", size(), sep="\n")
})
# Get the intervals by using cut()
intervals <- reactive({
cut(v(), breaks=seq(min(v()) - 0.05, max(v()) + 0.05, by=size()), right = FALSE)
})
output$text5 <- renderText({
paste(levels(intervals()))
})
# Construct the frequency table
output$table <- renderTable({
Rendimientos = levels(intervals())
ni = table(intervals()) # Absolute frequency
hi=prop.table(ni) # Relative Freq
Ni= cumsum(ni) # Cummulative Frequency
Hi=cumsum(hi)
data.frame(cbind(Rendimientos, ni, hi, Ni, Hi), row.names = NULL)
})
# construct the histogram
output$hist <- renderPlot({
hist(v(), breaks=seq(min(v()) - 0.05, max(v()) + 0.05, by=size()), labels = TRUE, col = "orange",
axes = FALSE, xlab = "Rendimientos", ylab = "Frecuencias", main = "Histogram de frecuencias absoluta")
axis(side=1, at = seq(min(v())- 0.05, max(v())+0.05, by=size()))
axis(side = 2, labels = TRUE)
}
)
})
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Frequency Distribution en shiny"),
sidebarPanel(
textInput('vec1','Introducir un vector (delimitado por comas)' ,"3.9,3.7,5.8,5.0,4.8,4.4,5.6,7.0,5.6,5.1,3.6,6.8,5.6,3.4,7.0,4.8,2.6,2.7,4.0,4.8")
),
mainPanel(
textOutput("text1"),
textOutput("vectors"),
br(),
textOutput("text2"),
br(),
textOutput("text3"),
br(),
textOutput("text4"),
br(),
h5("los rendimientos"),
textOutput("text5"),
br(),
h5("Tabla de distribucion de frecuencias de los rendimientos de una plantacion de hortalizas"),
tableOutput("table"),
br(),
plotOutput("hist")
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment