# # 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/ # ## More on reactivity: ## ## The previous exercise showed that each graph is connected to each of the input elements that it depends on. ## But it is smart enough so it only gets updated when the things it depends on get updated. This seems a bit like ## magic, because we never really told it to do this. Shiny looks through the functions and makes a reactivity ## graph automatically, so that it can handle all of this behind the scenes and not bother us. ## ## One problem with this current approach is that we can't access data from inside one server rendering ## function from another. What if we wanted to make two graphs of some data, or do some other analysis? ## We'd sort of like to have the data live outside the rendering function--but it can't live inside the interface ## description. How can we do this? ## ## Now, we want to move the data generation outside the plotting function, so we can do other things with it. ## This gets a bit complicated. To do this, we need this to be 'reactive', letting the app determine when it should ## be generated automatically. We do this by creating a data value inside the server that is generated by a reactive expression. Any ## time something that it uses is updated, it will itself update; but it won't update if we are just changing something about display ## This can make the app much more efficient as well, as we can effectively save information and let the user focus on just ## how to view it. ## To do this, add what is called a reactive function that essentially turns into a cached data set. ## We do this inside the server definition. The last value returned will be # the value that is accessible by referring to data elsewhere. Notice that we use {} to allow a # block of code to be the argument of the reactive function--this looks a little strange at first. # (uncomment when putting it in the code!) # data <- reactive({ # # # ##code goes here, for example # values <- 1:100 # values # }) # # then, you just need to refer to data elsewhere in the server. The server will create your data and # cache it for later use. You do this as if data is a function: ## x <- data() ## ## ## Now, if the reactive data function depends on something in the input, it will only get regenerated ## when the input changes. And if it changes, only the things that depend on it will change. Notice that if ## multiple things depend on it, it only gets generated once--otherwise it keeps whatever values ## we started with. ## Instructions: ## * Make a single data set as a reactive() ## * Use that data set in the second figure. ## * Verify that: ## - changing bin size does not regenerate the data for the second plot ## - changing bin size regenerates data for the first plot. library(shiny) library(ggplot2) library(gridExtra) # 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("Histogram and normal q-q plot"), plotOutput("distPlot"), plotOutput("distPlot2") ) ) ) # Define server logic required to draw a histogram server <- function(input, output) { data <- reactive( { ##Put data generation here for second plot } ) 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)