show_modal_spinner disappears instantly before generating outputs in Rshiny - r

I'm trying to use show_modal_spinner to to display a text message while the model runs to generate outputs, I'm using PLS-PM in single obsereveEvent function but the showModal popup flashes for a second and disappears while the observe event function is still running, I'm getting all the results from this event but the Modal spinner vanishes as soon as I click the run button. Below is the observe event function I'm using. Please help in in debugging this code.
#------------------PLSPM Analysis Function------------------------
observeEvent({input$actionButton_PLSPM_analysis}, {
show_modal_spinner(
spin = "cube-grid",
color = "firebrick",
text = "Please wait..."
)
PLSPM_result_data_sym <- reactive({
readData(exps=input$PLSPM_ProtocolSelection, crop=input$PLSPM_CropSelection, country=input$PLSPM_CountrySelection, sym = input$PLSPM_TreatmentSelection)
})
PLSPM_Model_Analysis <- reactive({run_PLSPM_Analysis(PLSPM_result_data_sym(), input$PLSPM_CropSelection)})
PLSPM_summary <- reactive({(PLSPM_Model_Analysis()$summary)})
PLSPM_inner_model <- reactive({innerplot(PLSPM_Model_Analysis()$model, colpos = "#6890c4BB", colneg = "#f9675dBB",
box.prop = 0.55, box.size = 0.08, box.cex = 1,
box.col = "gray95", lcol = "black", box.lwd = 2,
txt.col = "black", shadow.size = 0, curve = 0,
lwd = 3, arr.pos = 0.5, arr.width = 0.2, arr.lwd = 3,
cex.txt = 0.9)})
PLSPM_Weight_plot <- reactive({outerplot(PLSPM_Model_Analysis()$model, colpos = "#6890c4BB", colneg = "#f9675dBB",
box.prop = 0.55, box.size = 0.08, box.cex = 1,
box.col = "gray95", lcol = "black", box.lwd = 2,
txt.col = "black", shadow.size = 0, curve = 0,
lwd = 3, arr.pos = 0.5, arr.width = 0.2, arr.lwd = 3,
cex.txt = 0.9)})
PLSPM_Loading_plot <- reactive({outerplot(PLSPM_Model_Analysis()$model, colpos = "#6890c4BB", colneg = "#f9675dBB",
box.prop = 0.55, box.size = 0.08, box.cex = 1,
box.col = "gray95", lcol = "black", box.lwd = 2,
txt.col = "black", shadow.size = 0, curve = 0,
lwd = 3, arr.pos = 0.5, arr.width = 0.2, arr.lwd = 3,
cex.txt = 0.9)})
mydf_inner_model <- reactive({as.data.frame(PLSPM_summary()$inner_model$pyield)})
mydf_outer_model <- reactive({as.data.frame(PLSPM_summary()$outer_model)})
output$data_table_inner_model <- renderDataTable({
datatable(mydf_inner_model(),options = list(
scrollX = TRUE))
})
output$data_table_outer_model <- renderDataTable({
datatable(mydf_outer_model(),options = list(
scrollX = TRUE))
})
output$plot_PLSPM_inner_model <- renderPlot({
(PLSPM_inner_model())
})
output$plot_PLSPM_Weight_plot <- renderPlot({
(PLSPM_Weight_plot())
})
output$plot_PLSPM_Loading_plot <- renderPlot({
(PLSPM_Loading_plot())
})
remove_modal_spinner()
})

