Skip to content

Commit 859fbc6

Browse files
committed
Make fit_LMCurve() and fit_CWCurve() more resilient to plot failures.
1 parent fcfd350 commit 859fbc6

File tree

6 files changed

+59
-16
lines changed

6 files changed

+59
-16
lines changed

NEWS.Rmd

+5
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ to control the saving of the component contribution matrix in the RLum.Results
252252
object it returns. This is now disabled by default: to restore the previous
253253
behaviour, add `method_control = list(export.comp.contrib.matrix = TRUE)` to
254254
the function call (fixed in #573).
255+
* In case of plot failure the function now is able to recover gracefully and
256+
produce an output object (#574, fixed in #578).
255257

256258
### `fit_EmissionSpectra()`
257259
* The function can now return a data frame with the values of all curves
@@ -265,6 +267,9 @@ to control the saving of the component contribution matrix in the RLum.Results
265267
object it returns. This is now disabled by default: to restore the previous
266268
behaviour, add `method_control = list(export.comp.contrib.matrix = TRUE)` to
267269
the function call (fixed in #573).
270+
* In case of plot failure the function now is able to recover gracefully and
271+
produce an output object (#574, fixed in #578; thanks to @LumTKO for
272+
reporting).
268273

269274
### `get_RLum()`
270275
* If `get_RLum()` with `subset` was used on info objects of an `RLum.Analysis-class` object it

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@
296296
to restore the previous behaviour, add
297297
`method_control = list(export.comp.contrib.matrix = TRUE)` to the
298298
function call (fixed in \#573).
299+
- In case of plot failure the function now is able to recover gracefully
300+
and produce an output object (#574, fixed in \#578).
299301

300302
### `fit_EmissionSpectra()`
301303

@@ -312,6 +314,9 @@
312314
to restore the previous behaviour, add
313315
`method_control = list(export.comp.contrib.matrix = TRUE)` to the
314316
function call (fixed in \#573).
317+
- In case of plot failure the function now is able to recover gracefully
318+
and produce an output object (#574, fixed in \#578; thanks to @LumTKO
319+
for reporting).
315320

316321
### `get_RLum()`
317322

R/fit_CWCurve.R

+26-14
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,6 @@ fit_CWCurve<- function(
686686
"cont.sum")
687687

688688
}#endif :: (exists("fit"))
689-
690-
}else{
691-
if (verbose)
692-
.throw_message("Fitting failed, plot without fit produced")
693689
}
694690

695691
##============================================================================##
@@ -698,7 +694,8 @@ fit_CWCurve<- function(
698694
if(plot==TRUE){
699695

700696
##grep par parameters
701-
par.default <- par(no.readonly = TRUE)
697+
par.default <- par()[c("mfrow", "cex", "mar", "omi", "oma")]
698+
on.exit(par(par.default), add = TRUE)
702699

703700
##set colors gallery to provide more colors
704701
col <- get("col", pos = .LuminescenceEnv)
@@ -709,18 +706,23 @@ fit_CWCurve<- function(
709706
layout(matrix(c(1,2,3),3,1,byrow=TRUE),c(1.6,1,1), c(1,0.3,0.4),TRUE)
710707
par(oma = c(1, 1, 1, 1), mar = c(0, 4, 3, 0))
711708
}
712-
713-
##==uppper plot==##
709+
##== upper plot ==##
714710
##open plot area
715-
716-
plot(NA,NA,
711+
plot_check <- try(plot(NA, NA,
717712
xlim=c(min(x),max(x)),
718713
ylim = c(ifelse(log == "xy", 1, 0), max(y)),
719714
xlab = ifelse(inherits(fit, "try-error"), xlab, ""),
720715
xaxt = ifelse(inherits(fit, "try-error"), "s", "n"),
721716
ylab=ylab,
722717
main=main,
723-
log=log)
718+
log = log), silent = TRUE)
719+
720+
if (is(plot_check, "try-error")) {
721+
## reset the graphic device if plotting failed
722+
.throw_message("Figure margins too large or plot area too small, ",
723+
"nothing plotted")
724+
grDevices::dev.off()
725+
} else {
724726

725727
##plotting measured signal
726728
points(x,y,pch=20, col="grey")
@@ -754,15 +756,22 @@ fit_CWCurve<- function(
754756
##==lower plot==##
755757
##plot residuals
756758
par(mar=c(4.2,4,0,0))
757-
plot(x,residuals(fit),
759+
plot_check2 <- try(plot(x,residuals(fit),
758760
xlim=c(min(x),max(x)),
759761
xlab=xlab,
760762
type="l",
761763
col="grey",
762764
ylab="Residual [a.u.]",
763765
lwd=2,
764766
log=if(log=="x" | log=="xy"){log="x"}else{""}
765-
)
767+
), silent = TRUE)
768+
769+
if (is(plot_check2, "try-error")) {
770+
## reset the graphic device if plotting failed
771+
.throw_message("Figure margins too large or plot area too small, ",
772+
"nothing plotted")
773+
grDevices::dev.off()
774+
} else {
766775

767776
##add 0 line
768777
abline(h=0)
@@ -793,11 +802,14 @@ fit_CWCurve<- function(
793802
col = col[i+1])
794803
}
795804
rm(stepping)
805+
} # end if (plot_check2)
796806

807+
} else {
808+
if (verbose)
809+
.throw_message("Fitting failed, plot without fit produced")
797810
}#end if try-error for fit
798811

799-
par(par.default)
800-
rm(par.default)
812+
} # end if (plot_check)
801813
}
802814

803815
##============================================================================##

R/fit_LMCurve.R

+11-2
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ fit_LMCurve<- function(
918918

919919
##==upper plot==##
920920
##open plot area
921-
plot(
921+
plot_check <- try(plot(
922922
NA,
923923
NA,
924924
xlim = xlim,
@@ -928,7 +928,14 @@ fit_LMCurve<- function(
928928
main = main,
929929
log = log,
930930
ylab = ylab
931-
)#endplot
931+
), silent = TRUE)
932+
933+
if (is(plot_check, "try-error")) {
934+
## reset the graphic device if plotting failed
935+
.throw_message("Figure margins too large or plot area too small, ",
936+
"nothing plotted")
937+
grDevices::dev.off()
938+
} else {
932939

933940
mtext(side=3,sample_code,cex=0.8*cex)
934941

@@ -1031,6 +1038,8 @@ fit_LMCurve<- function(
10311038
}#end if try-error for fit
10321039

10331040
if (fun == TRUE) sTeve() # nocov
1041+
1042+
} # end if (plot_check)
10341043
}
10351044
##-----------------------------------------------------------------------------
10361045
##remove objects

tests/testthat/test_fit_CWCurve.R

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## load data
22
data(ExampleData.CW_OSL_Curve, envir = environment())
3+
data(ExampleData.FittingLM, envir = environment())
34

45
test_that("input validation", {
56
testthat::skip_on_cran()
@@ -77,6 +78,14 @@ test_that("check functionality", {
7778
## more coverage
7879
expect_message(fit_CWCurve(ExampleData.CW_OSL_Curve[1, ]),
7980
"Error: Fitting failed, plot without fit produced")
81+
82+
pdf(tempfile(), width = 1, height = 1)
83+
expect_message(fit_CWCurve(values.curve, n.components.max = 3,
84+
verbose = FALSE),
85+
"Figure margins too large or plot area too small")
86+
pdf(tempfile(), width = 1, height = 1)
87+
expect_message(fit_CWCurve(ExampleData.CW_OSL_Curve, verbose = FALSE),
88+
"Figure margins too large or plot area too small")
8089
})
8190

8291
test_that("regression tests", {

tests/testthat/test_fit_LMCurve.R

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ test_that("snapshot tests", {
9595
})
9696
expect_message(fit_LMCurve(values.curve[1:15, ], main = ""),
9797
"Fitting failed, plot without fit produced")
98+
pdf(tempfile(), width = 1, height = 1)
99+
expect_message(fit_LMCurve(values.curve, verbose = FALSE),
100+
"Figure margins too large or plot area too small")
98101

99102
SW({
100103
skip_on_os("mac")

0 commit comments

Comments
 (0)