Trying to Update the Control Chart


   Continuing from last time, I am still trying to figure out how to update the qcc chart based on the variables. What is strange is that I am changing the labels on the chart, but it is not reflecting those changes.

   I have tried updating the user interface with uiOutput, but that did not work. My mentor recommended I use an action button to update the user interface. I am trying that, but it still does not work. This is a unique thesis, but the problem is that I cannot find direct help online. This is literally one-of-a-kind. I have to look at different web applications and reference only the ones that are at least tangentially related to my code. When I finish, I think I might publish my source code so that other people can use it as a reference for their projects.

   Once I succeed in updating the chart, I am going to calculate the moving ranges for my control limits.

   I found out how to get the index of a variable from a list of variables (column names) from the client. I can use this to try to extract a column from the data set at the index. Hopefully that will work. Let's see. I put the method inside a reactive expression because it wouldn't run otherwise.


library(qcc)
library(shiny)
library(DT)



mydata <-read.csv("dummydata.csv")
variables <- 1:ncol(mydata)
names(variables) <- names(mydata)


ui <- fluidPage(
  titlePanel("Control Charts"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose a file in CSV format", accept = c("text/csv", "text/comma-separated-values,text/plain",
                                                                   ".csv")
      ), #fileInput
     
      textInput(inputId = "equipment", label = "Which equipment number are you monitoring?", value = "1234"), #textInput
     
     
      selectInput(inputId = "var", label = "Choose which variable you want to graph", choices = variables), #selectInput
     
      actionButton(inputId = "button", label = "Go") #actionButton
     
     
    ), #sidebar panel
   
   
   
    mainPanel(
   
      verbatimTextOutput("charttitle"),
      plotOutput(outputId = "controlchart"),
      dataTableOutput(outputId = "datachart")
     
     
    ) #main panel
  ) #sidebar layout
) #user interface






server <- function(input, output, session) {
 
 
 
  loadedFile <- reactive({
    file <- input$file1
    file
  }) #loadedFile()
 
 
 
   output$chartitle <- renderText({
     input$equipment
   
   }) #renderText
 
 
   varIndex <- reactive({
     varIndex <- match(input$var, variables)
   }) #find the index of the variable from a vector of variables (column names)
 
   index <- mydata[varIndex]
 
 
 
  output$controlchart <- renderPlot({
   
   
    input$button
   
    if (input$button == 0)
      return()
    isolate({
   
      file <-loadedFile()
   
      if(is.null(file))
      {
        d <-mydata[[varIndex]]
      }
      else
      {
        d <-read.csv(file$datapath)
        d <- d[[varIndex]]
      }
   
      mr = 0
      mean = 0
      UCL <-  mean + 2.66*mr
      LCL <- mean - 2.66*mr
   
   
   
      spcChart <- qcc(d, type = "xbar", title = "Xbar Chart", xlab = "Increment", ylab = "Value") #qcc
   
      (warn.limits <- limits.xbar(spcChart$center, spcChart$std.dev, spcChart$sizes, 2))
      plot(spcChart, restore.par =  FALSE)
   
    }) #isolate
   
  }) #render plot
 

 
  output$datachart <- DT::renderDataTable({
   
    file <-loadedFile()
   
    if(is.null(file))
    {
      d <-mydata
    }
    else
    {
      d <-read.csv(file$datapath)
    }
    d
  }) #render data table
 
 
 
 
 
} #server function

shinyApp(ui = ui, server = server)



Sources Used:

https://shiny.rstudio.com/articles/isolation.html

https://stackoverflow.com/questions/26890465/shiny-r-action-button-controls-reactive-elements

https://www.datamentor.io/r-programming/switch-function

https://tomhopper.me/2014/03/01/individuals-and-moving-range-charts-in-r/

https://gist.github.com/tomhopper/9000495

https://gist.github.com/wch/9606002#file-readme-md

https://stackoverflow.com/questions/28375891/how-can-i-obtain-the-index-of-the-selected-shiny-radiobutton

https://stackoverflow.com/questions/17002160/shiny-tutorial-error-in-r

http://www.r-tutor.com/r-introduction/data-frame/data-frame-column-slice

https://stackoverflow.com/questions/20043313/extract-a-column-from-a-data-table-as-a-vector-by-position

Comments