# # 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/ # ## ##Using plotly ## ggplot is fairly easy to integrate within shiny--there is just one thing to remember. ## we just need to create a plotOutput in the UI,and a renderPlot in the server, and put the ## ggplot code in the server function. However, in order for it to work, you need to assign ## your ggplot object a name, and then print the name, like this: ## ## p <- ggplot(data,aes(x=x))+geom_point() ## print(p) ## ## ## Plotly is an interactive graphing function that makes nice graphics akin to ggplot, but for posting online. ## It requires some alternative rendering and output functions. We must use plotlyOutput and renderPlotly instead ## ## Plotly plots don't use ggplot grammar, but it should be easy to create a simple graph (maybe in the console first). ## for example, try ## library(plotly) ## tmp <- data.frame(a = runif(100),b=runif(100)) ## plot_ly(tmp,x= ~a,y= ~b,type="scatter",mode="markers") ## ## ## ## Instructions: ## Create a plotly graph, adding or changing one of the current ggplots. ## Be sure these are installed! library(ggplot2) library(plotly) library(shiny) library(tidyverse) # 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), plotlyOutput("niceplot2",width=500, height=500), 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 } ) summaryTable <- reactive( { a <- data() atab <-summary(as.data.frame(a)) atab } ) observeEvent(input$save, { print("SAVING") # x <- data() # tmp <- as.data.frame(summary(as.data.frame(x))) tmp <- summaryTable() write.table(t(tmp),file="logfile.csv",append=T,sep=",",col.names=F) print("writing log file") }) 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$niceplot2 <- renderPlotly({ x <- data() y <- x + rnorm(length(x)) * 2 tmp <- data.frame(x,y) plot_ly(tmp,x=~x,y=~y,type="scatter",mode="markers") }) output$table<- renderTable({ xx <- summaryTable() as.data.frame(xx) }) 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)