停止旋转木马自动播放使用R&#的bplus,闪光和脚本脚本、闪光、木马、自动播放

2023-09-03 08:15:54 作者:抹卜掉的伤痛

使用@YBS建议放置,";bs_carousel(...)在renderUI;内 这是我的尝试。幻灯片的渲染和自动播放最初处于关闭状态。但是,单击右V形即可开始自动播放。

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)

原始问题

我正在使用R的bplus包中的旋转木马。我想停止自动播放。提到了各种解决方案here。

脚本大师 Script Expert V7.40绿色特别版 XP系统软件下载基地 XP下载站

我正在尝试实现以下其中之一,但未成功。

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)

推荐答案

自动播放不会在bs_carousel()中停止,除非鼠标指针悬停在活动幻灯片上。但是,下面的代码演示了可以在shinydashboardPlus包的carousel()中关闭自动播放。

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)
    })
  }
)