Null Errors and Moving Ranges


   First progress of the day: I was able to create an input where the client can choose which equipment that want to monitor based on Contract Order Number. I took a subset of the "Contract Order Number" column and all the the rows with it. In the dummy data set, there are two CONs :"ABCD" and "DCBA." I am trying to get my chart to graph the increments (Job Order Numbers) within that CON. I am getting an error : 'data' must be of a vector type, was 'NULL'

   It works on the entire data set. I think I am getting a null element in the subset data set. Good news. I am no longer getting a null error. I realized that the data table was taking in the dictionary that my mentor included in the dummy data set.  I removed that and put in in a separate excel sheet, leaving only the important data in, and it worked.

   Yet the graph is still not recognizing the change in inputs. I did not get the null error when I graphed the entire data set, but when I took a subset, the null error came back. I don't think my program is recognizing the indices I used in taking the subset. I sent a copy of my source code to my mentor so that she can help me figure out the problem.

   In the mean time, I decided to create a user-defined function to calculate the moving ranges of the data. The moving range is the absolute value of the average difference between consecutive points. I am creating the function outside the user interface and server function. I am getting an argument "data" is missing, with no default error in that function.


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



mydata <-read.csv("dummydata.csv")
variables <- 1:ncol(mydata)
names(variables) <- names(mydata)
equipment <- mydata[1:nrow(mydata), c("CON")]



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
     
      selectInput(inputId = "equip", label = "Contract Order Number:", choices = equipment), #selectInput
     
     
      selectInput(inputId = "var", label = "Choose which variable you want to graph", choices = variables), #selectInput
     
     
     
      ############################
     
      #Added by A. Coleman 20180225
     
      br(),
     
      actionButton("goButton", "Go!"),
     
      p("Update your variables")
     
      ############################
     
    ), #sidebar panel
   
   
   
    mainPanel(
     
      plotOutput(outputId = "controlchart"),
      dataTableOutput(outputId = "datachart")
     
     
    ) #main panel
  ) #sidebar layout
) #user interface


#movingRange <- function(data){
 
  #mRange <- vector()
 
 #if (!is.null(data))
 #{
  # for (num in data[C(2:nrow(data)),])
  #{
  #  difference <- data[num] - data[num - 1]
   #  mRange <- c(mRange, difference)
   
 # }
   
  #mRange <- sum(mRange, na.rm = FALSE)
 # mRange <- mRange / length(mRange)
  #mRange <- abs(mRange)
 
  #return(mRange)
 #}
 
 # else
  #{
  #  return(0)
 # }
#} #movingRange() function


server <- function(input, output, session) {
 
 
 
  loadedFile <- reactive({
    file <- input$file1
    file
  }) #loadedFile()
 
 
 
   graph <- eventReactive(input$goButton, {
   
     file <-loadedFile()
   
     if(is.null(file))
     {
       d <-mydata[input$equip, input$var]
     }
     else
     {
       d <-read.csv(file$datapath)
       d <- mydata[input$equip, input$var]
     }
   
     mr = 0 #movingRange()
     mean = 0
     UCL <-  mean + 2.66*mr
     LCL <- mean - 2.66*mr
   
     increments <- d[c(input$equip), c("JON")]
   
     spcChart <- qcc(increments, type = "xbar", title = "Xbar Chart", xlab = "Increments", ylab = "Value" ) #qcc
   
     (warn.limits <- limits.xbar(spcChart$center, spcChart$std.dev, spcChart$sizes, 2))
     plot(spcChart, restore.par =  FALSE)
   
   
   })#eventReactive
 
 
  output$controlchart <- renderPlot({
   
    graph()
   
  }) #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/gallery/actionbutton-demo.html

https://www.r-bloggers.com/taking-a-subset-of-a-data-frame-in-r/

http://stat.ethz.ch/R-manual/R-devel/library/base/html/sum.html

https://www.programcreek.com/2014/01/initialize-an-empty-vector-in-r/

Comments