# # 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/ # ## Tabs ## ## This is getting pretty packed. We can maybe move some of the ## graphics and tables to a second or third tab, using tabSetPanel() and tabPanel. ## These get used inside fluidpage, and can use the sidebarlayout within a tabPanel, ## ## ## So you can use ## ui <- fluidPage(tabsetPanel( # tabPanel("name", ui, elements), # tabPanel("name2", ui, elements), # tabPanel("name3", ui,elements) #)) ## and add interface elements within each tabPanel(). ## ## ## Instructions. Add a tabset to the ui. ## * Replace the sidebar/main panel with separate tabs so the control elements are on one tab ## * Add several other tabs for each graphical element you think should be separated. ## * Be sure to name each tab Put at least one of the graphics on a second panel. library(plotly) 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 tabsetPanel( tabPanel("Control", 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 tabPanel("output", h1("Histograms"), plotOutput("distPlot"), plotOutput("distPlot2",width=400, height=400) ), tabPanel("ggplot Output", plotOutput("niceplot",width=400, height=400)), tabPanel("plotly output", plotlyOutput("niceplot2",width=800, height=800) ), fluidRow( 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 } ) observeEvent(input$save, { tmp <- summary(data()) print(tmp) write.table(t(as.vector(tmp)),file="logfile.csv",append=T,sep=",", col.names=F) print("writing log file") }) output$distPlot <- renderPlot({ x <- data() bins <- seq(min(x), max(x), length.out = input$bins + 1) par(mfrow=c(1,1)) # draw the histogram with the specified number of bins hist(x, breaks = bins, col = 'gold', border = 'white') }) output$distPlot2 <- renderPlot({ x <- data() par(mfrow=c(1,1)) # draw the histogram with the specified number of bins qqnorm(x) }) output$niceplot <- renderPlot({ x <- data() y <- x + rnorm(length(x)) * 2 tmp <- data.frame(x,y) p <- ggplot(tmp,aes(x=x,y=y)) + geom_point() print(p) }) 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({ x <- data() summary(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)