That's because you define reactive inside an observeEvent. When you use PLSPM_result_data_sym <- reactive(...) it does not do the calculation, it is simply registered to be done later (when you call PLSPM_result_data_sym()). Instead you can use reactiveValues like this (and put the output outside the observeEvent too):
function(input, output, session) {
rv <- reactiveValues()
observeEvent({
input$actionButton_PLSPM_analysis
}, {
show_modal_spinner(spin = "cube-grid",
color = "firebrick",
text = "Please wait...")
rv$PLSPM_result_data_sym <- readData(
exps = input$PLSPM_ProtocolSelection,
crop = input$PLSPM_CropSelection,
country = input$PLSPM_CountrySelection,
sym = input$PLSPM_TreatmentSelection
)
rv$PLSPM_Model_Analysis <-run_PLSPM_Analysis(rv$PLSPM_result_data_sym, input$PLSPM_CropSelection)
rv$PLSPM_summary <- rv$PLSPM_Model_Analysis$summary
rv$PLSPM_inner_model <- innerplot(
rv$PLSPM_Model_Analysis$model,
colpos = "#6890c4BB",
colneg = "#f9675dBB",
box.prop = 0.55,
box.size = 0.08,
box.cex = 1,
box.col = "gray95",
lcol = "black",
box.lwd = 2,
txt.col = "black",
shadow.size = 0,
curve = 0,
lwd = 3,
arr.pos = 0.5,
arr.width = 0.2,
arr.lwd = 3,
cex.txt = 0.9
)
rv$PLSPM_Weight_plot <-
outerplot(
rv$PLSPM_Model_Analysis$model,
colpos = "#6890c4BB",
colneg = "#f9675dBB",
box.prop = 0.55,
box.size = 0.08,
box.cex = 1,
box.col = "gray95",
lcol = "black",
box.lwd = 2,
txt.col = "black",
shadow.size = 0,
curve = 0,
lwd = 3,
arr.pos = 0.5,
arr.width = 0.2,
arr.lwd = 3,
cex.txt = 0.9
)
rv$PLSPM_Loading_plot <-
outerplot(
rv$PLSPM_Model_Analysis$model,
colpos = "#6890c4BB",
colneg = "#f9675dBB",
box.prop = 0.55,
box.size = 0.08,
box.cex = 1,
box.col = "gray95",
lcol = "black",
box.lwd = 2,
txt.col = "black",
shadow.size = 0,
curve = 0,
lwd = 3,
arr.pos = 0.5,
arr.width = 0.2,
arr.lwd = 3,
cex.txt = 0.9
)
rv$mydf_inner_model <- as.data.frame(rv$PLSPM_summary$inner_model$pyield)
rv$mydf_outer_model <- as.data.frame(rv$PLSPM_summary$outer_model)
remove_modal_spinner()
})
output$data_table_inner_model <- renderDataTable({
datatable(rv$mydf_inner_model, options = list(scrollX = TRUE))
})
output$data_table_outer_model <- renderDataTable({
datatable(rv$mydf_outer_model, options = list(scrollX = TRUE))
})
output$plot_PLSPM_inner_model <- renderPlot({
rv$PLSPM_inner_model
})
output$plot_PLSPM_Weight_plot <- renderPlot({
rv$PLSPM_Weight_plot
})
output$plot_PLSPM_Loading_plot <- renderPlot({
rv$PLSPM_Loading_plot
})
}

You have not used session = shiny::getDefaultReactiveDomain() argument in both remove_modal_spinner() and show_modal_spinner(). Try this
observeEvent({input$actionButton_PLSPM_analysis}, {
show_modal_spinner(
spin = "cube-grid",
color = "firebrick",
text = "Please wait...",
session = shiny::getDefaultReactiveDomain()
)
## other computations here
remove_modal_spinner(session = shiny::getDefaultReactiveDomain())
})

Related

how to save a plot map on .tiff format on R

I am plotting an image with this code but I am having some trouble when I Try to save it.
library(markovchain)
library(expm)
library(diagram)
library(pracma)
stateNames <- c("Fischer", "Lewis", "Medium")
q0 <- new("markovchain", states = stateNames,
transitionMatrix =matrix(c(0.08,0.15,0.77,
0.25,0.13,0.62,
0.16,0.18,0.66),
byrow = TRUE, nrow = 3), name = "state t0")
q0_p <- matrix(c(0.08,0.15,0.77,
0.25,0.13,0.62,
0.16,0.18,0.66),
byrow = TRUE, nrow = 3)
row.names(q0_p) <- stateNames; colnames(q0_p) <- stateNames
plotmat(q0_p,pos = c(1,2),
lwd = 1, box.lwd = 1,
cex.txt = 0.8,
box.size = 0.1,
box.type = "circle",
box.prop = 0.7,
box.col = c("#F8766D", "#00BA38","#619CFF"),
shadow.size = 0,
arr.length=.2,
arr.width=.1,
self.cex =.4,
self.shifty = -.01,
self.shiftx = .13,
main = "")
tiff(file="plotq0_3.tiff",width=15,height=10,units="cm",res=300)
but when I go to the file, it weight 6128 kb but I can't see the image. Here is a copy.

problem in exporting pdf of heatmap from R

