0

I have a folder full of charts, generated from a previous step. All of them are PNG files. I want to be able to choose anyone using Flexdashboard and load it. As no shiny or server service is needed I tried Crosstalk package

library(crosstalk)
library(magrittr)
library(png)
    
df <- list.files("plots/", full.names = TRUE) %>%
      as_tibble() %>%
      magrittr::set_names("path") 
    
    shared_data <- SharedData$new(df,  key = ~path)
    
    p <- shared_data %>% readPNG(source = path)
    
    bscols( filter_select(id = "file_id", 
                         label = "CHOOSE", 
                         sharedData = shared_data, 
                         group = ~path), 
           p)

I am stuck on a very simple error i cannot solve as all paths are properly read from file:

Error in path.expand(source) : invalid 'path' argument

Tried to use knitr too:

   bscols(filter_select("path", "CHOOSE", shared_data),
      knitr::include_graphics(shared_data, ~path))

Error in makeGroupOptions(sharedData, group, allLevels) : argument "group" is missing, with no default

Maybe there is a simpler approach but crosstalk seemed a very simple one as it does not need shiny or any other component but a data frame.

Forge
  • 1,587
  • 1
  • 15
  • 36

1 Answers1

2

This can be achieved more easily by using bsselectR The library is 5 years old but works perfectly in my trial. It does not offer the same level of in-plot interaction as crosstalk but might be sufficient for the current purpose.

Below is the code snippet to add to R Markdown document. I've altered the sample code to allow for recursive directory walk. The plots directory needs to be placed in the same directory as the R file.

```{r}
# 
library(stringr)
library(bsselectR)

state_plots <- paste0(list.files("plots", full.names = TRUE, recursive = TRUE))
names(state_plots) <- str_replace_all(state_plots, 
                                      c("\\.png" = "", 
                                        "plots/" = ""))


bsselect(state_plots, type = "img", selected = "sns_heatmap", 
         live_search = TRUE, show_tick = TRUE)


```

Output :

enter image description here

R.S.
  • 2,093
  • 14
  • 29
  • This fails when selecting other than the preselected picture. It only worked on load. – Forge Nov 22 '21 at 11:40
  • Let me check (tomorrow). I remember trying all the images in the dynamic drop down list. – R.S. Nov 22 '21 at 20:34