# # This is a Shiny web application. You can run the application by clicking # the 'Run App' button above. # # Find out more about building applications with Shiny here: # # http://shiny.rstudio.com/ # ## Instructions: ## Now that we have a reactive data element, let's make another plot and a table, all ## using the same data. As long as the data doesn't get regenerated, we can use interface ## elements to change the graphing. ## * Create an output table that shows the summary statistics of your distribution. Use renderTable. ## * Add a text output that gets set when you change the distribution, so that it says "using x distribution with a mean of x and standard devation of x". Use renderText ## For these two new elements, be sure to add them to your interface, either in the mainPanel or sidebarPanel. library(shiny) library(ggplot2) # Define UI for application that draws a histogram ui <- fluidPage( # Application title titlePanel("Random distribution generation"), # Sidebar with a slider input for number of bins sidebarLayout( sidebarPanel( sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30), selectInput("type",label="Select form of distribution", choices=c("normal","uniform")), numericInput("number","Number of samples",500,min=100,max=10000,step=100,width='100%'), tags$a("Link to wikipedia",href="http://en.wikipedia.org"), tags$p(), tags$hr(), tags$a( tags$img(src="https://upload.wikimedia.org/wikipedia/en/c/c9/Michigan_Technological_University_logo.svg"), href="http://mtu.edu") ), # Show a plot of the generated distribution mainPanel( h1("Histograms"), plotOutput("distPlot",width=350,height=300), plotOutput("distPlot2",width=350, height=300) ) ) ) # Define server logic required to draw a histogram server <- function(input, output) { ##put reactive data element here: data <- reactive( { if( input$type=="normal") { x <- rnorm(input$number) }else if(input$type=="uniform") { x <- runif(input$number) } x } ) output$distPlot <- renderPlot({ n <- input$number if(input$type=="uniform") { x <- runif(n) } else if(input$type=="normal") { x <- rnorm(n) } else{ x <- 1:n } # draw the histogram with the specified number of bins a <- as.data.frame(x) |> ggplot(aes(x=x)) +geom_histogram(bins=input$bins,fill="orange2",color="black") + theme_bw() print(a) }) output$distPlot2 <- renderPlot({ x <- data() # draw the histogram with the specified number of bins a <- as.data.frame(x) |> ggplot(aes(x=x)) +geom_histogram(bins=input$bins,fill="navy",color="black") + theme_bw() print(a) }) } # Run the application shinyApp(ui = ui, server = server)