trevorld / ggpattern

ggplot geoms with pattern fills
https://trevorldavis.com/R/ggpattern/dev/
Other
356 stars 18 forks source link

image patterns do not update when local image files are changed #95

Closed yuans-cellbio closed 3 months ago

yuans-cellbio commented 1 year ago

I want to geom_col_pattern + scale_pattern_file_discrete or _manual to create well-aligned image tiles in R markdown. My goal is to edit the source images (crop, offset, etc.) so that I can generate different image tiles using the same code.

However, I noticed that if I use the same image file name after editing, the next plot generated still uses the old image. Even if you delete the referred images locally, the code still runs and generates the same plot.

I think ggpattern retrieves cached images when it sees the same file names. It runs correctly after restarting Rstudio or when knitted.

Here is an example.

# you can use any jpeg files named "img1.jpg", img2.jpg", img3.jpg"
df <- data.frame(
  width = 1, 
  label = paste0("img", 1:3), 
  img = paste0("img", 1:3, ".jpg")
)

df %>%
  ggplot(aes(x = width, y = label)) +
  geom_col_pattern(
    aes(pattern_filename = img),
    pattern = "image",
    pattern_type = "squish",
    color = "black",
    linewidth = 1, 
    pattern_scale = 1
    ) +
  labs(x = NULL, y = NULL) +
  scale_pattern_filename_discrete(choices = df$img) + # It would be nice if df is inherited so that choices = img.
  theme_minimal() +
  theme(
    legend.position = "none"
  )

# It works fine if I just reverse the file name order.

df %>%
  ggplot(aes(x = width, y = label)) +
  geom_col_pattern(
    aes(pattern_filename = img),
    pattern = "image",
    pattern_type = "squish",
    color = "black",
    linewidth = 1, 
    pattern_scale = 1
    ) +
  labs(x = NULL, y = NULL) +
  scale_pattern_filename_discrete(choices = rev(df$img)) +
  theme_minimal() +
  theme(
    legend.position = "none"
  )

# If you delete all jpegs, they both run and generate the old plots. 

Session info

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621), RStudio 2022.12.0.353

Locale:
  LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
  LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
  LC_TIME=English_United States.utf8    

Package version:
  cachem_1.0.6       class_7.3.21       classInt_0.4.8     cli_3.6.0         
  colorspace_2.1.0   DBI_1.1.3          e1071_1.7.13       fansi_1.0.4       
  farver_2.1.1       fastmap_1.1.0      ggpattern_1.1.0.0  ggplot2_3.4.0     
  glue_1.6.2         graphics_4.2.2     grDevices_4.2.2    grid_4.2.2        
  gridpattern_1.0.2  gtable_0.3.1       isoband_0.2.7      KernSmooth_2.23.20
  labeling_0.4.2     lattice_0.20.45    lifecycle_1.0.3    magrittr_2.0.3    
  MASS_7.3.58.2      Matrix_1.5.3       memoise_2.0.1      methods_4.2.2     
  mgcv_1.8.41        munsell_0.5.0      nlme_3.1.161       pillar_1.8.1      
  pkgconfig_2.0.3    png_0.1.8          proxy_0.4.27       R6_2.5.1          
  RColorBrewer_1.1.3 Rcpp_1.0.10        rlang_1.0.6        s2_1.1.2          
  scales_1.2.1       sf_1.0.9           splines_4.2.2      stats_4.2.2       
  tibble_3.1.8       tools_4.2.2        units_0.8.1        utf8_1.2.3        
  utils_4.2.2        vctrs_0.5.2        viridisLite_0.4.1  withr_2.5.0       
  wk_0.7.1   
trevorld commented 1 year ago
yuans-cellbio commented 1 year ago
  • Looking at the {memoise} documentation I suspect the following might work now to clear the cache: memoise::forget(gridpattern:::img_read_memoised) Do this every time the local image files are changed.
  • {gridpattern}'s image pattern uses an internal function img_read_memoised which is a "memoised" function to read images that uses the {memoise} package (this functionality is extracted from an older version of {ggpattern}).
  • In particular this means if you are using an image from a website (filename is a URL) you won't keep hitting that website over and over. In particular this is useful for the "placeholder" pattern which auto-grabs images from various "placeholder" websites.
  • Because we want to be respectful to the bandwidth of the placeholder websites (and other websites) I'm not entirely sure I want to change this behavior as a default. But maybe document/make easier to clear the cache?

memoise::forget(gridpattern:::img_read_memoised) worked like a charm.

I agree that the current image caching can be helpful when sourcing images from websites. Some more documentation is perfect.

Thank you for the cool package! It's not useless at all.

trevorld commented 9 months ago

Development version of {gridpattern} now exports reset_image_cache() which resets the cache used by the "image" and "placeholder" patterns.