I am trying to export the heatmap using:
pdf("y2.pdf", width = 7, height = 20);
textMatrix = paste(signif(moduleTraitCor, 2), "\n(",
signif(moduleTraitPvalue, 1), ")", sep = "");
dim(textMatrix) = dim(moduleTraitCor)
sizeGrWindow(9,7)
par(mar = c(8, 10, 3, 3));
labeledHeatmap(Matrix = moduleTraitCor,
xLabels = colnames(datTraits),
yLabels = names(MEs),
ySymbols = names(MEs),
colorLabels = FALSE,
cex.lab = NULL,
cex.lab.x = 0.6,
cex.lab.y = 0.3,
xColorWidth = 0.01,
yColorWidth = 0.1,
colors = greenWhiteRed(83),
textMatrix = textMatrix,
setStdMargins = FALSE,
cex.text = 0.2,
zlim = c(-1,1),
margins =c(12,9),
main = paste("Module-trait_relationships"))
dev.off()
However, the exported y2.pdf file does not open. Can anyone help me finding what is going wrong. Thank you!

Implementing ggvis with shiny and circlize

I'm trying to understand how ggvis works in the context of shiny and it's been a real headache. At this point I'm just trying to make something, anything interactive. Ideally I would like to be able to filter data points with sliders and be able to click on sectors and links to zoom and highlight respectively.
Ignoring the entire right bar, how would I be able to implement ggvis?
server.r
options(shiny.maxRequestSize=60*1024^2)
# Option to use scientific notation
options(scipen=999)
library(ggplot2)
library(ggvis)
shinyServer(function(input, output) {
inputData <- try(reactive({
inFile <- input$file1
if(is.null(inFile$datapath)){
return(iris)
}
newData <- read.csv(inFile$datapath, fill=TRUE)
newData
}))
output$choose_histVar <- renderUI({
newData <- inputData()
nameDataNew1<-c("ALL" ,"Earmarks", "Free-Cash")
if(class(nameDataNew1)!="try-error"){
selectInput("histVar", "1. Select Funding", as.list(nameDataNew1),
multiple = FALSE)
}
else{
selectInput("histVar", "1.Select Funding", NULL, multiple = FALSE)
}
})
# Use renderTable() function to render a table
output$summaryTable <- renderTable({ summary( try(inputData()) ) })
output$plot.hist <- renderPlot({
plotHistograms(data=try(inputData()), getCol=input$histVar,
getBin=input$bins)
})
output$plot.bar <- renderPlot({ plotcir(data)})
})
plotcir <- function(data) {
set.seed(999)
n = 1000
df = data.frame(factors = sample(letters[1:8], n, replace = TRUE),
x = rnorm(n), y = runif(n))
data.temp <- as.data.frame(df)
circos.par("track.height" = 0.1)
circos.initialize(factors = df$factors, x = df$x)
circos.track(factors = df$factors, y = df$y,
panel.fun = function(x, y) {
circos.text(CELL_META$xcenter, CELL_META$cell.ylim[2] + uy(5,
"mm"),
CELL_META$sector.index)
circos.axis(labels.cex = 0.6)
})
col = rep(c("#FF0000", "#00FF00"), 4)
circos.trackPoints(df$factors, df$x, df$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "text", sector.index = "a", track.index = 1)
bgcol = rep(c("#EFEFEF", "#CCCCCC"), 4)
circos.trackHist(df$factors, df$x, bin.size = 0.2, bg.col = bgcol, col = NA)
circos.track(factors = df$factors, x = df$x, y = df$y,
panel.fun = function(x, y) {
ind = sample(length(x), 10)
x2 = x[ind]
y2 = y[ind]
od = order(x2)
circos.lines(x2[od], y2[od])
})
##vis <- reactive({})
circos.link("a", 0, "b", 0, h = 0.4)
circos.link("c", c(-0.5, 0.5), "d", c(-0.5,0.5), col = "red",
border = "blue", h = 0.2)
circos.link("e", 0, "g", c(-1,1), col = "green", border = "black", lwd = 2,
lty = 2)
}
ui.r
# Load libraries used in this Shiny App
library(shiny)
library(ggplot2)
library(circlize)
library(ggvis)
library(shinythemes)
shinyUI(fluidPage(
titlePanel(title = h2("The Wall", align="center")),
theme = shinytheme("cyborg"),
sidebarPanel(
fileInput('file1', 'The default dataset is df data. You may choose your own
CSV file'),
sliderInput('file1', 'Mission 1', value = 10, min = 0, max = 100, step = 1,
post = "%"),
sliderInput('file1', 'Mission 2', value = 0, min = 0, max = 100, step = 1,
post = "%"),
sliderInput('file1', 'Mission 3', value = 0, min = 0, max = 100, step = 1,
post = "%"),
sliderInput('file1', 'Mission 4', value = 0, min = 0, max = 100, step = 1,
post = "%"),
sliderInput('file1', 'Mission 5', value = 0, min = 0, max = 100, step = 1,
post = "%"),
uiOutput("choose_histVar"),
uiOutput("choose_xVar"),
uiOutput("choose_yVar"),
uiOutput("choose_cateVar"),
uiOutput("choose_barVar"),
p()
),
mainPanel(
h3('DOS - Augmented decisions'),
tabsetPanel(type="tab",
tabPanel( "Optimal",
plotOutput('plot.bar')
),
tabPanel("Histogram",
h4(checkboxInput("showHideHistograms", "Show/hide histograms",
value=FALSE)),
# Add a conditional panel to plot the histogram only when "Show
histogram" is checked
conditionalPanel(
condition = "input.showHideHistograms",
# Use plotOutput function to plot the output visualization
plotOutput('plot.hist')
)
)
),
p('')
)
))

Interactive R Shiny plot with and without using ggplot

I modified the interactive R Shiny plot from the R Shiny gallery to plot an interactive standard curve. I would like to plot the interactive plot without using ggplot2 library with just using R base plotting functions.
library(ggplot2)
XYdata <- data.frame(cbind(Values = c(91.8, 95.3, 99.8, 123.3, 202.9, 619.8, 1214.2, 1519.1, 1509.2, 1523.3, 1595.2, 1625.1),
Concn = c(1000, 300, 100, 30, 10, 3, 1, 0.3, 0.1, 0.03, 0.01, 0)))
ui <- fluidPage(
fluidRow(
column(width = 6,
plotOutput("plot1", height = 350,
click = "plot1_click",
brush = brushOpts(
id = "plot1_brush"
)
),
actionButton("exclude_toggle", "Toggle points"),
actionButton("exclude_reset", "Reset")
)
)
)
server <- function(input, output) {
# For storing which rows have been excluded
vals <- reactiveValues(
keeprows = rep(TRUE, nrow(XYdata))
)
NonScientific <- function(l) {l <- format(l, scientific = FALSE); parse(text=l)}
output$plot1 <- renderPlot({
# Plot the kept and excluded points as two separate data sets
XYdata <- data.frame(cbind(Values = c(91.8, 95.3, 99.8, 123.3, 202.9, 619.8, 1214.2, 1519.1, 1509.2, 1523.3, 1595.2, 1625.1),
Concn = c(1000, 300, 100, 30, 10, 3, 1, 0.3, 0.1, 0.03, 0.01, 0)))
keep <- XYdata[ vals$keeprows, , drop = FALSE]
exclude <- XYdata[!vals$keeprows, , drop = FALSE]
keep <- subset(keep, Concn > 0)
exclude <- subset(exclude, Concn > 0)
nls.fit <- nls(Values ~ (ymax* keep$Concn / (ec50 + keep$Concn)) + Ns*keep$Concn + ymin, data=keep,
start=list(ymax=max(keep$Values), ymin = min(keep$Values), ec50 = 3, Ns = 0.2045514))
keep$nls.pred <- fitted(nls.fit)
ggplot(keep, aes(y = Values,x = Concn))+geom_point(size = 5,colour="red")+
geom_smooth(method = "loess",fullrange = F, se = T, aes(Concn, nls.pred),size = 1.5,colour="blue1")+
geom_point(data = exclude, shape = 21, fill = NA, color = "black",size = 5, alpha = 0.7) +
xlab('Concentration (nM)')+ ylab('Units')+
scale_x_log10(labels=NonScientific)+ggtitle("Standard Curve")+theme_classic()+
theme(panel.background = element_rect(colour = "black", size=1),
plot.margin = margin(1, 3, 0.5, 1, "cm"),
plot.title = element_text(hjust = 0, face="bold",color="#993333", size=16),
axis.title = element_text(face="bold", color="#993333", size=14),
axis.text.x = element_text(face="bold", color="#666666", size=12),
axis.text.y = element_text(face="bold", color="#666666", size=12))
})
# Toggle points that are clicked
observeEvent(input$plot1_click, {
res <- nearPoints(XYdata, input$plot1_click, allRows = TRUE)
vals$keeprows <- xor(vals$keeprows, res$selected_)
})
# Toggle points that are brushed, when button is clicked
observeEvent(input$exclude_toggle, {
res <- brushedPoints(XYdata, input$plot1_brush, allRows = TRUE)
vals$keeprows <- xor(vals$keeprows, res$selected_)
})
# Reset all points
observeEvent(input$exclude_reset, {
vals$keeprows <- rep(TRUE, nrow(XYdata))
})
}
shinyApp(ui, server)
I tried replacing the plotting portion of the script with the following but I am not able to interactively plot. What am I doing wrong here?
plot(Values ~ Concn, keep, subset = Concn > 0, col = 4, cex = 2, log = "x")
title(main = "XY Std curve")
lines(predict(nls.fit, new = list(Concn = Concn)) ~ Concn, keep)
points(Values ~ Concn, exclude, subset = Concn > 0, col = 1, cex = 2, log = "x")
You have to add xvarand yvar parameters to nearPoints:
res <- nearPoints(XYdata, input$plot1_click, xvar="Concn", yvar="Values", allRows = TRUE)
The working code implementing #HubertL's suggestion for someone like me to use for interactive plotting and to knockout outliers by clicking on or by selecting the point(s) using mouse:
XYdata <- data.frame(cbind(Values = c(91.8, 95.3, 99.8, 123.3, 202.9, 619.8, 1214.2, 1519.1, 1509.2, 1523.3, 1595.2, 1625.1),
Concn = c(1000, 300, 100,30, 10, 3, 1, 0.3, 0.1, 0.03, 0.01, 0)))
ui <- fluidPage(
fluidRow(
column(width = 6,
plotOutput("plot1", height = 350,click = "plot1_click", brush = brushOpts(id = "plot1_brush")),
actionButton("exclude_reset", "Reset")
)
)
)
server <- function(input, output) {
# For storing which rows have been excluded
vals <- reactiveValues(
keeprows = rep(TRUE, nrow(XYdata))
)
NonScientific <- function(l) {l <- format(l, scientific = FALSE); parse(text=l)}
output$plot1 <- renderPlot({
# Plot the kept and excluded points as two separate data sets
XYdata <- data.frame(cbind(Values = c(91.8, 95.3, 99.8, 123.3, 202.9, 619.8, 1214.2, 1519.1, 1509.2, 1523.3, 1595.2, 1625.1),
Concn = c(1000, 300, 100, 30, 10, 3, 1, 0.3, 0.1, 0.03, 0.01, 0)))
keep <- XYdata[ vals$keeprows, , drop = FALSE]
exclude <- XYdata[!vals$keeprows, , drop = FALSE]
keep <- subset(keep, Concn > 0)
exclude <- subset(exclude, Concn > 0)
o <- order(keep$Concn)
keep <- keep[o, ]
fo <- Values ~ (ymax* Concn / (ec50 + Concn)) + Ns * Concn + ymin
st <- list(ymax=max(keep$Values), ymin = min(keep$Values), ec50 = 3, Ns = 0.2045514)
nls.fit <- nls(fo, data = keep, start = st)
plot(Values ~ Concn, keep, subset = Concn > 0, type = 'p',pch = 16,cex = 2, axes = FALSE, frame.plot = TRUE,log = "x")
title(main = "Interactive Std curve")
logRange <- with(keep, log(range(Concn[Concn > 0])))
x <- exp(seq(logRange[1], logRange[2], length = 250))
lines(x, predict(nls.fit, new = list(Concn = x)))
points(Values ~ Concn, exclude, subset = Concn > 0, col = 1, cex = 2)
my.at <- 10^(-2:3)
axis(1, at = my.at, labels = formatC(my.at, format = "fg"))
axis(2)
})
# Toggle points that are clicked
observeEvent(input$plot1_click, {
res <- nearPoints(XYdata, input$plot1_click, xvar="Concn", yvar="Values", allRows = TRUE)
vals$keeprows <- xor(vals$keeprows, res$selected_)
})
# Reset all points
observeEvent(input$exclude_reset, {
vals$keeprows <- rep(TRUE, nrow(XYdata))
})
}
shinyApp(ui, server)

Remove Control Limits With qcc Package in R (Quality Control Charts)

I need to remove the lower control limit and center line (and their labels) from my control chart.
Here's the code:
# install.packages('qcc')
library(qcc)
nonconforming <- c(3, 4, 6, 5, 2, 8, 9, 4, 2, 6, 4, 8, 0, 7, 20, 6, 1, 5, 7)
samplesize <- rep(50, 19)
control <- qcc(nonconforming, type = "p", samplesize, plot = "FALSE")
warn.limits <- limits.p(control$center, control$std.dev, control$sizes, 2)
par(mar = c(5, 3, 1, 3), bg = "blue")
plot(control, restore.par = FALSE, title = "P Chart for Medical Insurance Claims",
xlab = "Day", ylab = "Proportion Defective")
abline(h = warn.limits, lty = 3, col = "blue")
v2 <- c("LWL", "UWL") # the labels for warn.limits
mtext(side = 4, text = v2, at = warn.limits, col = "blue", las = 2)
This approach seems more like a "hack" than an answer and it throws a warning:
control$center <- NULL
control$limits <- NULL
plot(control, add.stats = FALSE)
Not a QC expert by any means but would this work for you? Looking at the qcc function it seems to control what needs to be plotted, so what i've done here is manipulate the limits of the LCL and CENTRE lines. I then changed the plot function to plot between some y limits which does not cover the -1 value. The description unfortunately reflects the manipulated limit values of -1.
control$limits[1] <- -1
control$center <- -1
plot(control, restore.par = FALSE, title = "P Chart for Medical Insurance Claims",
xlab = "Day", ylab = "Proportion Defective", ylim=c(0.0,0.4))
The following function will do the required chart, and you don't need to change your control object, neither to know the control's limits. Load the function, then just call:
plot.qcc2(control, restore.par = FALSE, title = "P Chart for Medical Insurance Claims", + xlab = "Day", ylab = "Proportion Defective")
Function:
#Function plotting only UCL:
plot.qcc2 <- function (x, add.stats = TRUE, chart.all = TRUE, label.limits = c( "UCL"), title, xlab, ylab, ylim, axes.las = 0, digits = getOption("digits"),
restore.par = TRUE, ...)
{
object <- x
if ((missing(object)) | (!inherits(object, "qcc")))
stop("an object of class `qcc' is required")
type <- object$type
std.dev <- object$std.dev
data.name <- object$data.name
center <- object$center
stats <- object$statistics
limits <- object$limits
lcl <- limits[, 1]
ucl <- limits[, 2]
newstats <- object$newstats
newdata.name <- object$newdata.name
violations <- object$violations
if (chart.all) {
statistics <- c(stats, newstats)
indices <- 1:length(statistics)
}
else {
if (is.null(newstats)) {
statistics <- stats
indices <- 1:length(statistics)
}
else {
statistics <- newstats
indices <- seq(length(stats) + 1, length(stats) +
length(newstats))
}
}
if (missing(title)) {
if (is.null(newstats))
main.title <- paste(type, "Chart\nfor", data.name)
else if (chart.all)
main.title <- paste(type, "Chart\nfor", data.name,
"and", newdata.name)
else main.title <- paste(type, "Chart\nfor", newdata.name)
}
else main.title <- paste(title)
oldpar <- par(bg = qcc.options("bg.margin"), cex = qcc.options("cex"),
mar = if (add.stats)
pmax(par("mar"), c(8.5, 0, 0, 0))
else par("mar"), no.readonly = TRUE)
if (restore.par)
on.exit(par(oldpar))
plot(indices, statistics, type = "n", ylim = if (!missing(ylim))
ylim
else range(statistics, limits, center), ylab = if (missing(ylab))
"Group summary statistics"
else ylab, xlab = if (missing(xlab))
"Group"
else xlab, axes = FALSE, main = main.title)
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
col = qcc.options("bg.figure"))
axis(1, at = indices, las = axes.las, labels = if (is.null(names(statistics)))
as.character(indices)
else names(statistics))
axis(2, las = axes.las)
box()
lines(indices, statistics, type = "b", pch = 20)
if (length(center) == 1)
alpha <- 1
else lines(indices, c(center, center[length(center)]), type = "s")
if (length(lcl) == 1) {
abline(h = ucl, lty = 2)
}
else {
lines(indices, ucl[indices], type = "s", lty = 2)
}
mtext(label.limits, side = 4, at = c(rev(ucl)[1],rev(ucl)[1]),
las = 1, line = 0.1, col = gray(0.3))
if (is.null(qcc.options("violating.runs")))
stop(".qcc.options$violating.runs undefined. See help(qcc.options).")
if (length(violations$violating.runs)) {
v <- violations$violating.runs
if (!chart.all & !is.null(newstats)) {
v <- v - length(stats)
v <- v[v > 0]
}
points(indices[v], statistics[v], col = qcc.options("violating.runs")$col,
pch = qcc.options("violating.runs")$pch)
}
if (is.null(qcc.options("beyond.limits")))
stop(".qcc.options$beyond.limits undefined. See help(qcc.options).")
if (length(violations$beyond.limits)) {
v <- violations$beyond.limits
if (!chart.all & !is.null(newstats)) {
v <- v - length(stats)
v <- v[v > 0]
}
points(indices[v], statistics[v], col = qcc.options("beyond.limits")$col,
pch = qcc.options("beyond.limits")$pch)
}
if (chart.all & (!is.null(newstats))) {
len.obj.stats <- length(object$statistics)
len.new.stats <- length(statistics) - len.obj.stats
abline(v = len.obj.stats + 0.5, lty = 3)
mtext(paste("Calibration data in", data.name), at = len.obj.stats/2,
adj = 0.5, cex = 0.8)
mtext(paste("New data in", object$newdata.name), at = len.obj.stats +
len.new.stats/2, adj = 0.5, cex = 0.8)
}
if (add.stats) {
plt <- par()$plt
usr <- par()$usr
px <- diff(usr[1:2])/diff(plt[1:2])
xfig <- c(usr[1] - px * plt[1], usr[2] + px * (1 - plt[2]))
at.col <- xfig[1] + diff(xfig[1:2]) * c(0.1, 0.4, 0.65)
mtext(paste("Number of groups = ", length(statistics),
sep = ""), side = 1, line = 5, adj = 0, at = at.col[1],
font = qcc.options("font.stats"), cex = qcc.options("cex.stats"))
center <- object$center
if (length(center) == 1) {
mtext(paste("Center = ", signif(center[1], digits),
sep = ""), side = 1, line = 6, adj = 0, at = at.col[1],
font = qcc.options("font.stats"), cex = qcc.options("cex.stats"))
}
else {
mtext("Center is variable", side = 1, line = 6, adj = 0,
at = at.col[1], qcc.options("font.stats"), cex = qcc.options("cex.stats"))
}
mtext(paste("StdDev = ", signif(std.dev, digits), sep = ""),
side = 1, line = 7, adj = 0, at = at.col[1], font = qcc.options("font.stats"),
cex = qcc.options("cex.stats"))
if (length(unique(lcl)) == 1)
alpha <- 0
#mtext(paste("LCL = ", signif(lcl[1], digits), sep = ""),
# side = 1, line = 6, adj = 0, at = at.col[2],
# font = qcc.options("font.stats"), cex = qcc.options("cex.stats"))
else mtext("LCL is variable", side = 1, line = 6, adj = 0,
at = at.col[2], font = qcc.options("font.stats"),
cex = qcc.options("cex.stats"))
if (length(unique(ucl)) == 1)
mtext(paste("UCL = ", signif(ucl[1], digits), sep = ""),
side = 1, line = 7, adj = 0, at = at.col[2],
font = qcc.options("font.stats"), cex = qcc.options("cex.stats"))
else mtext("UCL is variable", side = 1, line = 7, adj = 0,
at = at.col[2], font = qcc.options("font.stats"),
cex = qcc.options("cex.stats"))
if (!is.null(violations)) {
mtext(paste("Number beyond limits =", length(unique(violations$beyond.limits))),
side = 1, line = 6, adj = 0, at = at.col[3],
font = qcc.options("font.stats"), cex = qcc.options("cex.stats"))
mtext(paste("Number violating runs =", length(unique(violations$violating.runs))),
side = 1, line = 7, adj = 0, at = at.col[3],
font = qcc.options("font.stats"), cex = qcc.options("cex.stats"))
}
}
invisible()
}

Resources