Description
I noticed this problem because locally checking 'ggspectra' started triggering multiple NOTEs for too-long a runing time for examples (as much as 20s), as well as the unit tests of 'ggspectra' taking much longer to run than earlier.
I see a x3 or more increase in run time when redering plots created with autoplot()
methods when going from 3.4.4 to 3.5.0 or under the current main branch here. I see this problem both under R 4.4.0 and R 4.3.2 patched. I am on Windows 11.
I see the problem also happening with my derived ggplot()
methods, which are called by my derived autoplot()
methods, so the something in the ggplot()
methods triggers this. The main difference introduced by 'ggspectra' is that the p$data
is set to a class derived from data.frame
(i.e., the class of the argument passed to data
is preserved) and also retains some attributes used to store metadata.
This reprex shows how derived ggplot()
methods work, the actual problem is shown farther down.
library(ggspectra)
#> Loading required package: photobiology
#> News at https://www.r4photobiology.info/
#> Loading required package: ggplot2
p <- ggplot(polyester.spct) + geom_line()
class(p$data)
#> [1] "filter_spct" "generic_spct" "tbl_df" "tbl" "data.frame"
get_attributes(p$data)
#> $multiple.wl
#> [1] 1
#>
#> $Tfr.type
#> [1] "total"
#>
#> $filter.properties
#> Rfr (/1): 0.074, thickness (mm): 0.125, attenuation mode: absorption.
#> $comment
#> [1] "Smoothed using 'supsmu', span = 0.01"
#>
#> $what.measured
#> [1] "Polyester, clear film, 0.000125 m thick, Autostat CT5 from McDermit Autotype; new"
Created on 2024-02-29 with reprex v2.1.0
I do not know if the problem is in 'ggspectra' or 'ggplot2' but something in the current 'ggplot2' that was not in earlier versions triggers this problem. What I find disconcerting is that I see with profvis()
that the slowdown is mainly caused under 3.5.0 by multiple calls to utils:::readCitationFile()
when rendering plots with ggplotGrob()
or print()
. Clearly there is something in my code in 'ggspectra' that triggers this, as the problem does not seem to affect other plots built within 'ggplot2', or at least not all of them.
The number of calls to utils:::readCitationFile()
seems to increase with increasing number of layers in the plots. I give to examples below.
Do you have any idea where I can start looking for the source of this calls to utils:::readCitationFile()
?
A simple examples using ggplot()
, really dispatched to ggplot.filter_spct()
.
library(profvis)
library(ggspectra)
ggplot(polyester.spct) + geom_line()
profvis(
p <- ggplot(polyester.spct) + geom_line(),
interval = 0.005,
rerun = TRUE
)
ggpg <- ggplotGrob(p)
profvis(
ggpg <- ggplotGrob(p),
interval = 0.005,
rerun = TRUE
)
and a detail
More examples
Additional examples using autoplot()
showing the same pattern, but differing in the number of calls to utils:::readCitationFile()
.
autoplot(sun.spct)
profvis(
p <- autoplot(sun.spct),
interval = 0.005,
rerun = TRUE
)
ggpg <- ggplotGrob(p)
profvis(
ggpg <- ggplotGrob(p),
interval = 0.005,
rerun = TRUE
)
print(p)
profvis(
print(p),
interval = 0.005,
rerun = TRUE
)
Building p
ggplotGrob()
And a detail
print()
A simpler plot with a single layer
p <- autoplot(sun.spct, annotations = "")
ggpg <- ggplotGrob(p)
profvis(
ggpg <- ggplotGrob(p),
interval = 0.005,
rerun = TRUE
)
And the detail