library(slickR)
library(svglite)
library(shiny)
The Following example is a Shiny App that creates plot_num
histograms from rnorm
with n_obs
observations. These plots are placed in a slick carousel.
The use can choose between the different slick layouts
<- fluidPage(
ui sidebarLayout(
sidebarPanel(
sliderInput(inputId = "plot_num",
label = "Number of Plots:",
min = 1, max = 20, value = 5),
sliderInput(inputId = "n_obs",
label = "Number of observations:",
min = 10, max = 500, value = 100),
::radioButtons('slick_type',
shinylabel = 'Carousel Type',
choices = c('single','stack','synch'),
selected = 'single',
inline = TRUE),
::verbatimTextOutput('current')
shiny
),mainPanel(
slickROutput("slick_output",width='100%',height='200px')
)
) )
<- function(input, output) {
server
# Create content for the carousel
<- eventReactive(c(input$n_obs,input$plot_num),{
plots
replicate(input$plot_num,{
xmlSVG({hist(rnorm(input$n_obs),
col = 'darkgray',
border = 'white')},
standalone=TRUE)
simplify = FALSE)
},
})
# renderSlickR (We create the slickR objects here)
$slick_output <- renderSlickR({
output
<- slickR(plots(),
x slideId = 'myslick',
height = 600,
width = '50%') +
settings(slidesToShow=3,centerMode=TRUE)
switch(input$slick_type,
'single' = x,
'stack' = x %stack% x,
'synch' = x %synch% x
)
})
# Observe the active slick
# The htmlwidget is observed by shiny and information can be retrieved.
# Using the output name you set for the `renderSlick` object in this example
# it is `output$slick_output`
# Using this you can interact server-side "on click" of the active carousel
# by accessing elements in `input$slick_output_current$`
# `.clicked_slide` : The index of the clicked element|
# `.relative_clicked`: The relative position of the clicked element|
# `.center_slide` : The index of the center element|
# `.total_slide` : The total number of elements in the carousel|
# `.active_slide` : The ID of the active carousel|
# We will store this information in a new reactive environment
<- shiny::reactiveValues()
active_slick
::observeEvent(input$slick_output_current,{
shiny
<- input$slick_output_current$.clicked
clicked_slide <- input$slick_output_current$.relative_clicked
relative_clicked <- input$slick_output_current$.center
center_slide <- input$slick_output_current$.total
total_slide <- input$slick_output_current$.slide
active_slide
if(!is.null(clicked_slide)){
$clicked_slide <- clicked_slide
active_slick$center_slide <- center_slide
active_slick$relative_clicked <- relative_clicked
active_slick$total_slide <- total_slide
active_slick$active_slide <- active_slide
active_slick
}
})
# Show in the UI the values in active_slick
$current <- renderText({
output<- shiny::reactiveValuesToList(active_slick)
l paste(gsub('_',' ',names(l)), unlist(l),sep=' = ',collapse='\n')
})
}
shinyApp(ui = ui, server = server)