ThinkR-open / golem

A Framework for Building Robust Shiny Apps
https://thinkr-open.github.io/golem/
Other
904 stars 132 forks source link

[Bug] {shinyBS} doesn't work inside a golem app #297

Closed ColinFay closed 2 years ago

ColinFay commented 4 years ago

If I create a new golem and use :

#' @import shiny
#' @import shinyBS
app_ui <- function(request) {
  tagList(
    # Leave this function for adding external resources
    golem_add_external_resources(),
    # List the first level UI elements here 
    fluidPage(
      sliderInput(
        "bins",
        "Number of bins:",
        min = 1,
        max = 50,
        value = 30
      ),
      bsTooltip(
        "bins", 
        "The wait times will be broken into this many equally spaced bins", 
        "right", 
        options = list(container = "body")
      )

    )
  )
}

as app_ui, the browser returns a series of errors:

Screenshot 2019-11-20 at 21 18 51

preventing {shinyBS} from working.

The very same code works in an app.R file (restart session between test to clear cache):

library(shiny)
library(shinyBS)
ui <- function(request){
  tagList(
    fluidPage(
      h1("BsTest"),
      sliderInput(
        "bins",
        "Number of bins:",
        min = 1,
        max = 50,
        value = 30
      ),
      bsTooltip(
        "bins", 
        "The wait times will be broken into this many equally spaced bins", 
        "right", 
        options = list(container = "body")
      )

    )
  )

}

server <- function(input, output, session){

}

shinyApp(ui, server)
sjspielman commented 4 years ago

+1 for this issue. Any current ideas for a workaround?

mnist91 commented 3 years ago

I experienced the same issue and my workaround has been as follows:

ColinFay commented 2 years ago

Hey all,

The workarounds so far are :

usethis::use_package("shinyBS", "depends")
golem_add_external_resources <- function() {
  add_resource_path(
    "www", app_sys("app/www")
  )

  add_resource_path(
    "sbs", system.file("www", package = "shinyBS")
  )

  tags$head(
    favicon(),
    bundle_resources(
      path = app_sys("app/www"),
      app_title = "xyz"
    )
    # Add here other external resources
    # for example, you can add shinyalert::useShinyalert()
  )
}
ColinFay commented 2 years ago

So, the issue doesn't come from {golem} itself but from the way {shinyBS} attach its dependency.

https://github.com/ebailey78/shinyBS/blob/shinyBS3/R/misc.R#L1

Because of the way the dependencies are bundled, it's only available in case the package is loaded, not attached.

I'll write the {shinyBS} team and see how this can be worked out.

ColinFay commented 2 years ago

Open issue on {shinyBS} about this issue, which is due to how the package attaches its external resources.

https://github.com/ebailey78/shinyBS/issues/138

I'm putting this issue on hold for now.

asbates commented 2 years ago

In case this helps someone.

The workarounds above didn't work for me. Shiny includes Bootstrap tooltips so I ended up writing a JS function. It's something like this (can't share it exactly):

addTooltip = function(id, options) {
 setTimeout(
  function() {
   $("#" + id).tooltip(options)
  }, 1000
)

}

The setTimeout() may or may not be needed in all cases. I added it because the function was being called too soon, before the element existed, so the tootlip didn't show.

ColinFay commented 2 years ago

Hey folks,

I'll close this issue for now, I don't think it's a {golem} issue but a {shinyBS} one — there has been an issue since 2018 (!!) to correct this https://github.com/ebailey78/shinyBS/issues/100 and a PR since 2019 https://github.com/ebailey78/shinyBS/pull/109 that corrects the issue.

There hasn't been any activity on the package since 2016 — https://github.com/ebailey78/shinyBS/commits/shinyBS3 and the last CRAN version is from 2015, so my answer would be to move to another package that is maintained and more modern.

For a tooltip, I've been using https://atomiks.github.io/tippyjs/ in several apps, I'd suggest using this one.

psolymos commented 2 years ago

Just putting this here: {golem} works fine with {shinyBS} with adding it to the Depends. The issue, as it turns out, is that jQuery > 4.1 uses the 'dispose' method instead of 'destroy' (https://stackoverflow.com/questions/51960211/error-no-method-named-destroy-for-popover-bootstrap).

I fixed this in my fork and the tooltips seem to work: https://github.com/psolymos/shinyBS/commit/86c427aed776fc6d5c4efcee0112e4c22c81a95e