Creating Summary, Structure, Data & Plot Tabs In The Output Panel Using Reactive Function

Abhijit Jantre

December 20, 2016

Synopsis

Using iris datasaet and shiny library, I have attempted to create a multitab output based on the selection of tab.

Output tabs are as follows - Summary : Displays the summary of the dataset ‘iris’ - Structure : Exhibits the structure of the dataset ‘iris’ - Data : Shows the data for the selected variable in the sidebar panel - Plot : Plot is produced for the selected variable in the sidebar panel

Use of Widgets

Radiobuttons are used to select the colour of the plot

Slider is used to select no. of bins for histogram

Checkboxes are used to select the variable from iris dataset

Ui File Code

library(shiny)
shinyUI(fluidPage(
      titlePanel(title=h4("Iris Dataset", align="center")),
      sidebarLayout(
            sidebarPanel(
                  selectInput("var","1. Select the variable from iris dataset",
                              choices = c("Sepal.Length"= 1,"Sepal.Width"=2,
                                          "Petal.Length" = 3,"Petal.Width"= 4)),
                  br(),
                  sliderInput("bins","Select number of bins for histogram",
                              min=5,max=25,value=15),
                  br(),
                  radioButtons("colour","Selct the colour of the histogram",
                               choices = c("Orange","Blue","Red"),selected = "Orange")
            ),
            mainPanel(
                  tabsetPanel(type="tab",
                              tabPanel("Summary",verbatimTextOutput("summary")),
                              tabPanel("Structure",verbatimTextOutput("str")),
                              tabPanel("Data",tableOutput("data")),
                              tabPanel("Plot",plotOutput("myhist")))
                  
            )
      )
))

Server File Code

library(shiny)

shinyServer(
      function(input,output){
            colm <- reactive({
                  as.numeric(input$var)
            })
            output$summary <- renderPrint({
                  summary(iris)
            })
            
            output$str <- renderPrint({
                  str(iris)
            })
            
            
            output$data <- renderTable({
                  iris[colm()]
            })
            
            output$myhist <- renderPlot({
                  
                  hist(iris[,colm()],breaks = seq(0,max(iris[,colm()]),length=input$bins+1),
                       col = input$colour,main="Histogram of iris Dataset",xlab=names(iris[colm()]))
            })
      }
)