4

Using @YBS suggestion to put, "bs_carousel(...) inside a renderUI" Here is my attempt. The slides render and autoplay is off at first. However, click the right chevron and autoplay begins.

library("shiny")
library("shinyjs")
library("bsplus")

# Stop autoplay
# https://stackoverflow.com/questions/26133618/how-to-stop-bootstrap-carousel-from-autosliding

jscode <- "
shinyjs.init = function() {
  $('.carousel').carousel({ interval: false });
}"

ui <- fluidPage(
  
  shinyjs::useShinyjs(),
  extendShinyjs(text = jscode, functions = c()),
  
  # Application title
  titlePanel("Carousel Demo"),
  
  uiOutput("carousel")
)

server <- shinyServer(function(input, output) {
  output$carousel <- renderUI({
    bs_carousel(id = "images", use_indicators = TRUE) %>%
      bs_append(
        content = bs_carousel_image(src = "https://placehold.it/900x500/3c8dbc/ffffff&text=Merry")
      ) %>%
      bs_append(
        content = bs_carousel_image(src = "https://placehold.it/900x500/3c8dbc/ffffff&text=Christmas")
      ) %>%
      bs_append(
        content = bs_carousel_image(src = "https://placehold.it/900x500/3c8dbc/ffffff&text=To")
      ) %>%
      bs_append(
        content = bs_carousel_image(src = "https://placehold.it/900x500/3c8dbc/ffffff&text=All")
      ) 
  })
  
})

# Run the application
shinyApp(ui = ui, server = server)

Original Question

I am using the carousel from R's bsplus package. I want to stop the auto play. Various solutions have been mentioned here.

I am trying, without success, to implement one of them below.

library("shiny")
library("bsplus")

# Stop autoplay
# https://stackoverflow.com/questions/26133618/how-to-stop-bootstrap-carousel-from-autosliding

jscode <- "
shinyjs.init = function() {
  $('.carousel').carousel({ interval: false });
}"

ui <- shinyUI(fluidPage(
  
  shinyjs::useShinyjs(),
  extendShinyjs(text = jscode, functions = c()),

  # Application title
  titlePanel("Carousel Demo"),
),

bs_carousel(id = "images", use_indicators = TRUE) %>%
  bs_append(
    content = bs_carousel_image(src = "https://placehold.it/900x500/3c8dbc/ffffff&text=Merry")
  ) %>%
  bs_append(
    content = bs_carousel_image(src = "https://placehold.it/900x500/3c8dbc/ffffff&text=Christmas")
  ) %>%
  bs_append(
    content = bs_carousel_image(src = "https://placehold.it/900x500/3c8dbc/ffffff&text=To")
  ) %>%
  bs_append(
    content = bs_carousel_image(src = "https://placehold.it/900x500/3c8dbc/ffffff&text=All")
  ) 

)

server <- shinyServer(function(input, output) {
  
})

# Run the application
shinyApp(ui = ui, server = server)
ixodid
  • 2,180
  • 1
  • 19
  • 46
  • If you put `bs_carousel(...)` inside a `renderUI` on the server side, autoplay is not on. – YBS Dec 26 '20 at 02:22
  • That works at first. But autoplay begins if you click the "next" chevron. See new code in original post. – ixodid Dec 26 '20 at 02:38
  • It appears that this was a bug that was fixed a while ago. I am not sure if it is still a bug. It may be best to put a play/pause button so that users can click to play and click again to pause. – YBS Dec 26 '20 at 14:00
  • How can I add a pause button if I don’t know how to pause programmatically? – ixodid Dec 26 '20 at 15:53

1 Answers1

2

Somehow the autoplay does not stop in bs_carousel(), unless mouse pointer is hovering over the active slide. However, the code below demonstrates that autoplay can be switched off in carousel() from shinydashboardPlus package.

library(shiny)
library(shinydashboardPlus)
library(DT)

jscode <-"
$(document).ready(function(){
            $('#mycarousel').carousel( { interval:  false } );
});"

shinyApp(
  ui = dashboardPage(
    header = dashboardHeader(),
    sidebar = dashboardSidebar(),
    body = dashboardBody(
      tags$head(
        tags$style(HTML("
      #mycarousel {
        width:900px;
        height:600px;
      }
    .carousel-control{
      color:#FF0000;
    }
    "))
      ),
      tags$head(tags$script(HTML(jscode))),
      carousel(
        id = "mycarousel",
        carouselItem(
          DTOutput("show_iris_dt")
        ),
        carouselItem(
          caption = "An image file",
          tags$img(src = "YBS.png")
        ),
        carouselItem(
          caption = "Item 3",
          tags$img(src = "http://placehold.it/900x500/39CCCC/ffffff&text=Happy+New+Year")
        )
      )
    ),
    title = "Carousel Demo"
  ),
  server = function(input, output) {
    output$show_iris_dt <- renderDT({
      datatable(iris)
    })
  }
)
YBS
  • 19,324
  • 2
  • 9
  • 27