# # 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/ # ## Introduction to reactivity: ## Notice that so far, everything we did just worked, without thinking too hard about it. But ## we need to think about how R knew to update the graphic when things were changed in the interface. ## Does any change to the input redo the output? If so, that would be unfortunate. ## As it currently stands, there is code within shiny that figures out the dependencies ## between the input and the output ## Right now, the app doesn't know that the bins argument is just 'for display' ## ## Instructions: ## * Get rid of the qqplot. ## * Put a histogram with a second distribution in a second plotOutput, moving vals into the ## plotting function in the server. Have it use the same bins setting as the first. ## * Add a second numeric input to control the samples in the second output histogram. ## * Determine whether everything gets updated or the two independent graphics update ## * independently. ##Once complete, verify that the distribution only gets resampled when samples or distribution is selected, but not bins. ## It is like magic! library(shiny) # 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%'), numericInput("number2","Number of samples for second distribution",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"), plotOutput("distPlot2") ) ) ) # Define server logic required to draw a histogram server <- function(input, output) { 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 <- rnorm(input$number2) # 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)