Skip to content

Instantly share code, notes, and snippets.

@ashutoshnanda
Last active September 29, 2016 02:33
Show Gist options
  • Save ashutoshnanda/1cdab6ef87a99b21824ea9a1d122d117 to your computer and use it in GitHub Desktop.
Save ashutoshnanda/1cdab6ef87a99b21824ea9a1d122d117 to your computer and use it in GitHub Desktop.
Code from Intro to Shiny (2016 DSSC Hackathon Preparation Session)
library(shiny)
shinyServer(function(input, output) {
data <- reactive({
if(input$distribution_type == 1) {
rnorm(n = 500, mean = input$parameter, sd = 1)
} else {
rpois(n = 500, lambda = input$parameter)
}
})
output$sample_size <- renderPrint({
if(input$distribution_type == 1) {
print("This is a normal distribution\n")
} else {
print("This is a Poisson distribution\n")
}
print(range(data()))
})
output$plot <- renderPlot({
p <- ggplot(mapping = aes(data())) + geom_histogram(fill = "blue") + xlim(c(-5, 20))
if(input$title) {
p + ggtitle("My Title Here")
} else {
p
}
})
})
library(shiny)
library(ggplot2)
shinyUI(
pageWithSidebar(
headerPanel("My First Shiny App!"),
sidebarPanel(
h1("Sidebar"),
sliderInput('parameter',
'Parameter of Distribution',
min = 0.1,
max = 10,
step = 0.05,
value = 5),
selectInput('distribution_type',
'Distribution Type',
c('Normal' = 1, 'Poisson' = 2)),
checkboxInput('title', 'Include Title?')
),
mainPanel(
h1("Main Panel"),
textOutput('sample_size'),
plotOutput('plot')
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment