# # 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/ # ##Adding a save button. ## Suppose we have found a data set we like, and want to use/save elsewhere. For a complex app, this could be used ## to record user feedback or something. Note that because you are allowing an external user to write things to your ## server space, this involves some possible risks--they could try to fill up your computer's memory and crash your system. ## ## ## To do this, we have to consider a couple things--what is it we want to save? How do we want to save it? And ## should we try to simply 'log' information to an existing file, or completely overwrite things. ## ## for the moment, let's say we want to create a logging file that simply adds the information in the summary table column freq to an ## existing .csv file. This is pretty straightforward to do with the append=T argument of write.csv. ## ## To do this, we need to: ## 1. create an action button in the interface. (this is already done, using actionButton()). ## 2. create code that responds to an update in the action button (using ObserveEvent() in the server, have the first argument refer to the ## button label, and the second argument a code block specified in {} that is the code you want to execute. ## 3. Have this code write a file consisting of the summary of the data you output in the previous tutorial. You can recalculate ## the summary within this ObserveEvent code. ## 4. Challenge: make the summary table reactive, so that the table gets calculated as its own reactive data, and then this table gets ## used both to display the table and to save the table out. 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"), plotOutput("distPlot2",width=400, height=400), actionButton("save","Save table to log file"), tableOutput("table"), h3("Overview in text:"), textOutput("textdesc") ) ) ) server <- function(input, output,session) { data <- reactive( { if( input$type=="normal") { x <- rnorm(input$number) updateNumericInput(session,"bins",value=25) }else if(input$type=="uniform") { x <- runif(input$number) updateNumericInput(session,"bins",value=10) } x } ) output$distPlot <- 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="orange2",color="black") + theme_bw() print(a) }) output$distPlot2 <- renderPlot({ x <- data() # draw the histogram with the specified number of bins b <- as.data.frame(x) |> ggplot(aes(sample=x)) + geom_qq() + theme_bw() print(b) }) output$table<- renderTable({ x <- data() as.data.frame(summary(as.data.frame(x))) }) output$textdesc <- renderText( paste("using ",input$type," distribution with a mean of ", round(mean(data()),3), " and standard devation of",round(sd(data()),3),"\n") ) } # Run the application shinyApp(ui = ui, server = server)