After a couple weeks of coding and fixing the code, I am happy to say that the app can now update the control charts based on user input. I was so happy when it finally worked! Once again, it was a syntax error, but it was not an obvious one. I tried indexing the column using input$var, but that returns a string, not an integer. Therefore, I used as.numeric(input$var) to get the column index. Also, in the qcc function, I changed the type from "xbar" to "xbar.one." I will look more into why this makes sense, but here's an excerpt from a forum when I had trouble with the Error: group sizes must be larger than one:
"xbar" : Sample means are plotted to control the mean level of a con- tinuous process variable.
and "xbar.one" : Sample values from a one–attime data process to control the mean level of a continuous process variable.
Now, I need to calculate the moving ranges for the control limits and get the app to work on other datasets. The only progress I got with that is getting the selectInput() function to update with the new data set. I am getting an error saying that I am passing a non-numeric argument into a binary operator. I will worry about that tomorrow. I am just happy that I was able to move past a major obstacle.
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
hr(),
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"),
############################
hr(),
helpText("Created by Cameron Lowry, Senior Thesis, 2018")
), #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()
observe({
f <- loadedFile()
if(is.null(f))
return(NULL)
temp <- read.csv(f$datapath)
v <- 1:ncol(temp)
names(v) <- names(temp)
e <- temp[1:nrow(temp), c("CON")]
updateSelectInput(session, 'equip', choices = e)
updateSelectInput(session, 'var', choices = v)
}) #observe UpdateSelectInput
graph <- eventReactive(input$goButton, {
file <-loadedFile()
if(is.null(file))
{
col <- as.integer(input$var)
d <- mydata[mydata$CON %in% input$equip, col ]
}
else
{
d <- read.csv(file$datapath, header = TRUE)
col <- as.integer(input$var)
d <- file[file$CON %in% input$equp, col]
}
mr = 0 #movingRange()
mean = 0
UCL <- mean + 2.66*mr
LCL <- mean - 2.66*mr
spcChart <- qcc(d, type = "xbar.one" ) #qcc
(warn.limits <- limits.xbar(spcChart$center, spcChart$std.dev, spcChart$sizes, 2))
plot(spcChart, add.stats = TRUE, chart.all = TRUE, label.limits = c("LCL", "UCL"), title = "XmR Chart",
xlab = "Increments", ylab = "Values", 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://stackoverflow.com/questions/30495458/server-r-undefined-columns
https://stackoverflow.com/questions/48532858/r-shiny-using-selectinput-as-column-selection-to-subset-data-frame
http://r.789695.n4.nabble.com/Shewhart-Control-Charts-for-Time-Series-data-qcc-package-td4542624.html
https://stackoverflow.com/questions/21515800/subset-a-data-frame-based-on-user-input-shiny?rq=1
http://r.789695.n4.nabble.com/Shewhart-Control-Charts-for-Time-Series-data-qcc-package-td4542624.html
https://stackoverflow.com/questions/38852916/shiny-selecting-specific-columns-from-uploaded-data-frame
Comments
Post a Comment