library(shiny)
  library(shinydashboard)
  library(tools)             #to check file extension
  library(dplyr)             #select_if()
  library(readxl)
  
  hist_ui=fluidPage(
    title="hist",
    sidebarLayout(
      sidebarPanel(
        fileInput(inputId = "file_hist", label = "Select Dataset",
                  accept = c("text/csv","text/comma-separated-values,
                              text/plain",".csv",".xlsx"),
                  buttonLabel = "Browse...",
                  placeholder = "No file selected"),
        selectInput(inputId = "hist_var_id",
                    label = "Select variable",
                    choices=""),
        sliderInput(inputId = "bins",label = "Number of bins:", min = 1, 
                    max = 50,value = 30)
        ),
      mainPanel (plotOutput("histogram")) 
    ))
  header = dashboardHeader()
  
  sidebar=dashboardSidebar(
    sidebarMenu(
      id = "tabs",
      menuItem("Graph", tabName = "graph",
               menuSubItem("Histogram", tabName = "histogram")
      )
    )
  )
  
  body=dashboardBody(
    tabItems(
      tabItem("histogram",hist_ui)
    )
  )
  ui = dashboardPage(
    title = "Web App With Shiny",
    header,
    sidebar,
    body
  )
  update_input= function(input_id,label,data){
    return(
      updateSelectInput(
        session = getDefaultReactiveDomain(),
        inputId = input_id,
        label = label,
        choices = names(data()),
        selected = NULL
      ) )
  }
  server=  function(input,output){
    data_hist=reactive({
      req(input$file_hist)
      file_ext= file_ext(input$file_hist$datapath)
      
      if(file_ext=="xlsx"|file_ext=="xls"){
        df=as.data.frame(read_excel(input$file_hist$datapath))
        
      }
      else{
        df = read.csv(input$file_hist$datapath )
        
      }
      return(select_if(df, is.numeric))
    })
    observe(update_input("hist_var_id",label="select variable",data_hist))
    
    output$histogram = renderPlot({
      
      x = data_hist()[,input$hist_var_id]
      # print(x)
      bins = seq(min(x), max(x), length.out = input$bins + 1)
      hist(x,xlab=input$hist_var_id, breaks=bins,col="#75AADB",border = "white",
           main = paste("Histogram of" ,input$hist_var_id))
    }
    )
  }
  shinyApp(ui,server)                

loading...

Key Steps


Previous
Next