一段时间以来一直在解决这个 slickR 问题。我将不胜感激有关如何解决此问题或解决方案的不同方法的任何意见或新观点。
我一直在解决两个问题:
第一个我认为可以使用我不太熟悉的 CSS 来解决,当通过使用 input$series 更新“obj”时,slickR 似乎正在创建多个 div。这是不可取的,因为它会将最近的 div 重新定位在页面的下方。我尝试使用我也不太熟悉的 javascript 来使用观察事件来破坏旧的浮油。该问题的简单解决方案的奖励积分。
我正在努力解决的主要问题是我想将点转换为图像,并在选择每个系列时让它们动态更新。这里的目标是,我希望在上方显示一个更大的图像,并在下方显示一系列“缩略图”,以便用户可以了解每张照片的外观,而无需滚动浏览轮播中的每张图像。
我的应用程序比这个示例复杂得多,但我使用的是 slickR,因为它可以方便地访问当前、活动和中心幻灯片,我使用它来过滤额外的数据框以呈现有关每个活动的信息的显示/在轮播中居中图像。
这是一个演示这两个问题的示例:
library(shiny)
library(shinyjs)
library(shinydashboard)
library(shinydashboardPlus)
library(jsonlite)
library(htmltools)
library(htmlwidgets)
library(slickR)
fish <- c(
"https://www.hakaimagazine.com/wp-content/uploads/aquarium-reef-fish-banner.jpg",
"https://www.hakaimagazine.com/wp-content/uploads/aquarium-reef-fish-banggai-cardinalfish.jpg",
"https://www.sonomamag.com/wp-content/uploads/2016/12/fish.jpg" )
butterfly <- c(
"https://www.futurity.org/wp/wp-content/uploads/2019/02/viceroy-butterfly_1600.jpg",
"https://harvardforest.fas.harvard.edu/sites/harvardforest.fas.harvard.edu/files/butterfly_giant_swallowtail_model.jpg",
"https://www.butterflyidentification.com/wp-content/uploads/2019/02/Doris-Longwing-Butterfly-Images.jpg"
)
bird <- c(
"http://www.cutepetname.com/wp-content/uploads/2018/11/funy-bird-names-feat-img.jpeg",
"http://3.bp.blogspot.com/-mJ-Kw1mdLOo/UeLJx7vxsaI/AAAAAAAADkg/TfDHtuJnY7I/s1600/The-Cardinal-Bird.jpg",
"https://images7.alphacoders.com/416/thumb-1920-416332.jpg"
)
df <- data.frame(fish=fish, butterfly=butterfly, bird=bird)
pics <- df[,"fish"]
ui <- dashboardPagePlus(
useShinyjs(),
header = dashboardHeaderPlus(disable = TRUE ),
sidebar = dashboardSidebar(
radioButtons('series', "Choose Series",
choices = c("fish"="fish", "butterfly"="butterfly", "bird"="bird")
)
),
body = dashboardBody(
tags$script( sprintf("var dotObj = %s", jsonlite::toJSON( 'dots')) ),
slickROutput('slickRCarousel'),
uiOutput('dots')
)
)
server <- function(input, output, session) {
# unslick to counteract slick generating multiple divs?
observeEvent(input$series, ignoreInit = TRUE, {
runjs("$('.slickRCarousel').slick('unslick');")
print(df[,input$series])
})
# observe({
# print(input$slickROutput_current$.clicked)
# })
output$dots <- renderPrint({
c(df[,input$series])
})
# carousel setup
cP2 <- JS("function(slick,index) {
return '<a><img src= ' + dotObj[index] + ' width=30px height=30px></a>';
}")
opts <-
settings(
initialSlide = 1,
slidesToShow = 3,
slidesToScroll = 1,
centerMode = TRUE,
focusOnSelect = TRUE,
dots = TRUE,
customPaging = cP2
)
output$slickRCarousel <- renderSlickR({
slick_dots_logo <- slickR(
obj = df[,input$series],
height = 100,
width = "95%"
) + opts
})
}
shinyApp(ui, server)
提前感谢您抽出宝贵时间查看此内容!
编辑 1:澄清和当前方法
这是我目前的方法,尝试通过 session$sendCustomMessage 传递动态值并更新负责呈现 slickR 点(或缩略图)的变量。
持续存在的问题是:
- 更改单选按钮时轮播会跳转
- 更改单选按钮时缩略图不会更新
代码:
library(shiny)
library(shinyjs)
library(shinydashboard)
library(shinydashboardPlus)
library(jsonlite)
library(htmltools)
library(htmlwidgets)
library(slickR)
fish <- c(
"https://www.hakaimagazine.com/wp-content/uploads/aquarium-reef-fish-banner.jpg",
"https://www.hakaimagazine.com/wp-content/uploads/aquarium-reef-fish-banggai-cardinalfish.jpg",
"https://www.sonomamag.com/wp-content/uploads/2016/12/fish.jpg" )
butterfly <- c(
"https://www.futurity.org/wp/wp-content/uploads/2019/02/viceroy-butterfly_1600.jpg",
"https://harvardforest.fas.harvard.edu/sites/harvardforest.fas.harvard.edu/files/butterfly_giant_swallowtail_model.jpg",
"https://www.butterflyidentification.com/wp-content/uploads/2019/02/Doris-Longwing-Butterfly-Images.jpg"
)
bird <- c(
"http://www.cutepetname.com/wp-content/uploads/2018/11/funy-bird-names-feat-img.jpeg",
"http://3.bp.blogspot.com/-mJ-Kw1mdLOo/UeLJx7vxsaI/AAAAAAAADkg/TfDHtuJnY7I/s1600/The-Cardinal-Bird.jpg",
"https://images7.alphacoders.com/416/thumb-1920-416332.jpg"
)
df <- data.frame(fish=fish, butterfly=butterfly, bird=bird)
ui <- dashboardPagePlus(
useShinyjs(),
header = dashboardHeaderPlus(disable = TRUE ),
sidebar = dashboardSidebar(
radioButtons('series', "Choose Series",
choices = c("fish"="fish", "butterfly"="butterfly", "bird"="bird")
)
),
body = dashboardBody(
# this sets thumbnails to always be fish, replacing with
# df[,input$series] seems to cause an issue.
tags$script( HTML(sprintf("var dotObj = %s", jsonlite::toJSON( df[,'fish'])) ) ),
#attempting to add a custom message handler to update the dots, but it doesn't
# update
tags$script("
Shiny.addCustomMessageHandler(setDots, function(newDots) {
var dotObj = newDots;
});
"),
slickROutput('slickRCarousel')
)
)
server <- function(input, output, session) {
#custom message handler to update the dots, but it doesn't update
observe({
session$sendCustomMessage('setDots', jsonlite::toJSON( df[,input$series]))
#print(jsonlite::toJSON( df[,input$series]))
})
# unslick to counteract slick generating multiple divs
# and pushing the carousel down? It's not working
observeEvent(input$series, ignoreInit = TRUE, {
runjs("$('.slickRCarousel').slick('unslick');")
})
# slickR carousel setup
cP2 <- JS(
"function(slick,index) {
return '<a><img src= ' + dotObj[index] + ' width=30px height=30px></a>';
}" )
opts <-
settings(
initialSlide = 1,
slidesToShow = 1,
slidesToScroll = 1,
centerMode = TRUE,
focusOnSelect = TRUE,
dots = TRUE,
customPaging = cP2
)
output$slickRCarousel <- renderSlickR({
slick_dots_thumb <- slickR(
obj = df[,input$series],
height = 100,
width = "95%"
) + opts
})
}
shinyApp(ui, server)
编辑 2:基于 @ismirsehregal 的显示和导航解决方案
最后一块拼图是将中心或活动幻灯片值返回给服务器。slickR 文档说明您可以像这样访问它:
输入$mySlick_current$.center
可能需要通过 renderSlickR({}),而不是 renderUI({}) 创建 output$mySlick。
以下是来自@ismirsehregal 解决方案的一些更新代码:
library(shiny)
library(htmlwidgets)
library(slickR)
fish <- c(
"https://www.hakaimagazine.com/wp-content/uploads/aquarium-reef-fish-banner.jpg",
"https://www.hakaimagazine.com/wp-content/uploads/aquarium-reef-fish-banggai-cardinalfish.jpg",
"https://www.sonomamag.com/wp-content/uploads/2016/12/fish.jpg"
)
butterfly <- c(
"https://www.futurity.org/wp/wp-content/uploads/2019/02/viceroy-butterfly_1600.jpg",
"https://harvardforest.fas.harvard.edu/sites/harvardforest.fas.harvard.edu/files/butterfly_giant_swallowtail_model.jpg",
"https://www.butterflyidentification.com/wp-content/uploads/2019/02/Doris-Longwing-Butterfly-Images.jpg"
)
bird <- c(
"http://www.cutepetname.com/wp-content/uploads/2018/11/funy-bird-names-feat-img.jpeg",
"http://3.bp.blogspot.com/-mJ-Kw1mdLOo/UeLJx7vxsaI/AAAAAAAADkg/TfDHtuJnY7I/s1600/The-Cardinal-Bird.jpg",
"https://images7.alphacoders.com/416/thumb-1920-416332.jpg"
)
df <- data.frame(fish = fish,
butterfly = butterfly,
bird = bird)
ui <- fluidPage(uiOutput("mySlick"),
radioButtons(
'series',
"Choose Series",
choices = c(
"fish" = "fish",
"butterfly" = "butterfly",
"bird" = "bird"
)
),
uiOutput('imageInfo')
)
server <- function(input, output, session) {
output$mySlick <- renderUI({
cP2 <- JS(
"function(slick,index) {
return '<a><img src= ' + dotObj[index] + ' width=100% height=100%></a>';
}"
)
opts_dot_logo <-
settings(
initialSlide = 1,
slidesToShow = 1,
slidesToScroll = 3,
centerMode = TRUE,
focusOnSelect = TRUE,
dots = TRUE,
customPaging = cP2
)
s2 <- htmltools::tags$script(sprintf("var dotObj = %s", jsonlite::toJSON(df[[input$series]])))
slick_dots_logo <- slickR(obj = df[[input$series]],
height = 100,
width = "95%") + opts_dot_logo
htmltools::tagList(s2, slick_dots_logo)
})
observeEvent(input$series, ignoreInit = TRUE, {
output$imageInfo <- renderPrint({
paste("The center image is: ", input$mySlick_current$.center)
})
#print(input$mySlick_current$.center)
})
}
shinyApp(ui, server)
编辑 3:最终解决方案
感谢@ismirsehregal 在评论中提供的链接,我能够将中心幻灯片的索引传递回服务器。
代码:
library(shiny)
library(htmlwidgets)
library(slickR)
fish <- c(
"https://www.hakaimagazine.com/wp-content/uploads/aquarium-reef-fish-banner.jpg",
"https://www.hakaimagazine.com/wp-content/uploads/aquarium-reef-fish-banggai-cardinalfish.jpg",
"https://www.sonomamag.com/wp-content/uploads/2016/12/fish.jpg"
)
butterfly <- c(
"https://www.futurity.org/wp/wp-content/uploads/2019/02/viceroy-butterfly_1600.jpg",
"https://harvardforest.fas.harvard.edu/sites/harvardforest.fas.harvard.edu/files/butterfly_giant_swallowtail_model.jpg",
"https://www.butterflyidentification.com/wp-content/uploads/2019/02/Doris-Longwing-Butterfly-Images.jpg"
)
bird <- c(
"http://www.cutepetname.com/wp-content/uploads/2018/11/funy-bird-names-feat-img.jpeg",
"http://3.bp.blogspot.com/-mJ-Kw1mdLOo/UeLJx7vxsaI/AAAAAAAADkg/TfDHtuJnY7I/s1600/The-Cardinal-Bird.jpg",
"https://images7.alphacoders.com/416/thumb-1920-416332.jpg"
)
js <- "
$(document).ready(function(){
$('#mySlick').on('setPosition', function(event, slick) {
var index = slick.currentSlide + 1;
Shiny.setInputValue('imageIndex', index);
});
})"
df <- data.frame(fish = fish,
butterfly = butterfly,
bird = bird)
ui <- fluidPage(
tags$head(
tags$script(HTML(js))
),
uiOutput("mySlick"),
radioButtons(
'series',
"Choose Series",
choices = c(
"fish" = "fish",
"butterfly" = "butterfly",
"bird" = "bird"
)
),
uiOutput('imageInfo')
)
server <- function(input, output, session) {
output$mySlick <- renderUI({
cP2 <- JS(
"function(slick,index) {
return '<a><img src= ' + dotObj[index] + ' width=100% height=100%></a>';
}"
)
opts_dot_logo <-
settings(
initialSlide = 1,
slidesToShow = 1,
slidesToScroll = 3,
centerMode = TRUE,
focusOnSelect = TRUE,
dots = TRUE,
customPaging = cP2
)
s2 <- htmltools::tags$script(sprintf("var dotObj = %s", jsonlite::toJSON(df[[input$series]])))
slick_dots_logo <- slickR(obj = df[[input$series]],
height = 100,
width = "95%") + opts_dot_logo
htmltools::tagList(s2, slick_dots_logo)
})
observeEvent(input$series, ignoreInit = TRUE, {
output$imageInfo <- renderPrint({
paste("The center image is: ", df[[input$series]][input[['imageIndex']]])
})
print(input[['imageIndex']])
print( df[[input$series]][input[['imageIndex']]] )
})
}
shinyApp(ui, server)