หยุดการเล่นอัตโนมัติแบบหมุนโดยใช้ bsplus, Shiny และ JavaScript ของ R
Dec 26 2020
ใช้คำแนะนำ @YBS เพื่อใส่ "bs_carousel (... ) ไว้ใน renderUI" นี่คือความพยายามของฉัน การแสดงสไลด์และการเล่นอัตโนมัติปิดอยู่ในตอนแรก อย่างไรก็ตามคลิกที่รูปตัววีด้านขวาและการเล่นอัตโนมัติจะเริ่มขึ้น
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)
คำถามเดิม
ฉันใช้ม้าหมุนจากแพ็คเกจ bsplus ของ R ฉันต้องการหยุดการเล่นอัตโนมัติ การแก้ปัญหาต่าง ๆ ได้รับการกล่าวถึงที่นี่
ฉันกำลังพยายามโดยไม่ประสบความสำเร็จในการใช้หนึ่งในนั้นด้านล่าง
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)
คำตอบ
1 YBS Dec 27 2020 at 09:54
อย่างไรก็ตามการเล่นอัตโนมัติจะไม่หยุดลงbs_carousel()เว้นแต่ว่าตัวชี้เมาส์จะวางเมาส์เหนือสไลด์ที่ใช้งานอยู่ อย่างไรก็ตามโค้ดด้านล่างนี้แสดงให้เห็นว่าสามารถปิดการเล่นอัตโนมัติcarousel()จากshinydashboardPlusแพ็กเกจได้
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)
})
}
)