thomasp85 / shinyFiles

A shiny extension for server side file access
195 stars 47 forks source link

Selection event not fired #162

Closed sgvignali closed 2 years ago

sgvignali commented 2 years ago

Hi,

first of all thank you for this package! It looks like the selection event is not fired.

To easily test it you can:

  1. run shinyFilesExample()
  2. add $('#save').on('selection', function(event, path) {console.log(path);}) form the console of the browser
  3. interact with the button.

I've tried only with shinySaveButton but I don't know if this happens also with the other buttons.

vnijs commented 2 years ago

@sgvignali: I'm not seeing any issues with shinyFiles::shinyFilesExample(). Can you provide a small fully reproducible example that highlights the issue you are seeing?

sgvignali commented 2 years ago

Here is a reproducible example:

library(shiny)
library(shinyFiles)
library(shinyjs)
library(fs)

ui <- fluidPage(
  useShinyjs(),
  headerPanel("Basic example"),
  sidebarLayout(
    sidebarPanel(
      shinyFilesButton(id = "file",
                       label = "File select",
                       title = "Please select a file",
                       multiple = FALSE,
                       viewtype = "detail"),
      tags$hr(),
      shinySaveButton(id = "save",
                      label = "Save file",
                      title = "Save file as...",
                      filetype = list(text = "txt"),
                      viewtype = "icon"),
    ),
    mainPanel(
      h4(id = "filetext"),
      tags$hr(),
      h4(id = "savetext"),
    )
  )
)

server <- function(input, output, session) {
  volumes <- c(Home = fs::path_home())

  shinyFileChoose(input,
                  id = "file",
                  roots = volumes,
                  session = session)
  shinyFileSave(input,
                "save",
                roots = volumes,
                session = session)

  shinyjs::runjs(
    "$('#file').on('selection', function(event, path)
                                  {$('#filetext').text('File event fired');})"
  )

  shinyjs::runjs(
    "$('#save').on('selection', function(event, path)
                                  {$('#savetext').text('Save event fired!');})"
  )
}

shinyApp(ui = ui, server = server)

When you select a file the selection event is fired and the corresponding h4 tag is populated. This doesn't happen with the save button. I hope this helps!

Sergio

vnijs commented 2 years ago

Thanks for that @sgvignali. This my busiest teaching quarter so it will be at least a few weeks before I will be able to make time to work on this. If you want to craft a PR that addresses your concern that would be wonderful

sgvignali commented 2 years ago

@vnijs I'll try to prepare a PR in the following days.