royfrancis / pixture

HTML Image Gallery in R with responsive grid layout and lightbox.
http://royfrancis.github.io/pixture/
GNU General Public License v3.0
11 stars 3 forks source link

pixgallery() not working with local images #4

Open n8craig opened 2 years ago

n8craig commented 2 years ago

Pixture looks really awesome, and can be super useful with parameterized reports. I'm looking to implement pixture in a museum setting where I have a large number of images. However, I'm having trouble executing pixgallery() with local files. The function works flawlessly with URL's but does not seem to work with local files. Hopefully I'm not missing something obvious.

Local images example not working

Following the Pixgallery article, I use list files to get a list of images with relative paths and pass these to pixgallery(). However, the function returns blank squares.

paths <- list.files("../images", full.names = TRUE)
paths

[1] "../images/brittany-1937051_960_720.jpg"                                    
[2] "../images/lostmarch-menhir.jpg"                                            
[3] "../images/Menhir_du_Champ_Dolent.jpg"                                      
[4] "../images/Organ_Mountains-Desert_Peaks_National_Monument_(17717943249).jpg"

image

The issue seems to be isolated to pixgallery() because I can display images using functions from other libraries like imager with plot.

library(imager)
im<-load.image(paths[2])
plot(im)

image

Sample local images not working

Additionally, the local images example that is based on sample images from the package also does not work. When I generate this set of paths, I can also display these from the paths list using imagr with plot.

paths <- list.files(system.file("extdata/images", package="pixture"), full=TRUE)
paths
pixgallery(paths)

Just as in the article example, the output is six blank squares.

image

However, I can plot items from the list.

im<-load.image(paths[6])
plot(im)

image

Any thoughts on how to address this? Is it something at my end, or is there another way to approach this? Thank you for any insight you might have.

sessionInfo

R version 4.2.1 (2022-06-23 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 [4] LC_NUMERIC=C LC_TIME=English_United States.utf8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] imager_0.42.13 stringr_1.4.0 purrr_0.3.4 magrittr_2.0.3 pixture_0.0.2.2001

loaded via a namespace (and not attached): [1] igraph_1.3.1 Rcpp_1.0.8.3 rstudioapi_0.13 knitr_1.39 here_1.0.1 xtable_1.8-4
[7] R6_2.5.1 jpeg_0.1-9 rlang_1.0.3 fastmap_1.1.0 tools_4.2.0 xfun_0.30
[13] png_0.1-7 cli_3.3.0 htmltools_0.5.2 ellipsis_0.3.2 rprojroot_2.0.3 yaml_2.3.5
[19] digest_0.6.29 lifecycle_1.0.1 bmp_0.3 shiny_1.7.1 later_1.3.0 htmlwidgets_1.5.4 [25] promises_1.2.0.1 mime_0.12 tiff_0.1-11 readbitmap_0.1.5 stringi_1.7.6 compiler_4.2.0
[31] jsonlite_1.8.0 httpuv_1.6.5 pkgconfig_2.0.3

royfrancis commented 2 years ago

Is this in the RStudio notebook or in a rendered HTML file?

n8craig commented 2 years ago

It is in the output files which at my end are rendered in quarto. The screenshots came from the browser, not the R Studio viewer. I notice also that the image files are not being written to the output directory.

n8craig commented 2 years ago

I notice that if I include knitr::include_graphics(paths) which renders the files, they are created in the output directory and pixgallery() renders. It seems that the issue is with the images not being written to the output directory. When they are, everything else works well.

royfrancis commented 2 years ago

Ah right, yes! Pixture does not move or embed the images. If it works with knitr::include_graphics(paths), that's good news! Thanks for bringing this up. I will update it in the vignette.

n8craig commented 2 years ago

The matter seems to be with needing to copy the files to the kintr output directory, which in my narrow case is docs because I'm using GitHub pages (Quarto normally renders to _site). If I 1) check for the output directory and create it if not there and 2) copy files from paths to the appropriate knitr output dir (in my case docs/images under a project), then pixgallery() seems to render things fine.

I'm probably not doing this in the most elegant way, but this got things working


if (!dir.exists(paste0(here::here(),"/docs/",substr(paths[1],4,17)))) {
  print("Not there, so create")
  dir.create(paste0(here::here(),"/docs/",substr(paths[1],4,9))) # create parent dir
  dir.create(paste0(here::here(),"/docs/",substr(paths[1],4,16))) # create image dir
} else {
  print("There, don't create")
}

file.copy(from = paths,
          to = paste0(here::here(), "/docs/", substr(paths,4,100)))

So it seems that pixgallery() isn't copying local files to the output directory. If one does this explicitly in an R Markdown document, then things work ok. Perhaps that is something to raise in the vignette. I don't know how hard it would be to include in the pixgallery() function the ability to copy files to the knitr output "under the hood".

nipnipj commented 1 year ago

I was like "What am I doing wrong?!" for like an hour.